关于Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)

来源:互联网 发布:调色软件vsco下载 编辑:程序博客网 时间:2024/04/29 08:13

我在使用hibernateTemplate的时候遇到这个问题,此问题是由于session被设置成了FlushMode.NEVER/MANUAL模式,是不支持写操作的。我在网上搜了很长时间,大概就是两种方法,没有解决我的问题,如下:

第一种:

web.xml配置里添加
<filter>
   <filter-name>OpenSessionInViewFilter</filter-name>
   <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
   </filter-class>
   <init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value>
   </init-param>
   <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>           
        </init-param>
        <init-param>
        <param-name> flushMode </param-name>
   <param-value>AUTO </param-value>        
        </init-param>
</filter>


第二种是:

如果在交给spring 管理的情况下,在beans.xml 里的配置

 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>


 <aop:config>
  <aop:pointcut id="bussinessService"
   expression="execution(* com.fan.service.base.*.*(..))" />
  <aop:advisor pointcut-ref="bussinessService"
   advice-ref="txAdvice" />
 </aop:config>


 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED"/>
   <tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED"/>
   <tx:method name="save*" propagation="REQUIRED"/> // 如果不把save update delete都配置上,
   <tx:method name="update*" propagation="REQUIRED"/> //这些操作会无效
   <tx:method name="delete*" propagation="REQUIRED"/>
  </tx:attributes>
 </tx:advice>



我最后在Stack Overflow找到了第三种方法,解决了我的问题:


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>

在这里多添加一行<property name="checkWriteOperations" value="false"></property>即可

0 0
原创粉丝点击