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()); } } }
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 |
Advertisements
Ads
Ads