问题描述
我有一个实体:
@Entity(name =target_group)
public class TargetGroup extends AbstractEntity {
私人字符串名称;
私有字符串描述;
@ManyToMany(fetch = FetchType.LAZY)
私人列表< Customer> customers = new ArrayList<>();
getter.setter ...
}
我有一个代码,以获得有关 stableId
(它位于抽象类中)的组列表:
public TargetGroup getTargetGroupByStableId(String stableId){
TargetGroup tg = null;
try {
Query q = em.createQuery(SELECT tg FROM TargetGroup tg WHERE tg.stableId =:stableId);
q.setParameter(stableId,stableId);
tg =(TargetGroup)q.getSingleResult();
logger.debug(TargetGroup reached:+ tg.generalInfo());
} catch(Exception e){
logger.error(通过stableId获取TargetGroup出错:+ stableId,e);
throw e;
}
return tg;
}
我得到这个错误:
SEVERE:通过stableId获取TargetGroup时出错:51a7b93f-a342-44ec-9849 -cd1688102f65
java.lang.IllegalArgumentException:org.hibernate.hql.ast .QuerySyntaxException:TargetGroup未映射[SELECT tg FROM TargetGroup tg WHERE tg.stableId =:stableId]
这是为什么?我错了什么,在代码中应该更改什么?谢谢!
这两个SELECT工作正常:
-
与映射名称
查询q = em.createQuery(SELECT tg FROM target_group tg WHERE tg.stableId =:stableId) ;
-
与全限定名称:
查询q = em.createQuery(SELECT tg FROM com.my_organisation.full.path.to.TargetGroup tg WHERE tg.stableId =:stableId);
I have an Entity:
@Entity(name = "target_group")
public class TargetGroup extends AbstractEntity {
private String name;
private String description;
@ManyToMany(fetch = FetchType.LAZY)
private List<Customer> customers = new ArrayList<>();
getter.setter...
}
And I have a code, to get a list about the groups with the stableId
(which is in the Abstract Class):
public TargetGroup getTargetGroupByStableId(String stableId) {
TargetGroup tg = null;
try {
Query q = em.createQuery("SELECT tg FROM TargetGroup tg WHERE tg.stableId = :stableId");
q.setParameter("stableId", stableId);
tg = (TargetGroup) q.getSingleResult();
logger.debug("TargetGroup reached: "+tg.generalInfo());
} catch(Exception e) {
logger.error("Error in getting TargetGroup by stableId:"+stableId,e);
throw e;
}
return tg;
}
And I am getting this error:
SEVERE: Error in getting TargetGroup by stableId:51a7b93f-a342-44ec-9849-cd1688102f65
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: TargetGroup is not mapped [SELECT tg FROM TargetGroup tg WHERE tg.stableId = :stableId]
Why is that? What do I wrong, what should I change in my code? Thank you!
This two SELECT worked fine:
with the mapped name
Query q = em.createQuery("SELECT tg FROM target_group tg WHERE tg.stableId = :stableId");
with the full qualified name:
Query q = em.createQuery("SELECT tg FROM com.my_organisation.full.path.to.TargetGroup tg WHERE tg.stableId = :stableId");
这篇关于为什么我的实体表映射在JPA Hibernate中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!