如何从nativeQuery中检索包含来自多个实体的数据的数据

时间:2016-01-08 00:03:02

标签: java single-page-application jpql nativequery

我有查询从多个实体返回数据

@Override
public List<PlayerDetails> testPlayerQuerry() {
    return copyTest(em.createNativeQuery("select player_name, pg.player_game_score, g.game_description from player p\n"
            + "join Player_Game pg on p.player_id = pg.fk_player_id\n"
            + "join Game g on pg.fk_game_id = g.game_id\n"
            + "where g.game_id = 2").getResultList());

从查询中检索从一个实体返回数据的数据相对简单,可以像这样完成

private List<PlayerDetails> copyPlayersToDetails(List< Player> players) {
List<PlayerDetails> list = new ArrayList<PlayerDetails>();
Iterator i = players.iterator();
while (i.hasNext()) {
    Player player = (Player) i.next();
    PlayerDetails details = new PlayerDetails(player.getPlayerId(),
            player.getPlayerName(), player.getPlayerRating());
    list.add(details);
}
return list;

}

问题是当查询存​​储来自多个实体的数据时如何执行此操作?

0 个答案:

没有答案