简体   繁体   中英

JPA / Hibernate - How to EAGERly fetch EmbeddedId?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM