1

I want to return a List of maps from my createNativeQuery().getResultList(), where each map is a pair key - value representing column name - value. I already tried to use direct in the method like this:

public List<Map<String, Object>> execQuery(String nativeQuery) {
    return entityManager().query(nativeQuery).getResultList();
}

but it always return List. Someone knows if what I want is even possible?
The JPA implementation that I'm using is Hibernate. I'm currently using Java 8 (don't know if this information is relevant for my case).
Any help is welcome. Thanks in advance.

4

2 回答 2

2

You can to use the ResultTransformer to transform your results in a map form. Following the oficial documentation

Like this:

List<Map<String,Object>> mapaEntity = session
    .createQuery( "select e  from Entity" )
    .setResultTransformer(new AliasToEntityMapResultTransformer())
    .list();

于 2016-07-22T19:34:48.093 回答
1

Please try with

Query q1 = entityManager().query(nativeQuery); 
org.hibernate.Query hibernateQuery =((org.hibernate.jpa.HibernateQuery)q1) .getHibernateQuery(); 
hibernateQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);‌​
于 2016-07-22T19:18:36.297 回答