failed to lazily initialize a collection of role: com.itheima.bos.domain.Staff.decidedzones, could not initialize proxy – no Session

问题出在:could not initialize proxy – no Session at  无法初始化代理-没有session。  证明获取不到session,访问action的时候报了500错误,

配置懒加载一定要在web.xml开头配置代码,放在其他位置无效。

解决办法:这个是 由于web.xml没有配置懒加载。名词解析:延迟加载,也叫懒加载,它是Hibernate为提高程序执行效率而提供的一种机制,即只有真正使用该对象的数据时才会创建。

在web.xml里面配置懒加载,代码如下:

  <!-- 配置过滤器,解决hibernate延迟加载问题 -->  

<filter>  

<filter-name>openSessionInView</filter-name> 

 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>  

</filter>  

<filter-mapping>  <filter-name>openSessionInView</filter-name>  <url-pattern>/*</url-pattern>  

</filter-mapping>

最最最重要的一点是,其他博客都没有提到的,但是有必须要做的,配置懒加载一定要在web.xml开头配置代码。

我的web.xml配置如下:

这样即可完美解决。

原文地址:https://www.cnblogs.com/karmapeng/p/9715956.html