hibernate学习8之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特性只对普通属性起作用


集合上的lazy策略

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打开状态下有效
//在Spring中设置openSessionInViewFilter可以解决这个问题。
		System.out.println("group.name=" + group.getName());
	}	
	
}


hibernate在集合上的lazy策略,可以取值:true/false/extra

<class>标签上的lazy不会影响到集合上的lazy特性
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);
		}
	}		
}


设置集合上的lazy=false,其它默认
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);
		}
	}		
}



设置集合上的lazy=extra(推荐),其它默认
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;

/**
 * 设置集合上的lazy=extra,其它默认
 * @author Administrator
 *
 */
public class CellectionlazyTest4 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);
		}
	}		
}


单端关联上的lazy策略
import org.hibernate.Session;

import junit.framework.TestCase;

/**
 * 所有lazy属性默认
 * @author Administrator
 *
 */
public class SingleEndTest1 extends TestCase {
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//不会发出sql
			User user = (User)session.load(User.class, 1);
			
			//会发出sql
			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);
		}
	}	
	
}


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



import org.hibernate.Session;

import junit.framework.TestCase;

/**
 * <class>标签上的lazy=false,其它默认
 * @author Administrator
 *
 */
public class SingleEndTest3 extends TestCase {
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//会发出sql
			User user = (User)session.load(User.class, 1);
			
			//不会发出sql
			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);
		}
	}	
	
}

你可能感兴趣的:(spring,sql,Hibernate,JUnit,单元测试)