简体   繁体   English

在Hibernate中“一对一或一”映射中的左联接

[英]Left join in Hibernate “one to zero or one” mapping

I have two tables as below: 我有两个表,如下所示:

Student: oneID (primary key), col1, col2, col3, subId, col4
Subject: subID (primary key), col1, col2

Every student will have exactly one or none of the subjects assigned. 每个学生将只分配一个学科,也可以不分配任何学科。

So in the case: one subject assigned: subId in Student will have some value which maps to subID in Subject table 因此,在以下情况中:分配了一个主题:Student中的subId将具有一些值,该值映射到Subject表中的subID

and in the case: 0 subject assigned: subId in Student will be null. 在以下情况下:分配了0个主题:Student中的subId将为null。

With above scenario, I have a left join query as below: 在上述情况下,我有一个左联接查询,如下所示:

select st.col1, st.col2, su.col1, su.col2 from Student st left join Subject su on st.subId = su.subId where st.oneId = 'abc'

How to write the same exact query in Hibernate ? 如何在Hibernate中编写相同的查询?

select st.col1, st.col2, su.col1, su.col2 from Student st left join st.sub as su where st.oneId = 'abc'

Above query does not work and gives the below error: 上面的查询不起作用,并给出以下错误:

"org.hibernate.hql.ast.QuerySyntaxException: Path expected for join!"

What am I missing ? 我想念什么?

In my java code I have kept both the tables independent. 在我的Java代码中,我将两个表保持独立。 That is there is no relationship between the tables defined. 定义的表之间没有关系。

Persistence.xml is as below: Persistence.xml如下所示:

<persistence-unit name="myEntityManager" transaction-type="RESOURCE_LOCAL">              
  <provider>org.hibernate.ejb.HibernatePersistence</provider>    
  <class>com.myPackage.Student</class>       
  <class>com.myPackage.Subject</class>   
</persistence-unit> 

POJOs are as below: POJO如下:

Student 学生

@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {

@Id
@Column(name = "ONEID")
private String oneId;

private Subject sub;

//other columns here

public void setSub(final Subject sub) {
    this.sub = sub;
}

@OneToOne(cascade = CascadeType.ALL)
public Subject getSub() {
    return this.sub;
}
} 

Subject 学科

@Entity
@Table(name = "SUBJECT")
public class Subject implements Serializable {

@Id
@Column(name = "SUBID")
private String subId;

//other columns here
} 

As I don't see any association between your models in mapping you can refer to this link for your problem. 由于我在映射中看不到模型之间的任何关联,因此可以参考链接来解决问题。 Or simply add association in your mapping ie in student add many-to-one association for subject and modify your query as 或者只是在您的映射中添加关联,即在学生中为主题添加多对一关联并将查询修改为

select st.col1, st.col2, su.col1, su.col2 from Student st left outer join st.subject as su where st.oneId = 'abc' and st.subId = su.subId

As mentioned in this hibernate forum thread , it's currently not possible in Hibernate to invoke left join on disassociated entities. 就像在这个hibernate论坛线程中提到的那样,Hibernate当前无法在未关联的实体上调用左连接。 There's also an opened Hibernate issue for it. 还存在一个未解决的Hibernate问题

What you should do instead, is define a one-to-one or one-to-many relationship between Student and Subject ( one-to-one example , many-to-one example ). 您应该做的是在StudentSubject之间定义one-to-oneone-to-many关系( 一对一的示例多对一的示例 )。

Then, try this HQL, assuming the reference in Student to its subjects is named subjects : 然后,尝试使用此HQL,并假设“ Student中对其主题的引用被命名为subjects

select st.col1, st.col2, su.col1, su.col2 from Student st left join st.subjects su where st.oneId = 'abc'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM