`
xjg396
  • 浏览: 45468 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

OpenSessionInViewFilter的两个异常:Illegal attempt to associate a collection with two

阅读更多
最近被hibernate 的懒加载给整郁闷了.当页面调用时,报session 已关闭,后来在web.xml中加上openSessionInView. 问题解决了,但是随之而来又出现了一个问题,在根据ID删除一个对象时,报有2个session. 网上找了些资料,把在web.xml中配置的openSessionInView 移到spring的配置文件中.
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode" value="1" />
<property name="singleSession" value="false" />
</bean>
加了一行 <property name="flushMode" value="1" />. 问题解决了.
  但是又出现了之前的 hibernate 延迟加载的错误 failed to lazily initialize a collection of role . 就这样问题反反复复,最终还是有我遭遇相同的兄弟..

    使用 Spring 整合 Hibernate, 在懒加载的情况下, 有时候需要在 JSP/View 层显示数据, 这时候就要用到Spring内置的: OpenSessionInViewFilter, 一般来说配置如下(web.xml):

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<!-- 和 spring 中的sesssionfactory ID 一致 -->
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
<!-- *.jsp, *.do-->
</filter-mapping>

不过, 这时候又会导致更新数据时抛出如下异常:
Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ‘readOnly’ marker from transaction definition.
这时候再去网上找解决方案, 会有人说: 把参数 singleSession改为false, 就行了. 不过, 改完后, 估计不久就会遇到另一个郁闷的异常:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

这下完了, 两个方案都不行, 到底怎么办? 还好, 在http://xuliangyong.javaeye.com/blog/144818的主页上, 给了一个方案, 就是改写 OpenSessionInViewFilter 的代码, 非常感谢, 下面给出的就是最终方案:

web.xml

< filter-name >hibernateFilter</filter-name>

< filter-class > org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter </filter-class>

OurOpenSessionInViewFilter.java 代码:

package org.springframework.orm.hibernate3.support;

import org.hibernate.*;

/** * 单session模式下, 默认会发生无法提交的错误: * Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. * 需要设置FlushMode并刷新session. * 参考: http://xuliangyong.javaeye.com/blog/144818 * @author 刘长炯 */publicclass OurOpenSessionInViewFilter extends OpenSessionInViewFilter {

    public OurOpenSessionInViewFilter() {
        super.setFlushMode(FlushMode.AUTO);
    }

    protectedvoid closeSession(Session session, SessionFactory sessionFactory) {
        session.flush();

        try {
            session.getTransaction().commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block//e.printStackTrace();
        }

        super.closeSession(session, sessionFactory);
    }
}
如果各位有更好的解决方案, 欢迎讨论哦!!!

题外话:

感觉 Spring + Hibernate 的健壮性还是不够啊! 容易抛异常, 这是事实, 也许这是开源软件的通病吧.

转自: http://www.beansoft.biz/?p=1806
分享到:
评论

相关推荐

    OpenSessionInViewFilter

    OpenSessionInViewFilter个人学习总结

    关于OpenSessionInViewFilter的学习

    NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164

    SSH项目整合示例【OpenSessionInView】所用到的jar包

    SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar

    jar包(struts2.0.8+spring2.0+hibernate3.2)

    struts2.0.8+spring2.0+hibernate3.2 jar包

    Sping 事务管理.doc

    OpenSessionInViewFilter解决Web应用程序的问题

    OA项目SSH整合框架

    &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter &lt;filter-name&gt;OpenSessionInView *.do 2,LazyInitializationException异常说明 1,对于集合属性...

    SPRING API 2.0.CHM

    CollectionFactory CollectionUtils ColumnMapRowMapper CommAreaRecord CommonsAttributes CommonsDbcpNativeJdbcExtractor CommonsFileUploadSupport CommonsFileUploadSupport.MultipartParsingResult ...

    spring_demo:Spring MVC示范项目

    Spring MVC Hibernate Demo Hibernate 配置 数据库实体必须设置以下注解 @Entity ... &lt;filter&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter &lt;param&gt;flushMode&lt;/param-nam

    S2SH集成 案例

    该案例实现了一个简单的登录功能,但里面将S2SH集成的所有配置信息都添加进去了。 如,OpenSessionInViewFilter、声明式事务、三层等等

    struts+spring+hibernate整合

    Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...

    spring_note.rar_inversion_spring concept

    课程内容 面向接口(抽象)编程的概念与好处 IOC/DI的概念与好处 inversion of control dependency injection AOP的概念与好处 ...opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC

    Mac Mysql数据库中文乱码问题解决

    Mac 下MySQL数据库中文乱码解决方案: 当我们用框架进行数据库的存储操作时,经常会遇到中文乱码...-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --&gt; &lt;filter&gt;CharacterFilter &lt;filter&gt;org.sprin

Global site tag (gtag.js) - Google Analytics