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

Average Projection 

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

File Name  :  
com/bethecoder/tutorials/hibernate/basic/criteria/CriteriaPropAvgProjRestrictionTest.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.Property;

import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class CriteriaPropAvgProjRestrictionTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Criteria criteria = session.createCriteria(Company.class);
    criteria.setProjection(Property.forName("employeeCount").avg());
    Double avgEmpCnt = (Doublecriteria.uniqueResult();    
    System.out.println("Average employee count : " + avgEmpCnt);
    
    session.getTransaction().commit();
    session.close();  
  }
  
}
   

It gives the following output,
Average employee count : 40294.4


                    Print

   



Books Recommendation

 
 
bl  br