JPA : How to convert a native query result set to POJO class collection

后端 未结 21 1764
孤街浪徒
孤街浪徒 2020-11-22 09:23

I am using JPA in my project.

I came to a query in which I need to make join operation on five tables. So I created a native query which returns five fields.

21条回答
  •  遥遥无期
    2020-11-22 09:45

    Since others have already mentioned all the possible solutions, I am sharing my workaround solution.

    In my situation with Postgres 9.4, while working with Jackson,

    //Convert it to named native query.
    List list = em.createNativeQuery("select cast(array_to_json(array_agg(row_to_json(a))) as text) from myschema.actors a")
                       .getResultList();
    
    List map = new ObjectMapper().readValue(list.get(0), new TypeReference>() {});
    

    I am sure you can find same for other databases.

    Also FYI, JPA 2.0 native query results as map

提交回复
热议问题