hibernate lazy策略可以使用在:
* <class>标签上,可以取值:true/false ,在hibernate3以上版本,默认是true
* <property>标签上,可以取值:true/false需要类增强工具
* <set><list>标签上,可以取值:true/false/extra
* <one-to-one><many-to-one>单端关联上,可以取值:false/proxy/no-proxy
lazy概念:只有真正使用该对象时,才会创建,对于hibernate而言,正真使用的时候才会发出sql
hibernate支持lazy策略只有在session打开状态下有效
=====================================================
1. <class>标签上:
hbm
group.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.model">
<class name="Group" table="group5" lazy="true" > //lazy,默认true,可不写
<id name="id" column="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" column="name" length="50" type="java.lang.String" />
</class>
</hibernate-mapping>
测试用例:
public class LazyTest extends TestCase {
public void testLoad1(){
Session session = null;
Transaction ta = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Group g2 = (Group) session.load(Group.class, 1); //还没发出sql,lazy起延迟作用,若lazy=false,则发出sql
// Group g2 = (Group) session.get(Group.class, 1); //不支持lazy
System.out.println("group.id=" + g2.getId()); //还没发出sql,
System.out.println("group.name=" + g2.getName()); //发出sql
ta.commit();
}catch(Exception e){
e.printStackTrace();
if(ta != null){
ta.rollback();
}
}finally{
//关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
//System.out.println("group.name=" + g2.getName()); //hibernate支持lazy策略只有在session打开状态下有效,所以此出Exception
}
}
<class>标签上的lazy特性只对普通属性起作用
<class>标签上的lazy不会影响到单端关联上的lazy特性
=========================================
2.<set><list>标签上,可以取值:true/false/extra,默认是true
hbm.xml
Classes.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zd.model">
<class name="Classes" table="classes" >
<id name="id" column="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String" />
<set name="students" lazy="true"> //可不配lazy,因默认是true
<key column="class_id" />
<one-to-many class="com.zd.model.Student" />
</set>
</class>
</hibernate-mapping>
测试用例:
public void testLoad1(){
Session session = null;
Transaction ta = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Classes c = (Classes) session.load(Classes.class, new Integer(2)); //没有sql
System.out.println("Class.name=" + c.getName());//发出一条sql,但不查 set
Set stuSet = c.getStudents();//没有发出查询sql,不是统计sql
//System.out.println(stuSet.size());//发出查询sqlsql
if(stuSet != null && !stuSet.isEmpty()){//发出查询sqlsql
for(Iterator it = stuSet.iterator(); it.hasNext();){
Student s = (Student) it.next();//若没有.size(),isEmpty(),就在这边发出sql
System.out.println("student.name=" + s.getName());
}
}
ta.commit();
}catch(Exception e){
e.printStackTrace();
if(ta != null){
ta.rollback();
}
}finally{
//关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes classes0_ where classes0_.id=?
Class.name=Java Class
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student students0_ where students0_.class_id=?
2
student.name=z3
student.name=l4
若<set name="students" lazy="false"> //不延迟加载, 马上加载
则在
System.out.println("Class.name=" + c.getName());// 就发出2条查询语句了。
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes classes0_ where classes0_.id=?
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student students0_ where students0_.class_id=?
Class.name=Java Class
student.name=l4
student.name=z3
若<set name="students" lazy="extra"> //和true差不多,只是在写set.size()时,发出selcet count的sql语句,比true好一些。
测试用例:
public void testLoad1(){
Session session = null;
Transaction ta = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Classes c = (Classes) session.load(Classes.class, new Integer(2));
System.out.println("Class.name=" + c.getName());
Set stuSet = c.getStudents();
System.out.println(stuSet.size());
if(stuSet != null && !stuSet.isEmpty()){
for(Iterator it = stuSet.iterator(); it.hasNext();){
Student s = (Student) it.next();
System.out.println("student.name=" + s.getName());
}
}
ta.commit();
}catch(Exception e){
e.printStackTrace();
if(ta != null){
ta.rollback();
}
}finally{
//关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes classes0_ where classes0_.id=?
Class.name=Java Class
Hibernate: select count(id) from student where class_id =?
2
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student students0_ where students0_.class_id=?
student.name=z3
student.name=l4
===============================================
3.<one-to-one><many-to-one>单端关联上,可以取值:false/proxy/no-proxy,默认是proxy(代理),延迟加载作用
hbm.xml
User.hbm.xml 多的一端
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.model">
<class name="User" table="user1" >
<id name="id" column="user_id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" length="50" type="java.lang.String" />
<many-to-one name="group" column="group_id" lazy="proxy"></many-to-one> //可不写,默认是proxy
</class>
</hibernate-mapping>
测试用例:
public void testGet1(){
Session session = null;
Transaction ta = null;
User user = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
user = (User)session.load(User.class, new Integer(3)); //无sql
System.out.println("user.name=" + user.getName()); //有一条sql
Group group = user.getGroup();//无sql
System.out.println("group.name=" + group.getName());//有一条sql
ta.commit();
}catch(Exception e){
e.printStackTrace();
ta.rollback();
}finally{
//关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
若<many-to-one name="group" column="group_id" lazy="false"></many-to-one>
不延迟加载,立即加载,
System.out.println("user.name=" + user.getName()); //发出2条sql语句
==========================================
hibernate 中的 lazy="proxy" 和 lazy="no-proxy" 到底是什么意思?
举个例子吧:
Child <- many-to-one ->Parent
class Child {
private Parent parent;
public Parent getParent (){
return this.parent;//访问了实例变量
}
}
class Parent {
private String name;
public String getName(){
return this.name;//访问了实例变量
}
public void f(){
System.out.println("invokeing f()");//没有访问实例变量
}
}
如果 many-to-one 的lazy设为proxy,当child.getParent().getName()或child.getParent().f()时,parent都会被抓取,若设为no-proxy,调用child.getParent().f()时,parent是不会被抓取的,同时这种方式需要编译时字节码增强,否则和proxy没区别。 (注:测试发现真和proxy一样,不能理解 编译时字节码增强,要再哪修改,或是什么条件?)
如果设置了 lazy="proxy",就 ,当通过 child 来调用其关联的 parent, 如果调用 parent 类中定义的任何方法,都会抓取 parent (所谓的抓取是不是就是从数据库查询,执行一次 select ?)
如果设置了 lazy="no-proxy" ,只有调用 parent 类中牵涉到类变量的方法,才会抓取 parent,否则,就像调用 f(), 不会抓取 parent
."编译时字节码增强" 是什么意思?
"字节码增强"分编译期和运行期2种,编译期是修改java类编译后的class字节码文件,在上面附加“增强”操作。(不明白why?)
================
lazy (可选 - 默认为 proxy): 默认情况下,单点关联是经过代理的。lazy="no-proxy"指定此属性应该在实例变量第一次被访问时应该延迟抓取(fetche lazily)(需要运行时字节码的增强)。 lazy="false"指定此关联总是被预先抓取。注意,如果constrained="false", 不可能使用代理,Hibernate会采取预先抓取!
来源:oschina
链接:https://my.oschina.net/u/217401/blog/49551