﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-JAVA-最新评论</title><link>http://www.blogjava.net/girlsbe/CommentsRSS.aspx</link><description /><language>zh-cn</language><pubDate>Sun, 19 Nov 2006 04:17:24 GMT</pubDate><lastBuildDate>Sun, 19 Nov 2006 04:17:24 GMT</lastBuildDate><generator>cnblogs</generator><item><title>re: Hibernate的关联关系映射</title><link>http://www.blogjava.net/girlsbe/archive/2006/11/10/80362.html#80400</link><dc:creator>拉猪</dc:creator><author>拉猪</author><pubDate>Fri, 10 Nov 2006 06:52:00 GMT</pubDate><guid>http://www.blogjava.net/girlsbe/archive/2006/11/10/80362.html#80400</guid><description><![CDATA[当出现　org.hibernate.LazyInitializationException:could not initialize proxy - the owing Session was closed　的错误时，是因为在集合类(collection)映射时，hibernate3默认的初始化方式是延迟初始化（laze initialize），即集合(不包括数组)是可以延迟初始化的,意思是仅仅当应用程序需要访问时，才载入他们的值。换句话说，如果有以下写法，则会抛出上述异常：<br><br>       s = sessions.openSession();<br> User u = (User) s.find(&quot;from User u where u.name=?&quot;, userName, Hibernate.STRING).get(0);<br>    Map permissions = u.getPermissions();<br>       s.connection().commit();<br>    s.close();<br>  Integer accessLevel = (Integer) permissions.get(&quot;accounts&quot;);  // Error!<br><br> 因为在这个Session被提交(commit)之前, permissions没有被初始化,那么这个集合将永远不能载入他的数据了。 解决方法是把读取集合数据的语句提到Session被提交之前。(然而，还有一种更先进的方法来解决这个问题。)<br>      另外一种选择是不使用延迟初始化集合。既然延迟初始化可能引起上面这样错误,hibernate2默认是不使用延迟初始化的。但是, 为了效率的原因, hibernate3对集合(特别是实体集合)使用延迟初始化。<br>   现在明白了，对于collection（实体类集合），hibernate2的默认不使用初始化方式，而hibernate3为了提高查询效率，默认采用了延迟初始化方式。<br>        解决方式很简单：<br>    在当前collection对象的hbm.xml配置文件中，加入laze=&quot;false&quot;即可。如下：<br>       &lt;many-to-one name=&quot;...&quot;  class=&quot;...&quot; column=&quot;...&quot; not-null=&quot;false&quot; lazy=&quot;false&quot; /&gt;<br>   由于在我当前项目中，DAOHibernate并没有采用sessions.openSession()和close()方式，所以只能采用修改其hbm.xml配置文件的方式来解决。<br><br><img src ="http://www.blogjava.net/girlsbe/aggbug/80400.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/girlsbe/" target="_blank">拉猪</a> 2006-11-10 14:52 <a href="http://www.blogjava.net/girlsbe/archive/2006/11/10/80362.html#80400#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>