tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Criteria Queries > Sum Projection

Sum Projection 

The following example shows how to use Hibernate sum aggregate projection. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/criteria/CriteriaProjectionSumTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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 = (Integercriteria.uniqueResult();    
    System.out.println("Total employees : " + avgEmpCnt);
    
    session.getTransaction().commit();
    session.close();  
  }
  
}
   

It gives the following output,
Total employees : 402944



 
  


  
bl  br