Why isn't my Entity Table mapped in JPA Hibernate?

别来无恙 提交于 2019-12-25 05:03:22

问题


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:

  1. with the mapped name

    Query q = em.createQuery("SELECT tg FROM target_group tg WHERE tg.stableId = :stableId");

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!