Home Hibernate Examples Criteria Hibernate Criteria Order by

Ask Questions?

View Latest Questions


 
 

Hibernate Criteria Order by
Posted on: April 5, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about the Order by api of hibernate criteria

Hibernate Criteria API Order By

The Order By API of hibernate criteria is used to display the results either in ascending or descending order. Suppose you have to write query like

SELECT * FROM Student ORDER BY roll_no DESC;

The same query can be written in hibernate as follows

Criteria criteria = session.createCriteria(Student.class);
	criteria.add(Restrictions.lt("rollNo", 10));
	criteria.addOrder(Order.desc("rollNo"));
	List list = criteria.list();

An Example of Criteria order by is given below

OrderByExample.java

package net.roseindia.main;

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

import net.roseindia.bean.Student;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class OrderByExample {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		Session session = HibernateUtil.getSessionFactory().openSession();

		Object object;

		try {
			// Creating a Criteria instance

			Criteria criteria = session.createCriteria(Student.class);
			criteria.add(Restrictions.lt("rollNo", 10));
			criteria.addOrder(Order.desc("rollNo"));
			List list = criteria.list();
			
			Iterator itr = list.iterator();
			System.out.println("\n");
			System.out.println("Student Details :- \n");
			System.out.println("\tRoll No"+"\t"+"Name"+"\t"+ "Course"+"\t"+ "Address\n");
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			while (itr.hasNext()) {
				object = (Object) itr.next();
				System.out.printf("\t");
				System.out.println(((Student) object).getRollNo()+"\t"+ ((Student) object).getName()+"\t"+ ((Student) object).getCourse()+"\t"+ ((Student) object).getAddress());
				}

		} catch (HibernateException e) {

			e.printStackTrace();
		} finally {
			session.close();
		}

	}
}

When you will run the above code you get the Output as follows:

Hibernate: select this_.roll_no as roll1_0_0_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_ from student this_ where this_.roll_no<? order by this_.roll_no desc


Student Details In descending Order :-

Roll No Name Course Address

4 Mahesh MCA Amritsar
3 Aman BCA Gorakhpur
2 Raman B.Tec Lucknow
1 John MCA Delhi

Hibernate: select this_.roll_no as roll1_0_0_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_ from student this_ where this_.roll_no<? order by this_.roll_no desc, this_.roll_no asc


Student Details In Ascending Order :-

Roll No Name Course Address

4 Mahesh MCA Amritsar
3 Aman BCA Gorakhpur
2 Raman B.Tec Lucknow
1 John MCA Delhi

Download Complete Source Code

Related Tags for Hibernate Criteria Order by:


DMCA.com