LazyInitializationException: could not initialize proxy-the owning Session was closed 异常
来源:互联网 发布:office for mac 破解版 编辑:程序博客网 时间:2024/04/27 17:19
Session.load() LazyInitializationException: could not initialize proxy-the owning Session was closed 异常
在 Hibernate 中,Session.load() 方法返回的是实体类的一个代理类的实例,而此时因为没有合适的事务处理代码,相应的 session 已经关闭,所以在执行第一条 assertEquals() 方法时,即报告“LazyInitializationException: could not initialize proxy-the owning Session was closed”异常。可以将 DAO 实现中的 load() 换成 get()。
return (FilterSet) getHibernateTemplate().get(FilterSet.class, id);
因为 get() 是返回的不是代理类的实例,而直接返回实体类的实例,所以上面的异常将不会出现。但是 FilterSet 中的 filters 属性为 Set 类型,而在 Hibernate 3 中,默认对所有 Collection 和 Map 都采用 lazy initialization 的方式,因此在这里,基于上面所说的原因,又会报告“org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net.patrickhe.FilterSet.filters, no session or session was closed” 这样的错误。所以,将 load() 换成 get() 其实并不是良好的解决办法。
---------------------------------------------------------------------------------------
ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
解决办法。
cmpy=(CompanyEdit)ss.load(CompanyEdit.class,companyId1);
Hibernate.initialize(cmpy);//强制初始化cmpy,否则ss.close()后,cmpy将消失.
---------------------------------------------------------------------------------------
在WEB级别应用时候,会出现意想不到的lazy错误,解决方法就是 设置 lazy="false" 例:
---------------------------------------------------------------------------------------
Lazy问题可以通过加opensessioninviewfilter解决 ------------(好像不行)
在web.xml中加上
<filter>
<filter-name>openSessionInViewFilter </filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter </filter-name>
<url-pattern>*.do </url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter </filter-name>
<url-pattern>*.jsp </url-pattern>
</filter-mapping>
<hibernate-mapping>
<class name="com.jeedev.hb.TSysuser" table="t_sysuser" lazy="false">
- LazyInitializationException:could not initialize proxy - the owning Session was closed
- LazyInitializationException: could not initialize proxy-the owning Session was closed 异常
- 异常:could not initialize proxy - the owning Session was closed
- 异常:could not initialize proxy - the owning Session was closed
- 异常:could not initialize proxy - the owning Session was closed
- 异常:could not initialize proxy - the owning Session was closed
- Could not initialize proxy - the owning Session was closed
- Could not initialize proxy - the owning Session was closed
- could not initialize proxy - the owning Session was closed
- Hibernate: Could not initialize proxy - the owning Session was closed
- Could not initialize proxy - the owning Session was closed
- Could not initialize proxy - the owning Session was closed
- could not initialize proxy - the owning Session was closed
- could not initialize proxy - the owning Session was closed
- Could not initialize proxy - the owning Session was closed
- could not initialize proxy - the owning Session was closed解决方法!
- Could not initialize proxy - the owning Session was closed[转]
- Could not initialize proxy - the owning Session was closed
- ftp获取文件列表的脚本
- Visual Studio 2008系列教程(一):VS 2008安装详解!
- 下雨的情绪
- 一个人的坚强
- Qt学习笔记--访问磁盘文件
- LazyInitializationException: could not initialize proxy-the owning Session was closed 异常
- c# comboBox 使用 linq to xml 绑定 XML文件
- 与drupal的缘份
- Silverlight 2教程中文版
- 如何在地址栏加上网站的标志、LOGO图片
- QT1106
- Android SDK Add-on Configure, Compile and Release
- WCF分布式开发
- 光头老熊做易赛是这样做教下家的