JPA 2.0 native query results as map

后端 未结 3 723
难免孤独
难免孤独 2020-12-01 14:25

I run a JPA 2.0 native query like this:

Query query = em.createNativeQuery(\"SELECT NAME, SURNAME, AGE FROM PERSON\");
List list = query.getResultList();
         


        
3条回答
  •  孤街浪徒
    2020-12-01 14:45

    Which JPA are you using - Hibernate, EclipseLink or something else?

    There is no standard way to do this in JPA but your specific implementation may allow it - for example, Eclipselink has a query result type hint.

    http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg03013.html

    Query query = entityManager.createNativeQuery(sql);
    query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    

    For Hibernate, with javax.persistence.Query dbQuery:

    org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)dbQuery)
    .getHibernateQuery();
    hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    

提交回复
热议问题