could not initialize proxy - no Session (SSH)

网页异常信息:

HTTP Status 500 - org.hibernate.LazyInitializationException: failed to lazily initialize a 
collection of role: cn.ssh.wms.domain.Role.permissions, could not initialize proxy - no Session

从上信息可以看出没有找到Session代理

由于SSH整合时Session已经交给了Spring来管理-Spring在执行完毕查询时就会立即将Session关闭,所有当开启懒加载的查询去查找结果集时就会发现找不到Session所以就报错找不到Session ; 解决方法有两种:

方法一 : (POJO.hbm.xml)

<!-- 多对多权限关联
        	bag:去除重复元素
        	name:角色集合名称
        	table:数据库表名
        	lazy:是否开启懒加载模式,默认true
        		解决方案一:将lazy属性值改为false,关闭懒加载模式即可
         -->
        <bag name="permissions" table="role_permission" lazy="true">
        	<!-- 外键名称 -->
        	<key column="role_id"/>
        	<!-- class:关联实体类的地址
        		 column:数据库对应的列名
        	 -->
        	<many-to-many class="cn.ssh.wms.domain.Permission" column="permission_id"/>
        </bag>

方法二 : (web.xml)

 	<!-- OSIV 在struts2核心过滤器之前配置 -->
 	<filter>
 		<filter-name>OSIV</filter-name>
 		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
 	</filter>
 	<filter-mapping>
 		<filter-name>OSIV</filter-name>
 		<!-- 拦截所有.action请求 -->
 		<url-pattern>*.action</url-pattern>
 	</filter-mapping>

猜你喜欢

转载自blog.csdn.net/weixin_41124484/article/details/83040976