重写OpenSessionInView

  1. 解决Hibernate的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.问题 
  2. 一般这个错误是事务引起的,如果确定事务没有问题,还是有这个错,可以重写OpenSessionInViewFilter的2个方法
  3. //在myfaces的wiki里提供了OpenSessionInViewFilter的一个子类如下:
  4. public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
  5.       
  6.         /**
  7.          * we do a different flushmode than in the codebase
  8.          * here
  9.          */
  10.         protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
  11.                 Session session = SessionFactoryUtils.getSession(sessionFactory, true);
  12.                 session.setFlushMode(FlushMode.COMMIT);
  13.                 return session;
  14.         }
  15.         /**
  16.          * we do an explicit flush here just in case
  17.          * we do not have an automated flush
  18.          */
  19.         protected void closeSession(Session session, SessionFactory factory) {
  20.                 session.flush();
  21.                 super.closeSession(session, factory);
  22.         }
  23. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值