java 使用 HQL 从它们之间具有一对多关系的连接表中进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10564773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:35:11  来源:igfitidea点击:

Using HQL to select from joined tables having one to many relationship between them

javahibernatehqlhibernate-mapping

提问by Arun Stanislavose

I am stuck with this Hibernate thing and dont know how to figure it out. Please help !

我被这个 Hibernate 的东西困住了,不知道如何弄清楚。请帮忙 !

So I have these two tables:

所以我有这两个表:

POSITION
positionid(PK), description

JOB
jobid(PK),positionid(FK),description

How do I use HQL in Hibernate to fetch all jobs with their corresponding position descriptions ?

如何在 Hibernate 中使用 HQL 来获取所有作业及其相应的职位描述?

Edit: So this what I am trying to achieve:

编辑:所以这就是我想要实现的目标:

JOBID     POSITION.DESCRPTION     JOB.DESCRIPTION
  1        Teacher                Science Teacher
  2        Coach                  Football Coach

and so on for all job's in JOB table. I am trying to figure out what will be HQL for this.

等等,适用于 JOB 表中的所有工作。我想弄清楚这将是什么 HQL。

I have put together the following code till now :

到目前为止,我已经整理了以下代码:

position.hbm.xml

职位.hbm.xml

<hibernate-mapping>
  <class name="com.XXXX.model.Position" table="POSITION">
   <id name="positionID" type="int" column="POSITIONID" >
   <generator class="assigned"/>
  </id>

  <property name="description">
   <column name="DESCRIPTION" />
  </property>

  <set name="jobs">
    <key column="positionID" />
    <one-to-many class="com.XXXX.model.Job" />
  </set>

 </class>
</hibernate-mapping>

job.hbm.xml

作业.hbm.xml

<hibernate-mapping>

  <class name="com.XXXX.model.Job" table="JOB">
   <id name="jobID" type="int" column="JOBID" >
   <generator class="assigned"/>
  </id>

  <property name="description">
   <column name="DESCRIPTION" />
  </property>

  <many-to-one name="position" class="com.XXXX.model.Position" column="positionID" />

 </class>
</hibernate-mapping>

Position.java

位置.java

public class Position {

    private int positionID;
    private String description;
    private Set<Job> jobs = new HashSet<Job>();

    // Getters and Setters for all the above three follows...

}

Job.java

作业.java

public class Job {

    private int jobID;
    private String description;
    private Position position;

    // Getters and Setters for all the above three follows...
}

In my code now I use

在我的代码中,我现在使用

session.createQuery("from Position as p left join p.positionID as pid").list();

I know its not exactly correct and I am getting the follow error:

我知道它不完全正确,并且出现以下错误:

java.lang.NullPointerException
    at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:317)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3268)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3060)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    .........................
    ...........................

Can someone tell me how to fix this please ?

有人可以告诉我如何解决这个问题吗?

回答by JB Nizet

If you only want these three columns, then the HQL should be

如果你只想要这三列,那么 HQL 应该是

select job.id, position.decription, job.description 
from Job job
left join job.position position

This query will return a List<Object[]>and each Object array in the list will contain these three elements.

此查询将返回 aList<Object[]>并且列表中的每个 Object 数组将包含这三个元素。

It would be much more natural however to use this query:

然而,使用这个查询会更自然:

select job from Job job
left join fetch job.position

which would load all the jobs with their position. The query would return a List, and you would be able to access the three information uou want using job.getId(), job.getPosition().getDescription(), and job.getDescription().

这将加载所有工作及其位置。该查询将返回一个列表,你就可以访问uou想用这三个信息job.getId()job.getPosition().getDescription()job.getDescription()

The syntax of HQL is described with examples in the reference documentation, a must-read.

HQL 的语法在参考文档中通过示例进行了描述,必读。

回答by Chiefumpire

I think you can just getJobs() from the Position object

我认为您可以从 Position 对象中获取 getJobs()