tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
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  :  kvenkatasudhakar@gmail.com
   
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


                    Print

   



Books Recommendation

 
 
bl  br