这个错我试了很多:
方法一:spring配置文件中加上
<!-- 配置事务通知
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes > -->
<!-- 事务管理策略,查为只读 -->
<!-- 事务隔离级别:mysql为可重复读,事务传播行为:默认值required -->isolation="REPEATABLE_READ"
<!-- <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>-->
<!-- 配置事务AOP
<aop:config>
<aop:pointcut expression="execution(* kim.service.impl.*.*(..))" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>-->
但是我的没有任何用处。无论我这么改这个部分
<tx:attributes > -->
<!-- 事务管理策略,查为只读 -->
<!-- 事务隔离级别:mysql为可重复读,事务传播行为:默认值required -->isolation="REPEATABLE_READ"
<tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" />
<tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
然后尝试方法二:
先配置spring:
<!-- 开启事务,记得在需要开启事务的类上 使用@Transaction-->
<tx:annotation-driven transaction-manager="transactionManager"/>
在service层上面加上一个@Transactional,
dao层中加一行getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
public void save(Customer customer) {
getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
this.getHibernateTemplate().save(customer);
}
然后就成功了,不过后面update又出问题
解决如下链接