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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值