Reputation: 823
I have 2 models with a @OneToOne relationship. Let's say a model User
and a model Player
. Each user is a player and each playeris a user.
Here is the code of these 2 models :
@Entity
public class User extends Model {
@Required
public String name;
@OneToOne(fetch = FetchType.EAGER)
public Player player;
}
@Entity
public class Player extends Model {
@Required
public String nickname;
@Required
public Gender gender;
}
I will always access a Player
from a User
and I want that when I load a User, his Player is also loaded (that's why I used fetch = FetchType.EAGER
).
So I expect (for optimization purposes) that when I load a User
, the query is a JOIN query that also loads the Player
.
Something like :
select u, p from User u join u.player p where u.player_id = p.id
But when I look at the queries count and the queries debug output I can see that 2 queries are performed. Something like:
select u from User u
select p from Player p where p.id = ?
But this is not optimized, how can I make JPA perform a join request to get my User
and its Player
?
Tank you for your help!
EDIT : I'm using Play framework 1.2.5
Upvotes: 0
Views: 823
Reputation: 3833
In the User class, as you noticed in your comment that the player can be null
public static void findByIdWithPlayer(Long id) {
find("select distinct user from User user left join fetch user.player where user.id=?", id).first();
}
Upvotes: 2