JPA Nested Select Left Outer Join

房东的猫 提交于 2019-12-24 19:04:03

问题


Is it possible to do a nested select in JPA QL?

How would I write the following SQL statement in JPA QL?

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. I want to get all the PREFERENCES whether or not there is a PREFERENCESDISPLAY reference.


回答1:


In hibernate you can use "with":

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

But in JPA I have never seen such possibilities.




回答2:


Does that SQL even work?

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

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


来源:https://stackoverflow.com/questions/1395021/jpa-nested-select-left-outer-join

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!