1

I have query which returns data from multiple entities

@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());

Retrieving data from query which returns data form one entity is relatively straight forward it can be done like this

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;

}

The question is how to do it when query stores data from more than one entity?

  • It will be returned into an Object Array, use a debugger to view the data and decide how to extract the data. – Scary Wombat Jan 08 '16 at 00:13
  • this is interesting suggestion I am beginner and I would never think about using debugger :-) –  Jan 08 '16 at 00:26
  • The debugger is your friend, you can learn so much from it, especially if you have the source code of jars – Scary Wombat Jan 08 '16 at 00:33
  • If you are not satisfied with [the suggestion](http://stackoverflow.com/questions/34666244/retrieving-data-from-native-query-and-conversion#comment57080508_34666244) on your previous question, take a look at [this answer](http://stackoverflow.com/a/30295937/1391249). – Tiny Jan 08 '16 at 01:45
  • I do wonder why you're using native queries in the first place, instead of JPQL... – Lukas Eder Jan 08 '16 at 09:51
  • I was trying to use JPQL in the first place but I have this weird playerGameCollections in main entity classes which made joining tables painful, and I couldn't figure out how to make those queries, thats why I did use native query due to simpler syntax so I wrote mysql query test it in console and implemented to the code –  Jan 08 '16 at 12:51

0 Answers0