hibernate lazy策略
hibernate lazy策略可以使用在:
*<class>标签上,可以取值:true/false
<property>标签上,可以取值:true/false,指定实例变量第一次被访问时,这个属性是否延迟抓取,需要运行时字节码增强。
* <set><list>标签上,可以取值:true/false/extra
*<one-to-one><many-to-one>单端关联上,可以取值:false/proxy/noproxy
lazy概念:只有真正使用该对象时,才会创建,对于hibernate而言,正真使用的时候才会发出sql
hibernate支持lazy策略只有在session打开状态下有效
<class>标签上的lazy特性只对普通属性起作用,不影响list,set类型
一、class上的lazy
1、设置<class>标签上的lazy=true,也就是默认配置
package com.ncepu.hibernate; import org.hibernate.Session; import junit.framework.TestCase; /** * 运行本单元测试的条件: * * 设置<class>标签上的lazy=true,也就是默认配置 * * @author Administrator * */ public class ClassLazyTest extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql Group group = (Group)session.load(Group.class, 1); //不会发出sql System.out.println("group.id=" + group.getId()); //会发出sql System.out.println("group.name=" + group.getName()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad2() { Session session = null; Group group = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); group = (Group)session.load(Group.class, 1); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } //不能正确输出,抛出LazyInitializationException 异常,因为session已经关闭 //hibernate支持lazy策略只有在session打开状态下有效 System.out.println("group.name=" + group.getName()); } }
2、设置<class>标签上的lazy=false
package com.ncepu.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import junit.framework.TestCase; import org.hibernate.Session; /** * 设置<class>标签上的lazy=false * @author Administrator * */ public class CellectionlazyTest2 extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //不会发出sql System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //会发出sql for (Iterator iter=students.iterator(); iter.hasNext();) { Student student = (Student)iter.next(); System.out.println("student.name=" + student.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }
二、集合上的Lazy
hibernate在集合上的lazy策略,可以取值:true/false/extra
设置成extra,比如查询size时,会发比较智能的sql语句,利用count,而不是全部查出来。
<class>标签上的lazy不会影响到集合上的lazy特性,
1、默认为true时
package com.ncepu.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import junit.framework.TestCase; import org.hibernate.Session; /** * 保持lazy默认 * @author Administrator * */ public class CellectionlazyTest1 extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //会发出sql System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //会发出sql for (Iterator iter=students.iterator(); iter.hasNext();) { Student student = (Student)iter.next(); System.out.println("student.name=" + student.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //会发出sql System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //会发出sql,发出查询全部数据的sql System.out.println("student.count=" + students.size()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }
2、集合上lazy为false时
package com.ncepu.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import junit.framework.TestCase; import org.hibernate.Session; /** * 设置集合上的lazy=false,其它默认 * @author Administrator * */ public class CellectionlazyTest3 extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //会发出sql,会发出两条sql分别加载Classes和Student System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //不会发出sql for (Iterator iter=students.iterator(); iter.hasNext();) { Student student = (Student)iter.next(); System.out.println("student.name=" + student.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //会发出sql,会发出两条sql分别加载Classes和Student System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //不会发出sql System.out.println("student.count=" + students.size()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }
3、集合上lazy为extra时
package com.ncepu.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import junit.framework.TestCase; import org.hibernate.Session; /** * 设置<class>标签上的lazy=false * @author Administrator * */ public class CellectionlazyTest2 extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //会发出sql Classes classes = (Classes)session.load(Classes.class, 1); //不会发出sql System.out.println("classes.name=" + classes.getName()); //不会发出sql Set students = classes.getStudents(); //会发出sql for (Iterator iter=students.iterator(); iter.hasNext();) { Student student = (Student)iter.next(); System.out.println("student.name=" + student.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }
三、单端关联上lazy
package com.ncepu.hibernate; import org.hibernate.Session; import junit.framework.TestCase; /** * 将<many-to-one>中的lazy设置为false,其它默认 * @author Administrator * */ public class SingleEndTest2 extends TestCase { public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //不会发出sql User user = (User)session.load(User.class, 1); //会发出sql,发出两条sql分别加载User和Group System.out.println("user.name=" + user.getName()); //不会发出sql Group group = user.getGroup(); //不会发出sql System.out.println("group.name=" + group.getName()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }