tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Criteria Queries > And Restriction

And Restriction 

The following example shows how to use Hibernate AND restriction. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/criteria/CriteriaAndRestrictionTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.hibernate.basic.criteria;

import java.util.List;

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

import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class CriteriaAndRestrictionTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Criteria criteria = session.createCriteria(Company.class);
    criteria.add(Restrictions.and(
        Restrictions.gt("id"new Integer(1)),  
        Restrictions.lt("id"new Integer(4))));
    
    List<Company> companies = criteria.list();    
    System.out.println("Selected row count : " + companies.size());
    
    for (Company company : companies) {
      System.out.println(company);
    }
    
    session.getTransaction().commit();
    session.close();
  }
  
}
   

It gives the following output,
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }
{ id = 3, name = MNO, employeeCount = 26790, 
	projectCount = 670, address1 = add111, address2 = add222 }



 
  


  
bl  br