hibernate+spring进行增删改的只读题,求各位大神帮我看看配置文件和代码哪里不对,求教育,求指导!!!
我使用hibernate+spring实现对数据的增删改查,但是除了查询都能成功,其他都提示Writeoperationsarenotallowedinread-only...
我使用hibernate+spring实现对数据的增删改查,但是除了查询都能成功,其他都提示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.下面是我的applicationContext.xml的配置:
application.xml:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/niuniu"/>
<property name="username" value="root"/>
<property name="password" value="kiban410."/>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/lyq/service/mysql/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialet.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<!-- hibernate封装的模版类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<!-- 事务的属性 -->
<tx:attributes>
<!-- 事务的执行方法 -->
<tx:method name="save*" rollback-for="Exception" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:pointcut expression="execution(com.lyq.Dao.UserDao(..))" id="mycut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config>
<!-- 注入sessionFactory -->
<bean id="userDao" class="com.lyq.Dao.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans>
我已经在web.xml中加入<filter>
<filter-name>OpenSessionInViewFilter</filter-name> 的配置了
也没有用 展开
application.xml:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/niuniu"/>
<property name="username" value="root"/>
<property name="password" value="kiban410."/>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/lyq/service/mysql/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialet.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<!-- hibernate封装的模版类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<!-- 事务的属性 -->
<tx:attributes>
<!-- 事务的执行方法 -->
<tx:method name="save*" rollback-for="Exception" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:pointcut expression="execution(com.lyq.Dao.UserDao(..))" id="mycut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config>
<!-- 注入sessionFactory -->
<bean id="userDao" class="com.lyq.Dao.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans>
我已经在web.xml中加入<filter>
<filter-name>OpenSessionInViewFilter</filter-name> 的配置了
也没有用 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询