简体   繁体   English

JPA嵌套选择左外部联接

[英]JPA Nested Select Left Outer Join

Is it possible to do a nested select in JPA QL? 是否可以在JPA QL中执行嵌套选择?

How would I write the following SQL statement in JPA QL? 如何在JPA QL中编写以下SQL语句?

select * from preferences p left outer join (select * from preferencesdisplay where user_id='XXXX') display on ap.pref_id=display.pref_id;

The JPA entity PREFERENCES has a OneToMany relationship to PREFERENCESDISPLAY . JPA的实体偏好具有PREFERENCESDISPLAY一个一对多的关系。 I want to get all the PREFERENCES whether or not there is a PREFERENCESDISPLAY reference. 无论是否有PREFERENCESDISPLAY参考,我都想获取所有的PREFERENCES

In hibernate you can use "with": 在休眠状态下,可以使用“ with”:

select ... from Preferences p left join p.displays d  with  d.user.id = 100

But in JPA I have never seen such possibilities. 但是在JPA中,我从未见过这种可能性。

Does that SQL even work? 那条SQL还能用吗?

It would help if you showed your Entities, however if you have everything correctly mapped it should be this easy: 如果您显示了实体,这将有所帮助,但是,如果您正确地映射了所有内容,那么应该很简单:

select p from Preferences p left join p.preferencesDisplays pd with pd.userId =XXXX

or if with isn't supported by TopLink 或者TopLink不支持with

select p from Preferences p left join p.preferencesDisplays pd
where pd IS NULL or pd.userId = XXXX

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

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