我创建了一个原生查询
Query q=em.createNativeQuery("select *\n" +
"from contaarticoli ca,articolo a\n" +
"where ca.articolo=a.id\n" +
"order by ca.qta desc\n" +
"limit 1;");
在工作台中,我创建了一个视图:
create view contaarticoli(articolo,qta)as
(
select a.idarticolo as articolo,sum(a.quantita) as qta
from articoliordine a
group by a.idarticolo
);
当我这样做时:
List<Object> lista=q.getResultList();
System.out.println(lista.get(0))
在输出中我有:[Ljava.lang.Object; @ 1c5b8a4
为什么?
答案 0 :(得分:3)
因为你回来了List<Object[]>
。每个数组具有相同的列数,对应于查询列。
答案 1 :(得分:1)
使用Query结果和Object执行本机查询,直到您在createNativeQuery方法中定义resultClass属性。
createNativeQuery
Query createNativeQuery(java.lang.String sqlString,
java.lang.Class resultClass)
Create an instance of Query for executing a native SQL query.
Parameters:
sqlString - a native SQL query string
resultClass - the class of the resulting instance(s)
如果您有一个与查询结果字段名称相同的实体,请添加resultClass = Class.class并自动创建对象
您是否正在使用2个表之间的连接,您可以使用@SqlResultSetMapping。
@SqlResultSetMapping(name="ScalarAndEntities",
entities={
@EntityResult(name="org.hibernate.test.annotations.query.Night", fields = {
@FieldResult(name="id", column="nid"),
@FieldResult(name="duration", column="night_duration"),
@FieldResult(name="date", column="night_date"),
@FieldResult(name="area", column="area_id")
}),
@EntityResult(name="org.hibernate.test.annotations.query.Area", fields = {
@FieldResult(name="id", column="aid"),
@FieldResult(name="name", column="name")
})
},
columns={
@ColumnResult(name="durationInSec")
}
)
与
一起使用Query q = entityManager.createNativeQuery(sqlQuery, "ScalarAndEntities");
详细了解如何从nativequeries here,
中检索实体