Home i am getting data from database and query is executed successfully and could not extract the result by using group by id
Reply: 1

i am getting data from database and query is executed successfully and could not extract the result by using group by id

Vikas Biradargoudar
1#
Vikas Biradargoudar Published in 2018-01-12 10:30:29Z

How to do i get driver id and driver name separately?

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

service:

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

while(it.hasNext())
{

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

} 

result :

[Ljava.lang.Object;@1e65f750
[Ljava.lang.Object;@36d591df
Maciej Kowalski
2#
Maciej Kowalski Reply to 2018-01-12 10:34:49Z

1) Create a result class:

package my.package;

public class DriverResultClass{

   private Integer count;
   private String name;

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

2) Alter your query:

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

3) Change list type:

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

Hibernate will not automatically populate the POJO object and wont need to extract manually from a List of Object arrays.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307608 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO