Hibernate Criteria With Collections
Posted on: April 13, 2011 at 12:00 AM
In this tutorial you will learn about hibernate criteria with collections

Hibernate Criteria With Collections

An example of hibernate criteria with collections is given below

CriteriaCollection.java

package net.roseindia.main;

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

import net.roseindia.bean.Contact;
import net.roseindia.bean.Student;
import net.roseindia.util.HibernateUtil;

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.Subqueries;

public class CriteriaCollection {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria studentCriteria = session.createCriteria(Student.class);
		DetachedCriteria contactCriteria = DetachedCriteria
				.forClass(Contact.class);
		contactCriteria.add(Property.forName("id").eq(1));
		contactCriteria.setProjection(Projections.property("id"));
		studentCriteria.add(Subqueries.propertyIn("rollNo", contactCriteria));

		List list = studentCriteria.list();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Student student = (Student) iterator.next();
			System.out.println(student.getName());
		}

	}
}

Here is the video tutorial of "Hibernate Criteria Query on the Collection Object":ADS_TO_REPLACE_1

When you run this application it will display message as shown below:

Hibernate: select this_.roll_no as roll1_0_0_, this_.name as name0_0_, this_.course as course0_0_, this_.address as address0_0_ from student this_ where this_.roll_no in (select this_.ID as y0_ from contact this_ where this_.ID=?)
Ramesh

Download Complete Source Code

Related Tags for Hibernate Criteria With Collections :

Advertisements

Ads

Ads

 
Advertisement null

Ads