org.hibernate.LazyInitializationException: could not initialize proxy - no Session

时间:2014-09-18 14:46:03   收藏:0   阅读:202

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

使用Hibernate延迟加载的时候报错了,原因是session提前关闭

解决方法1、不使用lazy,也就是@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)

改为@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)

成功解决错误,但是不能使用lazy延迟加载了,so

解决方案2、在web.xml中添加filter

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这样就可以使用lazy属性了

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!