我被这个休眠的东西困住了,不知道如何解决。请帮忙 !
所以我有这两张表:
POSITION
positionid(PK), description
JOB
jobid(PK),positionid(FK),description
如何在 Hibernate 中使用 HQL 来获取具有相应职位描述的所有工作?
编辑:所以这就是我想要实现的:
JOBID POSITION.DESCRPTION JOB.DESCRIPTION
1 Teacher Science Teacher
2 Coach Football Coach
以此类推,适用于 JOB 表中的所有工作。我试图弄清楚这将是什么 HQL。
到目前为止,我已经整理了以下代码:
位置.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>
作业.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>
位置.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...
}
作业.java
public class Job {
private int jobID;
private String description;
private Position position;
// Getters and Setters for all the above three follows...
}
现在在我的代码中我使用
session.createQuery("from Position as p left join p.positionID as pid").list();
我知道它不完全正确,我收到以下错误:
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)
.........................
...........................
有人可以告诉我如何解决这个问题吗?