Reputation: 2158
I have the following entity:
@Entity(name = "game_users")
public class GameUser {
private GameUsersPK primaryKey;
@EmbeddedId
public GameUsersPK getPrimaryKey() {
return primaryKey;
}
...
}
With the following PK:
@Embeddable
public class GameUsersPK implements Serializable {
@ManyToOne
private Game game;
@ManyToOne
private User user;
...
}
When I query for a GameUser
by executing:
GameUser gameUser = em.createQuery("from game_users", GameUser.class).setMaxResults(1).getSingleResult();
I notice Hibernate is executing two queries - one from game_users
and one from games left outer join users
.
Can I make Hibernate fetch all entities in one query - from game_users, games, users
?
Thanks.
Upvotes: 0
Views: 1518
Reputation: 691755
select gu from GameUser gu
left join fetch gu.primaryKey.game
left join fetch gu.primaryKey.user
Read the Hibernate documentation about HQL and associations.
Upvotes: 2