如何使用LEFT OUTER JOIN创建JPA查询 [英] How to create a JPA query with LEFT OUTER JOIN
本文介绍了如何使用LEFT OUTER JOIN创建JPA查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始学习JPA,并基于我在SQL Server中测试的以下本机SQL实现了JPA查询示例:
I am starting to learn JPA, and have implemented an example with JPA query, based on the following native SQL that I tested in SQL Server:
SELECT f.StudentID, f.Name, f.Age, f.Class1, f.Class2
FROM Student f
LEFT OUTER JOIN ClassTbl s ON s.ClassID = f.Class1 OR s.ClassID = f.Class2
WHERE s.ClassName = 'abc'
来自上面的SQL我构建了以下JPQL查询:
From the above SQL I have constructed the following JPQL query:
SELECT f FROM Student f LEFT JOIN f.Class1 s;
如你所见,我仍然缺少条件或s.ClassID =来自我原始查询的f.Class2
。我的问题是,如何将它放入我的JPQL?
As you can see, I still lack the condition OR s.ClassID = f.Class2
from my original query. My question is, how can I put it into my JPQL?
推荐答案
写这个;
SELECT f from Student f LEFT JOIN f.classTbls s WHERE s.ClassName = 'abc'
因为您的学生实体与ClassTbl实体有一对多的关系。
Because your Student entity has One To Many relationship with ClassTbl entity.
这篇关于如何使用LEFT OUTER JOIN创建JPA查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文