首页 > 代码库 > 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 - 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属性了

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