Home Hibernate Projections Hibernate Projection Example (Sum)



Hibernate Projection Example (Sum)
Posted on: March 10, 2008 at 12:00 AM
In this section, you will learn to hibernate aggregate function like: sum() using hibernate projection.

Hibernate Projection Example (Sum)

     

In this section, you will learn to hibernate aggregate function like: sum() using hibernate projection. 

The following example to calculate the sum of invested_amount to the Insurance table 

Table Name: Insurance

Here is the code of program:

package roseindia.tutorial.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class ProjectionExample1 {

  /**
 @param args
 */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Session sess = null;
  try {
  SessionFactory fact = new 
 
Configuration().configure().buildSessionFactory();
  sess = fact.openSession();
  Criteria crit = sess.createCriteria(Insurance.class);
  ProjectionList proList = Projections.projectionList();
  proList.add(Projections.sum("investementAmount"));
  crit.setProjection(proList);
  List sumResult = crit.list();
  System.out.println("Total Invested Amount: " + sumResult);
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  }

}

Download this Code.

Output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select sum(this_.invested_amount) as y0_ from insurance this_

Total Invested Amount: [51400]

 

Related Tags for Hibernate Projection Example (Sum):
chibernatefunctionfuniosumaggregateusingthislikeuncprojectionnattolearneareilsectionliinmjfuncprossuatkisllgreeaarssthgagatjeprojprono


More Tutorials from this section

Ask Questions?    Discuss: Hibernate Projection Example (Sum)   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.