Home Hibernate Examples Criteria Hibernate criteria avg.

Ask Questions?

View Latest Questions


 
 

Hibernate criteria avg.
Posted on: April 13, 2011 at 12:00 AM
Advertisement
Hibernate criteria avg.

Hibernate criteria avg.

In this example, you will see the use of avg() method of Criteria class. It returns average value of the given column. 

Syntax :

Criteria criteria=session.createCriteria(Pojo.class);
criteria.setProjection(Projections.avg(propertyName));

CriteiaSort.java

package net.roseindia.action;

import java.util.List;
import net.roseindia.bean.StudentBean;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

public class CriteiaSort {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(StudentBean.class);
		criteria.setProjection(Projections.avg("fee"));
		List totalFee = criteria.list();
		System.out.println("Average value of fee column : " + totalFee);
	}
}
When you run this application it will display message as shown below:
Hibernate: select avg(this_.fee) as y0_ from studentinfo this_
Average value of fee column : [1500.0]

Download Complete Source Code

Related Tags for Hibernate criteria avg.:


DMCA.com