2 littledingdang littleDingDang 于 2017.01.11 21:29 提问

Write operations are not allowed。。。

spring整合hibernate报错:
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.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at com.gy.dao.BooktwoDaoImpl.save(BooktwoDaoImpl.java:17)
at com.gy.service.BooktwoServiceImpl.save(BooktwoServiceImpl.java:28)
at com.gy.test.test1.main(test1.java:30)

applicationContext配置如下:
<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<!-- <context:property-placeholder location="classpath:jdbc.properties" 
    file-encoding="utf-8" ignore-unresolvable="true" /> -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>oracle.jdbc.OracleDriver</value>
    </property>
    <property name="url">
        <value>jdbc:oracle:thin:@localhost:1521:orcl
        </value>
    </property>
    <property name="username">
        <value>gaoya</value>
    </property>
    <property name="password">
        <value>gaoya</value>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

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

<bean id="BooktwoDao" class="com.gy.dao.BooktwoDaoImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

<bean id="BooktwoService" class="com.gy.service.BooktwoServiceImpl">
    <property name="booktwoDao" ref="BooktwoDao"></property>
</bean>

<!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<!-- 基于数据源的事务管理器 -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
    p:dataSource-ref="dataSource" /> -->
<!-- 事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- 事务属性定义 -->
    <tx:attributes>
    <!-- <tx:method name="save*"/>
    <tx:method name="update*"/>
    <tx:method name="delete*"/>
    <tx:method name="find*" read-only="true"/> -->
    <tx:method name="**" read-only="true" />  

        <tx:method name="**" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<!-- 配合<tx:advice>和<aop:advisor>完成了事务切面的定义 -->
<!-- 使用强大的切点表达式是语言轻松定义目标方法 -->
<aop:config proxy-target-class="true">
    <!-- 通过aop定义事务增强切面 -->
    <!-- 引用事务增强 -->
    <!-- <aop:advisor advice-ref="txAdvice"
        pointcut="execution(* com.gy.service..*.* (..))" /> -->
        <aop:advisor advice-ref="txAdvice"
        pointcut="execution(* com.gy.。*.* (..))" />
</aop:config>


找了好久了还是没解决,网上的方法都试了也不行,希望大家帮帮忙

1个回答

baij1230
baij1230   2017.01.12 21:01
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!