• October 15, 2014
  • Hibernate
  • Comments Off on Hibernate criteria restrictions query example

Hibernate criteria restrictions query example

Restrictions class provides the methods to restrict the search result based on the restriction provided.

Commonly used Restriction class methods:

1. Restrictions.eq: Make a restriction that the property value must be equal to the specified value.

Syntax: Restrictions.eq(“property”, specifiedValue)

2. Restrictions.lt: Make a restriction that the property value must be less than the specified value.

Syntax: Restrictions.lt(“property”, specifiedValue)

3. Restrictions.le: Make a restriction that the property value must be less than or equal to the specified value.

Syntax: Restrictions.le(“property”, specifiedValue)

4. Restrictions.gt: Make a restriction that the property value must be greater than the specified value.

Syntax: Restrictions.gt(“property”, specifiedValue)

5. Restrictions.ge: Make a restriction that the property value must be greater than or equal to the specified value.

Syntax: Restrictions.ge(“property”, specifiedValue)

6. Restrictions.like: Make a restriction that the property value follows the specified like pattern.

Syntax: Restrictions.like(“property”, “likePattern”)

7. Restrictions.between: Make a restriction that the property value must be between the start and end limit values.

Syntax: Restrictions.between(“property”, startValue, endValue)

8. Restrictions.isNull: Make a restriction that the property value must be null.

Syntax: Restrictions.isNull(“property”)

9. Restrictions.isNotNull: Make a restriction that the property value must not be null.

Syntax: Restrictions.isNotNull(“property”)



 * This class represents a persistent class for Student.
 * @author javawithease
public class Student {
	//data members
	private int studentId;
	private String firstName;
	private String lastName;
	private String className;
	private String rollNo;
	private int age;
	//no-argument constructor
	public Student(){
	//getter and setter methods
	public int getStudentId() {
		return studentId;
	public void setStudentId(int studentId) {
		this.studentId = studentId;
	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 String getClassName() {
		return className;
	public void setClassName(String className) {
		this.className = className;
	public String getRollNo() {
		return rollNo;
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration SYSTEM
        <property name="dialect">
        <property name="connection.url">
        <property name="connection.username">
        <property name="connection.password">
        <property name="connection.driver_class">
        <property name="hbm2ddl.auto">
        <property name="show_sql">
        <mapping resource="student.hbm.xml"/>


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping SYSTEM
 <class name="com.javawithease.business.Student" table="Student">
  <id name="studentId" type="int" column="Student_Id">
	<generator class="native"></generator>
  <property name="firstName" column="First_Name" type="string"/>
  <property name="lastName" column="Last_Name" type="string"/>
  <property name="className" column="Class" type="string"/>
  <property name="rollNo" column="RollNo" type="string"/>
  <property name="age" column="Age" type="int"/>


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 * This is a utility class for getting the hibernate session object.
 * @author javawithease
public class HibernateUtil {
    private static final SessionFactory sessionFactory = 
    private static SessionFactory buildSessionFactory() {
    	SessionFactory sessionFactory = null;
        try {
        	//Create the configuration object.
        	Configuration configuration = new Configuration(); 
        	//Initialize the configuration object 
                //with the configuration file data
        	// Get the SessionFactory object from configuration.
        	sessionFactory = configuration.buildSessionFactory();
        catch (Exception e) {
        return sessionFactory;
    public static SessionFactory getSessionFactory() {
        return sessionFactory;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.javawithease.persistence.HibernateUtil;
 * This class is used for the hibernate operations.
 * @author javawithease
public class HibernateTest {
    public static void main(String args[]){
          //Create the student object.
         Student student = new Student();
        //Setting the object properties.
        student.setClassName("MCA final");
        //Get the session object.
        Session session = 
        //Start hibernate transaction.
        //Persist the student object.
        //Commit hibernate transaction.
        //select a student record using Criteria restriction query
        Criteria criteria = session.createCriteria(Student.class);
        criteria.add(Restrictions.eq("rollNo", "MCA/07/21"));
        Student stu = (Student) criteria.uniqueResult();
        System.out.println("First Name: " + stu.getFirstName()); 
        System.out.println("Last Name: " + stu.getLastName()); 
        System.out.println("Class: " + stu.getClassName()); 
        System.out.println("RollNo: " + stu.getRollNo()); 
        System.out.println("Age: " + stu.getAge());  
        //Close hibernate session.


Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Student (First_Name, Last_Name, Class, 
RollNo, Age, Student_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: select this_.Student_Id as Student1_0_0_, 
this_.First_Name as First2_0_0_, this_.Last_Name as
Last3_0_0_, this_.Class as Class0_0_, this_.RollNo as
RollNo0_0_, this_.Age as Age0_0_ from 
Student this_ where this_.RollNo=?
First Name: Ashish
Last Name: Malik
Class: MCA final
RollNo: MCA/07/21
Age: 27

Download this example.
Next Topic: Hibernate criteria ordering query example.
Previous Topic: Hibernate criteria basic query example.

Related Topics:

Hibernate criteria basic query example.
Hibernate criteria restrictions query example.
Hibernate criteria ordering query example.
Hibernate criteria pagination query example.
Hibernate criteria projections query example.


Comments are closed.