问题
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!
回答1:
Using this annotation @Entity(name = "target_group")
you are renaming the default value (the name of the class) that is used to refer to the entity in queries (look here as a reference).
The fully qualified name of the class is needed only, as usual, when an ambiguity exists.
回答2:
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");
来源:https://stackoverflow.com/questions/19517502/why-isnt-my-entity-table-mapped-in-jpa-hibernate