spring+hibernate中的事务

上下文:

     从数据库服务器上获取数据可以,保存的时候增加了事务提交,即em.flush方法,报错no transaction in progress 

报错信息:

   no transaction in progress 

解决办法:

    看不到你的配置文件,不知道是否和我以前遇到的问题类似: 

   在主容器中(applicationContext.xml),将Controller的注解排除掉 
  <context:component-scan base-package="com"> 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  </context:component-scan> 

  而在springMVC配置文件中将Service注解给去掉 
  <context:component-scan base-package="com"> 
       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
  </context:component-scan> 

     因为spring的context是父子容器,所以会产生冲突,Controller会先进行扫描装配,而此时的Service还没有进行事务的增强处理,得到的将是原样的Service(没有经过事务 加强处理,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描配置进行事务处理。

调整bean生成的配置: 
    1、实体和服务层和common层的bean,由ApplicationContext负责扫描; 
    2、控制器,分别由前台、后台的servlet负责扫描; 

原文地址:https://www.cnblogs.com/lodor/p/6688839.html