Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Hibernate Projection Example (Sum) 
 

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]

 

                         

» View all related tutorials
Related Tags: sql c class ui select framework static build application io methods types general method sed type factory new criteria applications

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.