zoukankan      html  css  js  c++  java
  • hibernate>lazy

    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);
    		}
    	}	
    	
    }
    


     


     
     

  • 相关阅读:
    善于倾听,时刻清醒
    FlinkCDC读取MySQL并写入Kafka案例(com.alibaba.ververica)
    直线与圆弧插补采用DDS的模拟程序
    博客园的傻逼程序员开发的傻逼功能
    MeEdu知识付费系统文件上传功能
    微软商业智能(BI)分析服务从入门到精通读书笔记连载
    软件设计师&产品经理应常去的网站
    Windows Phone 7 Jump Start 系列教程
    设计学习《大象》阶段学习总结
    通过FxCop来验证.NET编码规范
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637192.html
Copyright © 2011-2022 走看看