论坛首页 Java企业应用论坛

Lazy Loading(两个session),既报错,又能拿到值,奇怪。

浏览 3712 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-12-01  
Policy.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping schema="life">
	<class name="com.ebao.test.hibernate.policy.Policy" table="T_POLICY_GENERAL" proxy="com.ebao.test.hibernate.policy.Policy" dynamic-update="true" dynamic-insert="true">
		<id name="policyId" column="POLICY_ID" type="long">
			<generator class="sequence">
				<param name="sequence">S_POLICY_GENERAL__POLICY_ID</param>
			</generator>
		</id>
<!--Property略-->
		<set name="insureds" lazy="true">
			<key column="policy_id"/>
			<one-to-many class="com.ebao.test.hibernate.policy.Insured"/>
		</set>
	</class>
</hibernate-mapping>

1:m中1那方的load
	public static Policy loadPolicy(Long policyId); throws HibernateException {
		Policy policy = null;
		
		Session sess = factory.openSession();;		
		try {
			policy = (Policy);sess.load(Policy.class, policyId);;
			print(policy);;
//			listInsureds(policy);;	
		} finally {
			sess.close();;
		}
		
		return policy;
	}

1:m中m那方的load
	public static Insured[] listInsureds(Policy policy); throws HibernateException {
		if (policy == null || policy.getInsureds(); == null || policy.getInsureds();.size(); == 0);
			return null;
			
		Session sess = factory.openSession();;		
		Set insuredsSet = policy.getInsureds();;
		Iterator iter = insuredsSet.iterator();;
		Insured[] insureds = new Insured[insuredsSet.size();];
		for (int i = 0, len = insuredsSet.size();; iter.hasNext();; i++); {
			insureds[i] = (Insured);iter.next();;
			log.debug(insureds[i].getPolicy();.getPolicyId(););;
			log.debug("标的" + (i + 1); + ": ");;
			print(insureds[i]);;
		}
		sess.close();;
		
		return insureds;
	}

main方法
Policy policy = loadPolicy(new Long(1047822););;	
		listInsureds(policy);;

部分运行结果
10:21:30,726 DEBUG PolicyService:88 - 1047822
10:21:30,726 DEBUG PolicyService:89 - 标的1: 
10:21:30,742 ERROR LazyInitializationException:25 - Failed to lazily initialize a collection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
	at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:167);
	at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64);
	at net.sf.hibernate.collection.Set.toString(Set.java:226);
	at com.ebao.test.hibernate.policy.PolicyService.print(PolicyService.java:148);
	at com.ebao.test.hibernate.policy.PolicyService.listInsureds(PolicyService.java:90);
	at com.ebao.test.hibernate.policy.PolicyService.main(PolicyService.java:183);
10:21:30,757 ERROR PolicyService:157 - model to string error 
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no Session
	at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:167);
	at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64);
	at net.sf.hibernate.collection.Set.toString(Set.java:226);
	at com.ebao.test.hibernate.policy.PolicyService.print(PolicyService.java:148);
	at com.ebao.test.hibernate.policy.PolicyService.listInsureds(PolicyService.java:90);
	at com.ebao.test.hibernate.policy.PolicyService.main(PolicyService.java:183);
10:21:30,757 DEBUG PolicyService:160 - com.ebao.test.hibernate.policy.Insured toString : 
Policy: com.ebao.test.hibernate.policy.Policy@18c0a1 
CurRealAnp: 150 
CurRealApa: 144.23 
CurRealApt: 150 
NetPrem: 150 
RecDate: 2003-10-15 11:58:40.0 
StdGrossPrem: 150 
StdNetPrem: 150 
SumInsured: 9999999999.99 
InsuredId: 259035 
InsuredName: Virtual Insured 
InsuredNum: 1 
LiabilityStatus: 1 
RiskCode: 1 
TreatTimes: 1 


我的本意是既然lazy loading在session关闭后就不能使用set对象了(不要Hibernate.initialize(),因为不能确定这时一定要准备set的数据),那就另外开一个session来补充,以可以拿到Policy对象中的Set insureds的值。

事实上也拿到insureds了,它的值都打印出来。但它同时也抛出LazyInitializationException 

跟踪PersistentCollection的方法initialize(),发现它在isConnectedToSession为false时抛出LazyInitializationException。就是说,它还是要关心load主对象的那个session,如果该session被关闭,它就抛异常。但在新的session中,还是可以使用它来读取set的值的。

这样就比较奇怪。自己目的达到了,但又有异常。或者是我的lazy loading不对?
   发表时间:2003-12-01  
Hibernate.initialize(o)不会初始化o关联的lazy的对象或集合。
0 请登录后投票
   发表时间:2003-12-01  
不好意思,那我一直理解错了。

我把initialize()方法加入到loadPolicy(),也还是抛出异常,那么请问应该怎样改呢?
0 请登录后投票
   发表时间:2003-12-01  
public static Policy loadPolicy(Long policyId); throws HibernateException { 
      Policy policy = null; 
       
      Session sess = factory.openSession();;       
      try { 
         policy = (Policy);sess.load(Policy.class, policyId);; 
         Hibernate.initialize(policy);;
         Hibernate.initialize(policy.getInsureds(););;
      } finally { 
         sess.close();; 
      } 
       
      return policy; 
   }
0 请登录后投票
   发表时间:2003-12-01  
sorry,我知道了,是Insured里面还有Set policyCts是lazy的,我没有initialize()它。

<hibernate-mapping schema="life">
	<class name="com.ebao.test.hibernate.policy.Insured" table="T_Insured_list" proxy="com.ebao.test.hibernate.policy.Insured" dynamic-update="true" dynamic-insert="true">
		<id name="insuredId" column="INSURED_ID" type="long">
			<generator class="sequence">
				<param name="sequence">S_INSURED_LIST__INSURED_ID</param>
			</generator>
		</id>
		<set name="policyCts" lazy="true">
			<key column="insured_id"/>
			<one-to-many class="com.ebao.test.hibernate.policy.PolicyCt"/>
		</set>
	</class>
</hibernate-mapping>

是否这种多级的一层一层1:m的lazy loading都要在最上层的对象(这里是policy)load处initialize()呢?就是说A:B=1:m, B:C=1:m,则要在laod A的地方initialize(a.getB());initialize(a.getB().getC())?

另外Hibernate.initialize()它的作用是什么?跟踪源码,发现也是到了PersistentCollection.initialize()方法。它这时确实没有读数据库吗?那我在后来没有session也可以难道的数据从哪里来?
0 请登录后投票
   发表时间:2003-12-01  
Hibernate.initialize()就是用来初始化(也就是载入)那些lazy(延迟载入的)proxy或者collection
0 请登录后投票
   发表时间:2003-12-01  
好,明白了。

我这里还是用两个session来实现初始化比较合适。谢谢yehs220。
0 请登录后投票
   发表时间:2004-01-17  
引用

Hibernate.initialize()就是用来初始化(也就是载入)那些lazy(延迟载入的)proxy或者collection

引用

Hibernate.initialize(o)不会初始化o关联的lazy的对象或集合。

yehs220你前后说的不一致,那到底hibernate.initialize()能不能初始话o关联的lazy的对象或集合
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics