LEFT JOIN ON() in JPQL

a 夏天 提交于 2019-12-12 10:44:19

问题


I have two entities:

  • User: id:long, name:String
  • Player: id:long, owner:User, points:int

Now I want to select a User and his associated Player in one JPQL query. In SQL I'd do it like this:

SELECT u.*, p.* FROM User u
LEFT JOIN Player p ON (p.owner_id = u.id)
WHERE u.name = ...

My first instinct was to do it like this in JPQL

SELECT u, p FROM User u LEFT JOIN Player p ON (p.owner = u) WHERE u.name = ...

But I don't think the ON clause is supported in JPQL. I do need it however, because User has no reference to Player (many things other than Player could be attached to a User). How can I solve this one?


回答1:


You have a relationship from Player to User, so you can invert the join to follow it:

SELECT u, p FROM Player p RIGHT JOIN p.owner u WHERE ...


来源:https://stackoverflow.com/questions/4992834/left-join-on-in-jpql

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