hibernate在单端关联上的lazy策略

本文探讨了Hibernate中懒加载(lazy)策略的应用,特别是在单端关联上,并通过具体示例展示了不同配置下懒加载的行为变化。

hibernate在单端关联上的lazy策略,可以取值:false/proxy/noproxy

<class>标签上的lazy不会影响到单端关联上的lazy特性

例子:

ContractedBlock.gifExpandedBlockStart.gifUser.hbm.xml
 1<?xml version="1.0"?>
 2<!DOCTYPE hibernate-mapping PUBLIC 
 3    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5<hibernate-mapping>
 6    <class name="com.bjsxt.hibernate.User" table="t_user" lazy="false">
 7        <id name="id">
 8            <generator class="native"/>
 9        </id>
10        <property name="name"/>
11        <!-- 
12        <many-to-one name="group" column="groupid" cascade="all"/>
13         -->
14         <many-to-one name="group" column="groupid"/>
15    </class>
16</hibernate-mapping>
ContractedBlock.gifExpandedBlockStart.gifGroup.hbm.xml
 1<?xml version="1.0"?>
 2<!DOCTYPE hibernate-mapping PUBLIC 
 3    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5<hibernate-mapping>
 6    <class name="com.bjsxt.hibernate.Group" table="t_group">
 7        <id name="id">
 8            <generator class="native"/>
 9        </id>
10        <property name="name"/>
11    </class>
12</hibernate-mapping>
ContractedBlock.gifExpandedBlockStart.gifSingleEndTest1.java
 1package com.bjsxt.hibernate;
 2
 3import org.hibernate.Session;
 4
 5import junit.framework.TestCase;
 6
 7ExpandedBlockStart.gifContractedBlock.gif/** *//**
 8 * 所有lazy属性默认
 9 * @author Administrator
10 *
11 */

12ExpandedBlockStart.gifContractedBlock.gifpublic class SingleEndTest1 extends TestCase {
13    
14ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testLoad1() {
15        Session session = null;
16ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
17            session = HibernateUtils.getSession();
18            session.beginTransaction();
19            
20            //不会发出sql
21            User user = (User)session.load(User.class1);
22            
23            //会发出sql
24            System.out.println("user.name=" + user.getName());
25            
26            //不会发出sql
27            Group group = user.getGroup();
28            
29            //会发出sql
30            System.out.println("group.name=" + group.getName());
31            
32            session.getTransaction().commit();
33ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
34            e.printStackTrace();
35            session.getTransaction().rollback();
36ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
37            HibernateUtils.closeSession(session);
38        }

39    }
    
40    
41}

42

 

ContractedBlock.gifExpandedBlockStart.gifSingleEndTest2.java
 1package com.bjsxt.hibernate;
 2
 3import org.hibernate.Session;
 4
 5import junit.framework.TestCase;
 6
 7ExpandedBlockStart.gifContractedBlock.gif/** *//**
 8 * 将<many-to-one>中的lazy设置为false,其它默认
 9 * @author Administrator
10 *
11 */

12ExpandedBlockStart.gifContractedBlock.gifpublic class SingleEndTest2 extends TestCase {
13    
14ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testLoad1() {
15        Session session = null;
16ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
17            session = HibernateUtils.getSession();
18            session.beginTransaction();
19            
20            //不会发出sql
21            User user = (User)session.load(User.class1);
22            
23            //会发出sql,发出两条sql分别加载User和Group
24            System.out.println("user.name=" + user.getName());
25            
26            //不会发出sql
27            Group group = user.getGroup();
28            
29            //不会发出sql
30            System.out.println("group.name=" + group.getName());
31            
32            session.getTransaction().commit();
33ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
34            e.printStackTrace();
35            session.getTransaction().rollback();
36ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
37            HibernateUtils.closeSession(session);
38        }

39    }
    
40    
41}

42

 

ContractedBlock.gifExpandedBlockStart.gifSingleEndTest3.java
 1package com.bjsxt.hibernate;
 2
 3import org.hibernate.Session;
 4
 5import junit.framework.TestCase;
 6
 7ExpandedBlockStart.gifContractedBlock.gif/** *//**
 8 * <class>标签上的lazy=false,其它默认
 9 * @author Administrator
10 *
11 */

12ExpandedBlockStart.gifContractedBlock.gifpublic class SingleEndTest3 extends TestCase {
13    
14ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testLoad1() {
15        Session session = null;
16ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
17            session = HibernateUtils.getSession();
18            session.beginTransaction();
19            
20            //会发出sql
21            User user = (User)session.load(User.class1);
22            
23            //不会发出sql
24            System.out.println("user.name=" + user.getName());
25            
26            //不会发出sql
27            Group group = user.getGroup();
28            
29            //会发出sql
30            System.out.println("group.name=" + group.getName());
31            
32            session.getTransaction().commit();
33ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e) {
34            e.printStackTrace();
35            session.getTransaction().rollback();
36ExpandedSubBlockStart.gifContractedSubBlock.gif        }
finally {
37            HibernateUtils.closeSession(session);
38        }

39    }
    
40    
41}

42

转载于:https://www.cnblogs.com/ksyou/archive/2009/04/02/1428139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值