开发者

How to map count query to pojo class?

开发者 https://www.devze.com 2023-03-19 18:34 出处:网络
I\'m trying to execute the query which returns a count of the employees and the department id. How can I assign to pojo. And how to retrieve the resultset from query using hibernat开发者_C百科e? See m

I'm trying to execute the query which returns a count of the employees and the department id. How can I assign to pojo. And how to retrieve the resultset from query using hibernat开发者_C百科e? See my code below:

select e.depid, count(empid) from empwithdep e,
   dept d  where e.depid=d.depid group by e.depid order by e.depid


You can create an appropriate constructor for your POJO and then use constructor syntax:

select new DepartmentInfo(e.depid, count(empid)) from empwithdep e,
    dept d  where e.depid=d.depid group by e.depid order by e.depid 


When a query returns projections rather than entities, the returned list is a List<Object[]>. Each Object[] contains the columns returned by the query:

List<Object[]> rows = query.list();
for (Object[] row : rows) {
    Long depId = (Long) row[0];
    Long count = (Long) row[1];
    // create a POJO using depId and count...
}

This is described in the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消