ssh整合之Session延迟加载问题的解决
问题描述:在使用Hibernate和Struts是经常会遇到如下BUG:
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.hibernate.LazyInitializationException:
failed to lazily initialize a collectionof role: blank.domain.Route.stopRoutes, could not initialize proxy - no Session
解决方案:
方法一、在web.xml中添加Session过滤器的配置
<!-- 延迟关闭session 的顺序位于struts2过滤之上 否则延迟关闭session不起作用 -->
<filter>
<filter-name>opensession</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
由于org.springframework.orm.hibernate4.support.OpenSessionInViewFilter这个类中指定sessionFactoryBeanName的值为“sessionFactory”
所以,在Spring配置文件中,与SessionFactory相关的配置需要把名称改为“sessionFactory”,否则会报找不到sessionFactory的BUG。
具体如下:
<!-- SessionFactory工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置HibernateTemplate模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置HibernateDaoSupport -->
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate4.support.HibernateDaoSupport"
abstract="true">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<!-- Hinernate的事务管理器 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
方法二、在Hibernate的*.hbm.xml配置文件中将lazy属性设置为false(不推荐)。
案例:
stopRoute.hbm.xml中
<many-to-one name="route" class="Route" cascade="save-update" fetch="join" lazy="false">
<column name="rid" />
</many-to-one>
<many-to-one name="stop" class="Stop" cascade="save-update" fetch="join" lazy="false">
<column name="sid" />
</many-to-one>
Route.hbm.xml中
<set name="stopRoutes" inverse="true" cascade="delete" lazy="false">
<key>
<column name="rid" />
</key>
<one-to-many class="StopRoute" />
</set>
ssh整合之Session延迟加载问题的解决的更多相关文章
- ssh整合时报出的异常及解决办法
com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$Mis ...
- 用ssh整合时,用sessionfactory的getCurrentSession()获取不到session
在用ssh整合时,一开始用的是getCurrentSession(),获取当前线程上的session,但是总是抛异常,不能获取. 后来用sessionfactory的openSession(),但是, ...
- 【转载】Ssh整合开发介绍和简单的登入案例实现
Ssh整合开发介绍和简单的登入案例实现 Ssh整合开发介绍和简单的登入案例实现 一 介绍: Ssh是strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8整合的开 ...
- SSH整合总结(xml与注解)
本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入ja ...
- Java - 框架之 SSH 整合
代码获取 十四. ssh 整合1 - 包 1. Struts jar 包 - Struts-2.xx\apps\stutrs2-blank\WEB-INF ...
- 三 Spring对于延迟加载问题的解决
Spring提供了延迟加载问题的解决方法 什么是延迟加载? 延迟加载:lazy(懒加载) 执行到该行代码的时候不会发送语句,真正使用这个对象的属性的时候才会发送sql语句进行查询. 类级别延迟加载:指 ...
- 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】
一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...
- Spring第四天——SSH整合
(从整合开始,使用回归使用eclipse) 一.三大框架版本: struts2 hibernate5 spring4 二.SSH三大框架回顾: Hibernate: ORM思想 核心配置文件: 单独 ...
- SSH整合简单例子
说明:简单SSH整合,struts版本2.3.32,spring版本3.2.9,hibernate版本3.6.10 一.开发步骤 1 引jar包,创建用户library.使用的包和之前博文相同,可以参 ...
随机推荐
- 将一个表的数据导入到另一个表的sql
ALTER PROCEDURE [dbo].[usp_ea_Copy] ( @eaId int, @createdBy varchar(), @newEaId int output ) AS decl ...
- iOS开源项目周报0302
由OpenDigg 出品的iOS开源项目周报第十期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.TodayMin ...
- WinForm-SuspendLayout、ResumeLayout、PerformLayou——转载
WinForm-SuspendLayout.ResumeLayout.PerformLayou——转载 https://www.cnblogs.com/si-shaohua/archive/2011/ ...
- zsh: command not found: gulp
明明安装了gulp,但是为什么执行gulp命令却在控制台输出 zsh: command not found: gulp 可能因为gulp没有被全局安装 在控制台输入 which gulp 如果输出 g ...
- 撩课-Web大前端每天5道面试题-Day3
1. javascript的typeof返回哪些数据类型? 答案: undefined string boolean number symbol(ES6) Object Function 2. 列举3 ...
- oracle 多列数据相同,部分列数据不同合并不相同列数据
出现这样一种情况: 前面列数据一致,最后remark数据不同,将remark合并成 解决办法: 最后一列:结果详情: 使用到的语句为: select a,b,c,wm_concat(d) d,wm_c ...
- 使用javascript调用android代码
1.使用webview对象的addJavascriptInterface方法 2.addJavascriptInterface方法有两个参数,第一个参数就是我们一般会实现一个自己的类,类里面提供我们要 ...
- 理解webpack4.splitChunks之cacheGroups
cacheGroups其实是splitChunks里面最核心的配置,一开始我还认为cacheGroups是可有可无的,这是完全错误的,splitChunks就是根据cacheGroups去拆分模块的, ...
- 用CSS3/JS绘制自己想要的按钮
我认为按钮的绘制分以下三个步骤 第一步,绘制按钮的轮廓 选择合适的html标签,设置轮廓的CSS /* html代码 */ <a href="#" class="b ...
- drupal7 转化 public:// 为实际url
file_create_url('public://xxx.png'); // 得到URL drupal_realpath('public://xxx.png'); // 得到系统路径(磁盘路径,如D ...