Search code examples
javajpajpqlnativequery

Retrieving data from native query and conversion


I have following query

@Override
public List<PlayerDetails> testPlayerQuerry() {
    return copyPlayersToDetails(em.createNativeQuery("select player_name 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());
}

which as far as I am aware it return list of String? due to the error I am getting

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to entities.Player at utils.SessionFacade.copyPlayersToDetails(SessionFacade.java:105)

the class which throws the error it is working fine with queries which returns player String for example return * from players

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;
}

How I have to convert this method or how (if possible) I can get those values stored in this String list in to JSF page so I can retrieve the query results?


Solution

  • Your copyPlayersToDetails() method expects a list of Player entities, not a list of String. This is why you get a ClassCastException here:

    Player player = (Player) i.next();
    

    Because i.next() returns a String. The reason why your Java compiler didn't complain is because Query.getResultList() (unfortunately) returns a raw type list.

    You should change your native query to this:

    @Override
    public List<PlayerDetails> testPlayerQuerry() {
        return copyPlayersToDetails(em.createNativeQuery("select p.* 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", Player.class).getResultList());
    }
    

    Further criticism

    • You should (probably) use a bind variable
    • You don't need to join the Game table

    Write this instead:

    @Override
    public List<PlayerDetails> testPlayerQuerry() {
        return copyPlayersToDetails(em.createNativeQuery("select p.* from player p\n"
              + "join Player_Game pg on p.player_id = pg.fk_player_id\n"
              + "where pg.fk_game_id = :1", Player.class).setParameter(1, 2).getResultList());
    }