tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Hibernate Query Language > Min Max Aggregate Functions

Min Max Aggregate Functions 

The following example shows using HQL min, max and count aggregate functions. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLMinMaxCountTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.hibernate.basic.tests;

import java.util.Arrays;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class HQLMinMaxCountTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = "select max(projectCount), min(employeeCount), count(*) from Company comp";
    Object[] row = (Object[]) session.createQuery(HQL_QUERY).uniqueResult();    
    System.out.println(Arrays.toString(row));

    System.out.println("Max project count : " + row[0]);
    System.out.println("Min Employee count : " + row[1]);
    System.out.println("Company count : " + row[2]);
    
    session.getTransaction().commit();
    session.close();
  }

}
   

It gives the following output,
[8478, 478, 10]
Max project count : 8478
Min Employee count : 478
Company count : 10



 
  


  
bl  br