Home Hibernate Examples Criteria Hibernate Criteria Greater Than

Ask Questions?

View Latest Questions


 
 

Hibernate Criteria Greater Than
Posted on: April 8, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about Hibernate Criteria Greater Than

Hibernate Criteria Greater Than

The Hibernate Criteria Greater Than is used the fetch the value from the database which is Greater than the given value. gt() method of Restriction class is used to find greater value from a given value. gt() takes two parameters one is field name and other is value. You can use gt() method in a following way.

Criteria criteria = session.createCriteria(Student.class);
	criteria.add(Restrictions.gt("rollNo", 2));
	List list = criteria.list();

An Example of greater than is given below please consider the example

GreaterThan.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.Restrictions;

public class GreaterThan {
	public static void main(String[] args) {
		try {
			Student student;
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Student.class);
			criteria.add(Restrictions.gt("rollNo", 2));
			List list = criteria.list();
			Iterator itr = list.iterator();
			System.out.println("\n");
			System.out.println("Student Details :- \n");
			if (list.size() == 0) {
				System.out.println("No Result Found !");
			}
			System.out.println("Roll No\tName\tCourse\tAddress");
			System.out.println();
			while (itr.hasNext()) {
				student = (Student) itr.next();
				System.out.println(student.getRollNo() + "\t"
						+ student.getName() + "\t" + student.getCourse() + "\t"
						+ student.getAddress());
			}

		} catch (HibernateException e) {

			e.printStackTrace();
		}
	}
}

When you run this application it will display message as shown below:

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>?


Student Details :-

Roll No Name Course Address

3 Aman BCA Gorakhpur
4 Raman MCA Amritsar
5 Vikash MBA Patna
6 Ramesh DCA Gwalior

Download Complete Source Code

Related Tags for Hibernate Criteria Greater Than :


DMCA.com