LEFT JOIN ON () in JPQL

I have two objects:

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

Now I want to select the user and the player associated with him in a single JPQL query. In SQL, I would 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 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. However, I need this because User does not have a reference to Player (many things, except Player , can be tied to User ). How can I solve this problem?

+6
java sql jpa entity jpql
source share
1 answer

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

 SELECT u, p FROM Player p RIGHT JOIN p.owner u WHERE ... 
+3
source share

All Articles