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

后端 未结 21 1772
孤街浪徒
孤街浪徒 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:43

    In hibernate you can use this code to easily map your native query.

    private List < Map < String, Object >> getNativeQueryResultInMap() {
    String mapQueryStr = "SELECT * FROM AB_SERVICE three ";
    Query query = em.createNativeQuery(mapQueryStr);
    NativeQueryImpl nativeQuery = (NativeQueryImpl) query;
    nativeQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    List < Map < String, Object >> result = query.getResultList();
    for (Map map: result) {
        System.out.println("after request  ::: " + map);
    }
    return result;}
    

提交回复
热议问题