Share on Google+Share on Google+

Hibernate Criteria Grouping Example

In this tutorial you will learn about the Hibernate Criteria Projection Grouping.

Hibernate Criteria Grouping Example

In this tutorial you will learn about the Hibernate Criteria Projection Grouping.

As we were using a 'group by' clause explicitly in HQL for grouping, there is no need to use this clause in Criteria query. In Criteria query for grouping projections certain types of projection are defined.

Click Here for Hibernate Criteria Ordering tutorial


In the example given below I have used the Projections.groupProperty() for grouping the projections. We may use Property.forName() besides the built-in Projections with the setProjection() method. You can specify the number of columns by using the Projections.projectionList().add(

Table goodsdealer

CREATE TABLE `goodsdealer` ( 
`goodsDealerId` int(15) NOT NULL, 
`orderedGoodsName` varchar(15) default NULL, 
`numOfGoodsOrder` int(20) default NULL, 
`priceOfGoods` int(10) default NULL 

package roseindia;

public class GoodsDealer {
private int goodsDealerId;
private int numOfGoodsOrder; 
private int priceOfGoods;
private String orderedGoodsName; 

public GoodsDealer()
public GoodsDealer(int priceOfGoods, int goodsDealerId, int numOfGoodsOrder, String 
this.priceOfGoods = priceOfGoods;
this.goodsDealerId = goodsDealerId;
this.numOfGoodsOrder = numOfGoodsOrder;
this.orderedGoodsName = orderedGoodsName;
public int getPriceOfGoods() {
return priceOfGoods;
public void setPriceOfGoods(int priceOfGoods) {
this.priceOfGoods = priceOfGoods;
public int getGoodsDealerId() {
return goodsDealerId;
public void setGoodsDealerId(int goodsDealerId) {
this.goodsDealerId = goodsDealerId;
public int getNumOfGoodsOrder() {
return numOfGoodsOrder;
public void setNumOfGoodsOrder(int numOfGoodsOrder) {
this.numOfGoodsOrder = numOfGoodsOrder;
public String getOrderedGoodsName() {
return orderedGoodsName;
public void setOrderedGoodsName(String orderedGoodsName) {
this.orderedGoodsName = orderedGoodsName;


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate mapping DTD//EN"

<hibernate-mapping package="roseindia">
<class name= "GoodsDealer" table="goodsdealer">
<id name= "goodsDealerId" type="int" column="goodsDealerId">
<property name="numOfGoodsOrder">
<column name="numOfGoodsOrder"/>
<property name="orderedGoodsName">
<column name="orderedGoodsName"/>
<property name="priceOfGoods">
<column name="priceOfGoods"/>


package roseindia;

import java.util.Iterator;
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.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateCriteriaGroupingExample {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String args[])
Session session = null;
Configuration cfg= new Configuration().addResource("roseindia/GoodsDealer.hbm.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
catch(Throwable th)
System.err.println("Failed to create sessionFactory object."
+ th);
throw new ExceptionInInitializerError(th);
session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(GoodsDealer.class);
// To group the rows in table goodsdealer
// or it can be also written as 
List groupBy = criteria.list();
Iterator it = groupBy.iterator();
System.out.println("numOfGoodsOrder priceOfGoods");
Object[] obj = (Object[]);
System.out.println(obj[0]+"\t\t "+ obj[1]);
catch(Exception e)


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>


Output :

table goodsdealer

When you will execute the java file (RightClick -> RunAs -> Java Application) an output will be displayed on your console as :

INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 21, 2012 6:41:03 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select this_.numOfGoodsOrder as y0_, this_.priceOfGoods as y1_, this_.priceOfGoods as y2_ from goodsdealer this_ group by this_.priceOfGoods

Download Source Code


Posted on: March 21, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Criteria Grouping Example  

Post your Comment

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