我有以下实体:

@Entity(name = "game_users")
public class GameUser {

    private GameUsersPK primaryKey;

    @EmbeddedId
    public GameUsersPK getPrimaryKey() {
        return primaryKey;
    }
    ...
}


具有以下PK:

@Embeddable
public class GameUsersPK implements Serializable {
    @ManyToOne
    private Game game;

    @ManyToOne
    private User user;
    ...
}


当我通过执行查询GameUser时:

GameUser gameUser = em.createQuery("from game_users", GameUser.class).setMaxResults(1).getSingleResult();


我注意到Hibernate正在执行两个查询-一个来自game_users和一个来自games left outer join users

我可以让Hibernate在一个查询-from game_users, games, users中获取所有实体吗?

谢谢。

最佳答案

select gu from GameUser gu
left join fetch gu.primaryKey.game
left join fetch gu.primaryKey.user


阅读Hibernate documentation about HQL and associations

10-04 13:14