|
Sum Projection
The following example shows how to use Hibernate sum aggregate projection.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.criteria;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class CriteriaProjectionSumTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Company.class);
criteria.setProjection(Projections.sum("employeeCount"));
Integer avgEmpCnt = (Integer) criteria.uniqueResult();
System.out.println("Total employees : " + avgEmpCnt);
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Total employees : 402944
|
|