marshalljian 2018-06-24 12:08 采纳率: 100%
浏览 783
已采纳

在Spring集成Hibernate后,进行save操作报错

在进行save操作时抛出Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.异常

在网上找了许久,全是千篇一律。基本上都是说在web.xml的过滤器加FlushMode,但是我这个只有Spring+Hibernate,哪来的web.xml。也有说加事务处理的,但是我加了事务,测试了无数遍,还是不行,请大神们帮忙下,谢谢,下面是我的xml配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Stock business object -->
<bean id="stockBo" class="com.yiibai.stock.bo.impl.StockBoImpl" >
    <property name="stockDao" ref="stockDao" />
</bean>

<bean id="stockDao" class="com.yiibai.stock.dao.impl.StockDaoImpl" >
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="save">PROPAGATION_SUPPORTS</prop>  
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="productBoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="stockDao" />
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value>
        </list>
    </property>
</bean>

下面是具体错误信息
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1093)
at org.springframework.orm.hibernate5.HibernateTemplate.lambda$update$13(HibernateTemplate.java:658)
at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:383)
at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:349)
at org.springframework.orm.hibernate5.HibernateTemplate.update(HibernateTemplate.java:657)
at org.springframework.orm.hibernate5.HibernateTemplate.update(HibernateTemplate.java:652)
at com.yiibai.stock.dao.impl.StockDaoImpl.update(StockDaoImpl.java:17)
at com.yiibai.stock.bo.impl.StockBoImpl.update(StockBoImpl.java:20)
at com.yiibai.common.App.main(App.java:28)

  • 写回答

4条回答 默认 最新

  • uniquewdl 2018-06-24 13:38
    关注

    dao上加上@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)
  • ¥20 QAxWidget上显示一个word文档后,如何直接在该QAxWidget上修改和保存word文档
  • ¥15 Simulink仿真报错,请问如何解决
  • ¥20 宝塔面板无法添加Node项目,一直处于正在添加脚本页面