Hibernate criteria sort.
Posted on: April 11, 2011 at 12:00 AM
Hibernate criteria sort.

Hibernate criteria sort.

In this tutorial, we will introduce to you how to sort values in using criteria in hibernate. The Criteria is class, which is available in org.hibernate.criteria package.

Syntax :

Criteria criteria=session.createCriteria(Pojo.class);


package net.roseindia.action;

import java.util.Iterator;
import java.util.List;
import net.roseindia.bean.StudentBean;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

public class CriteiaSort {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(StudentBean.class);
		List list = criteria.list();
		Iterator studentIter = list.iterator();
		System.out.println("   Roll No      Name      Address   ");

		while (studentIter.hasNext()) {
			StudentBean student = (StudentBean) studentIter.next();
			System.out.print("\t"+ student.getRoll() +"\t");
			System.out.print(student.getName() +"\t");
			System.out.print(student.getAddress() + "\n");

When you run this application it will display message as shown below:
 Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.class as class0_0_, this_.address as address0_0_, this_.email as email0_0_, this_.joindate as   joindate0_0_ from studentinfo this_ order by this_.name desc
 Roll No      Name     Address
3               vrishti      Kanpur
5               Rohit         Barabanki
4              parineeta  GKP
2             gyan           Bareilly
1              bharat        Bareilly

Download Complete Source Code

Related Tags for Hibernate criteria sort.: