Hibernate In Clause

In this section we will introduce concept of Hibernate In Clause.


Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query

Hibernate In Clause

In this section we will introduce concept of Hibernate In Clause.

Hibernate In Clause :

The IN clause allows user to specify multiple values in a WHERE clause. You can use IN clause with your collection.

Suppose you want to choose student record  in some collection of names  So you can use IN clause here.

Example :  This example satisfied your required condition as mentioned above. We are searching record of such students whose details are in the given collection name.

package net.roseindia.main;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class MainClazz {

public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
String hql = "SELECT stud.roll, stud.name, stud.course FROM Student stud WHERE stud.name IN(:name)";
Query query = session.createQuery(hql);
query.setParameterList("name", new String[] { "Ron", "Som", "Roxi"});

List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("RollNo.\t Name \t Course");
while (iterator.hasNext()) {
Map map = (Map) iterator.next();
System.out.print("\t" + map.get("1"));
System.out.print("\t" + map.get("2"));

session.close(); // session is closed


Output :

Hibernate: select student0_.roll_no as col_0_0_, student0_.name as col_1_0_, student0_.course as col_2_0_ from student student0_ where student0_.name in (? , ? , ?)
RollNo.     Name      Course
1           Ron       java
3           Roxi      unix

Click here to download complete source code

Share on Google+Share on Google+

Hibernate In Clause

Posted on: August 18, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate In Clause  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image