Share on Google+Share on Google+

Hibernate Criteria :Ordering the results

In this section, you will learn about ordering the results using Hibernate Criteria query.

Hibernate Criteria :Ordering the results

In this section, you will learn about ordering the results using Hibernate Criteria  query.

Hibernate Criteria API permits to create and execute object oriented queries. It is more refined substitute of Hibernate Query Language (HQL). Criteria Query is more effective in case of multiple criteria search. The org.hibernate.Criteria interface is basically used to create the criteria for the search.

The project hierarchy is given below :

The code of the hibernate.cfg.xml is given below :

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

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">validate</property>

<mapping class="net.roseindia.Worker"/>


The code of is given below :

package net.roseindia;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "worker")
public class Worker {

@Column(name = "worker_id")
private Long workerId;

@Column(name = "firstname")
private String firstname;

@Column(name = "lastname")
private String lastname;

@Column(name = "birth_date")
private Date birthDate;

@Column(name = "cell_phone")
private String cellphone;

public Worker() {


public Worker(String firstname, String lastname, Date birthdate,
String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = birthdate;
this.cellphone = phone;


public Long getWorkerId() {
return workerId;

public void setWorkerId(Long workerId) {
this.workerId = workerId;

public String getFirstname() {
return firstname;

public void setFirstname(String firstname) {
this.firstname = firstname;

public String getLastname() {
return lastname;

public void setLastname(String lastname) {
this.lastname = lastname;

public Date getBirthDate() {
return birthDate;

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;

public String getCellphone() {
return cellphone;

public void setCellphone(String cellphone) {
this.cellphone = cellphone;

The code of the is given below :

package net.roseindia;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateCriteriaOrdering {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {
try {
Configuration configuration = new Configuration();
.setProperty("hibernate.show_sql", "false");
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
.println("********Hibernate Criteria Ordering Example********");
Session session = sf.openSession();

// Criteria crit = session.createCriteria(net.roseindia.Worker.class);
// crit.setMaxResults(5);
List workers = session.createCriteria(net.roseindia.Worker.class).add("firstname", "s%")).addOrder(
for (Iterator it = workers.iterator(); it.hasNext();) {
Worker worker = (Worker);
System.out.println("Worker ID: " + worker.getWorkerId());
System.out.println("First Name: " + worker.getFirstname());
System.out.println("Last Name: " + worker.getLastname());
System.out.println("Birth Date: " + worker.getBirthDate());
System.out.println("Cell Phone: " + worker.getCellphone());


********Hibernate Criteria Ordering Example********
Worker ID: 11
First Name: Sameer
Last Name: Nahata
Birth Date: 1981-09-30 00:00:00.0
Cell Phone: 919191929292
Worker ID: 9
First Name: Sushmita
Last Name: Dasgupta
Birth Date: 1987-05-21 00:00:00.0
Cell Phone: 919595959595

Download Source Code



Posted on: March 16, 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 :Ordering the results  

Post your Comment

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