ratna rathor
Using sum() in hibernate criteria.
1 Answer(s)      3 years and 6 months ago
Posted in : Hibernate

How to calculate sum() in hibernate criteria by using projection?

View Answers

June 4, 2012 at 7:41 PM

You can calculate sum of any numeric value in hibernate criteria by using projection. Projection is an interface and a class in ?org.hibernate.criterion?. For using Projection object in our Criteria, we have to call setProjection method.

Here is an example:

package net.roseindia.projection;

import java.util.*;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;

public class SumProjection{
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria=session.createCriteria(Employee.class);

        List employeeList = (List)criteria.list();

        for(Object employee: employeeList){


Hibernate: select sum(this_.salary) as y0_ from employee this_

Description: Here we are calculating sum of all employee salary by using


Related Tutorials/Questions & Answers: