本文介绍了为什么我的实体表映射在JPA Hibernate中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体:

  @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工作正常:


  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);



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:

  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");

这篇关于为什么我的实体表映射在JPA Hibernate中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:08