我从数据库获取数据并且查询成功执行,无法通过使用group by id提取结果

时间:2018-01-12 10:30:29

标签: java spring hibernate jpa

如何分别获取驱动程序ID和驱动程序名称?

String hql="select count(tx.driver.driverid),tx.driver.drivername from Transaction tx group by driver"; 
return   (List)hibernateTemplate.find(hql);

服务:

List list=driverService.groupby();
Iterator it=list.iterator();

while(it.hasNext())
{

    Object obj=(Object)it.next();
    System.out.println(obj);

} 

结果:

[Ljava.lang.Object;@1e65f750
[Ljava.lang.Object;@36d591df

1 个答案:

答案 0 :(得分:0)

1)创建结果类:

package my.package;

public class DriverResultClass{

   private Integer count;
   private String name;

   public DriverResultClass(Integer count, String name){
      // set fields
   }
}

2)更改您的查询:

select new my.package.DriverResultClass(count(tx.driver.driverid),tx.driver.drivername) 
from Transaction tx 
group by tx.driver.drivername 

3)更改列表类型:

(List<DriverResultClass>)hibernateTemplate.find(hql)

Hibernate不会自动填充POJO对象,也不需要从对象数组列表中手动提取。