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

Detached Criteria In 

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

File Name  :  
com/bethecoder/tutorials/hibernate/basic/criteria/DetachedCriteriaSubqueriesTest.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.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

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

public class DetachedCriteriaSubqueriesTest {

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

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    DetachedCriteria reqComps = 
      DetachedCriteria.forClass(Company.class)
        .setProjection(Projections.property("name"))
        .add(Restrictions.gt("id"8));
    
    Criteria criteria = session.createCriteria(Company.class);
    //criteria.add(Subqueries.in("name", reqIds));
    criteria.add(Property.forName("name").in(reqComps));
    
    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,
Selected row count : 2
{ id = 9, name = CFR, employeeCount = 478, 
	projectCount = 2487, address1 = null, address2 = add2443 }
{ id = 10, name = CFR, employeeCount = 6478, 
	projectCount = 887, address1 = , address2 = add24435 }



 
  


  
bl  br