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

Hibernate Max() Function (Aggregate Functions)

In this section, we will show you, how to use the Max() function. Hibernate supports multiple aggregate functions.

Hibernate Max() Function (Aggregate Functions)

                         

In this section, we will show you, how to use the Max() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg(...), sum(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all...) ) calculated from property values of all objects satisfying other query criteria. 

Following is a aggregate function (max() function) with their respective syntax.

max( [ distinct | all ] object.property)

The Max() function aggregates the maximum value of the given column. 

Table Name: insurance

ID insurance_name invested_amount investement_date
2 Life Insurance 25000 0000-00-00 00:00:00
1 Givan Dhara 20000  2007-07-30 17:29:05
3 Life Insurance  500 2005-10-15 00:00:00
4 Car Insurance   2500 2005-01-01 00:00:00
5 Dental Insurance 500 2004-01-01 00:00:00
Life Insurance 900 2003-01-01 00:00:00
Travel Insurance  2000  2005-02-02 00:00:00

Here is the java code to retrieve the maximum value of "invested_amount" column from insurance table:

package roseindia.tutorial.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateHQLMaxFunction {

  /**
   * mail_mevinod@yahoo.co.in
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session sess = null;
    try {
      SessionFactory fact = new 
Configuration
().configure().buildSessionFactory();
      sess = fact.openSession();
      String SQL_QUERY = "select 
max(investementAmount)from Insurance insurance"
;
        Query query = sess.createQuery(SQL_QUERY);
        List list = query.list();
        System.out.println("Max 
Invested Amount: " 
+ list.get(0));   
      sess.close();
    }
    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 max(insurance0_.invested_amount) as col_0_0_ from insurance insurance0_

Max Invested Amount: 25000

                         

» View all related tutorials
Related Tags: sql c hibernate api com inheritance session class collections table binding parameters object bugs io connection classes focus object-oriented polymorphism

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 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

results.get(0), give it to me a Null?
public int siguienteIdDeclarante(){
int result = 0;
Session session = null;
try {
session = factory.openSession();
String SQL_QUERY = "select max(declarantesId) from Declarantes declarantes";
Query query = session.createQuery(SQL_QUERY);
List results = query.list();
System.out.println("Maximo " + results.get(0));
}catch(Exception e) {
result= -1;
System.out.println(e.toString());
}finally {
if (session != null) {
try {
session.close();
}
catch (HibernateException e) {
}
}
return result;
}
}


Thank you

Posted by Jorge on Tuesday, 12.8.09 @ 05:32am | #93159

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
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

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

Copyright © 2008. All rights reserved.