tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Hibernate > Hibernate Query Language > Where in Clause

Where in Clause 

The following example shows using HQL where in clause. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLWhereInClauseTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.hibernate.basic.tests;

import java.util.List;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class HQLWhereInClauseTest {

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

    System.out.println("Where in clause with company ids");
    getCompanies("from Company comp where comp.id in (1, 3)");
    
    System.out.println("Where in clause with company names");
    getCompanies("from Company comp where comp.name in ('ABC', 'PQR')");
  }

  private static void getCompanies(String hql) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    List<Company> companies = (List<Company>session.createQuery(hql).list();    
    
    for (Company company : companies) {
      System.out.println(company);
    }
    
    session.getTransaction().commit();
    session.close();
  }
}
   

It gives the following output,
Where in clause with company ids
{ id = 1, name = PQR, employeeCount = 3430, 
	projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 3, name = MNO, employeeCount = 26790, 
	projectCount = 670, address1 = add111, address2 = add222 }
	
Where in clause with company names
{ id = 1, name = PQR, employeeCount = 3430, 
	projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }


                    Print

   



Books Recommendation

 
 
bl  br