Hibernate Criteria Lazy Loading
Posted on: April 9, 2011 at 12:00 AM
In this tutorial you will learn about the lazy loading in hibernate criteria

hibernate criteria lazy loading

To perform a Lazy Loading in Hibernate Criteria do the following

Criteria criteria = session.createCriteria(Contact.class);
	criteria.setFetchMode("Student", FetchMode.LAZY).add(
		Restrictions.eq("id", 1));

An example Lazy Loading is given below, Please consider the example

LazyLoading.javaADS_TO_REPLACE_1

package net.roseindia.main;

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

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

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

public class LazyLoading {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Contact.class);
		criteria.setFetchMode("Student", FetchMode.LAZY).add(
				Restrictions.eq("id", 1));

		List list = criteria.list();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Contact contact = (Contact) iterator.next();
			System.out.println(contact.getFirstName());
		}
	}
}

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

select this_.ID as ID1_0_, this_.first_name as first2_1_0_, this_.last_name as last3_1_0_ from contact this_ where this_.ID=?
Vinay

Download Complete Source Code

Related Tags for Hibernate Criteria Lazy Loading:

Advertisements

Ads

Ads

 
Advertisement null

Ads