HQL Group By Clause Example

Group by clause is used to return the aggregate values by grouping on returned component. HQL supports Group By Clause.

Ads

Tutorials   
Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example

HQL Group By Clause Example

     

Group by clause is used to return the aggregate values by grouping on returned component. HQL supports Group By Clause. In our example we will calculate the sum of invested amount in each insurance type. Here is the java code for calculating the invested amount insurance wise:


 

 

 

 
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net 
HQL Group by Clause Example

 *  
 */
public class HQLGroupByExample {
  public static void main(String[] args) {
  Session session = null;
  try {
  // This step will read 
hibernate.cfg.xml and prepare hibernate for

  // use
  SessionFactory sessionFactory = 
new Configuration().configure()
  .buildSessionFactory();
  session = sessionFactory.openSession();
  //Group By Clause Example
  String SQL_QUERY = "select sum
(insurance.investementAmount),
insurance.insuranceName "

  "from Insurance insurance 
group by insurance.insuranceName"
;
 Query query = session.createQuery(SQL_QUERY);
 for (Iterator it = 
query.iterate
(); it.hasNext();) {
 Object[] row = (Object[]) it.next();
 System.out.println("
Invested Amount: " 
+ row[0]);
 System.out.println("
Insurance Name: " 
+ row[1]);
  }
  session.close();
  catch (Exception e) {
  System.out.println(e.getMessage());
  finally {
  }
  }
}

To run the example select Run-> Run As -> Java Application from the menu bar. Following out is displayed in the Eclipse console:

Hibernate: select sum(insurance0_.invested_amount) as col_0_0_, insurance0_.insurance_name as col_1_0_ from insurance insurance0_ group by insurance0_.insurance_name

Invested Amount: 3500

Insurance Name: Car Insurance

Invested Amount: 500

Insurance Name: Dental Insurance

Invested Amount: 1550

Insurance Name: Home Insurance

Invested Amount: 1500

Insurance Name: Life Insurance

Invested Amount: 1600

Insurance Name: Medical Insurance

Invested Amount: 1680

Insurance Name: Motorcycle Insurance

Invested Amount: 2600

Insurance Name: Travel Insurance

Advertisements

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

HQL Group By Clause Example

Posted on: January 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: HQL Group By Clause Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
jmt
March 22, 2012
problem

package com.hibernate._4; import java.util.Iterator; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class GroupTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Session session = null; try{ SessionFactory factory = new Configuration().configure().buildSessionFactory(); session = factory.openSession(); String SQL_QUERY = "select sum(books.bookPrice)from Books books group by books.bookPrice"; Query query = session.createQuery(SQL_QUERY); for (Iterator it = query.iterate(); it.hasNext();) { Double[] row = (Double[]) it.next(); System.out.println("Sum: " + row[0]); } session.close(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { } } } this is what i got Hibernate: select sum(books0_.BOOKPRICE) as col_0_0_ from BOOKS books0_ group by books0_.BOOKPRICE java.lang.Double cannot be cast to [Ljava.lang.Double;
Manish Kumar Nayak
May 30, 2012
compalin

Hi this is good site.

Ads

 

Ads