为什么不在JPA Hibernate中映射我的实体表?

时间:2013-10-22 12:10:31

标签: sql hibernate jpa entity jpql

我有一个实体:

@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...
}

我有一个代码,以获取有关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("Error in getting TargetGroup by stableId:"+stableId,e);
        throw e;
    }
    return tg;
}

我收到了这个错误:

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]

为什么?我错了什么,我的代码应该改变什么?谢谢!

2 个答案:

答案 0 :(得分:1)

使用此批注@Entity(name = "target_group"),您将重命名用于在查询中引用实体的默认值(类的名称)(look here as a reference)。 只有在存在歧义时才需要该类的完全限定名称。

答案 1 :(得分:0)

这两个SELECT工作正常:

  1. 带有映射名称

    查询q = em.createQuery(“SELECT tg FROM target_group tg WHERE tg.stableId =:stableId”);

  2. 使用完全限定名称:

    查询q = em.createQuery(“SELECT tg FROM com.my_organisation.full.path.to.TargetGroup tg WHERE tg.stableId =:stableId”);