org.hibernate.LazyInitializationException
程序员文章站
2022-03-02 14:13:55
...
org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role:cn.its.oa.domain.Department.children,no session or session was closed
(在ssh框架下的项目中出现了这个错误,被老鸟一眼就看出了问题所在,改正过来了,在这里记录一下)分析下导致这种异常的出现的原因:在设置两个实体之间多对一的关系的时候 对象实例化失败(使用了懒加载:这里使用的是hibernate3,默认lazy=”proxy”即”true”)使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常。
解决办法
- 使用openSessionInView 过滤器
<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>/*</url-pattern>
</filter-mapping>
- 取消这个类的延迟加载(hibernate3中所有的实体设置文件(*.hbm.xml)中的lazy属性都被默认设成了true)所以设置:
lazy="false"
推荐阅读
-
解决:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
-
org.hibernate.LazyInitializationException: could not initialize proxy [xxxx] - no Session异常
-
Hiberante异常之org.hibernate.LazyInitializationException: could not initialize proxy - no Session
-
【记录】异常:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
-
org.hibernate.LazyInitializationException: could not initialize proxy 解决方案(JPA)
-
Jpa项目报错:org.hibernate.lazyinitializationexception could not initialize proxy - no session
-
org.hibernate.LazyInitializationException: could not initialize proxy no Session
-
jpa 解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session
-
Spring DATA JPA org.hibernate.LazyInitializationException: could not initialize proxy - no Session
-
org.hibernate.LazyInitializationException: could not initialize proxy - no Session