非活动事务:javax.persistence.TransactionRequiredException:执行更新/删除查询

时间:2015-11-03 08:28:27

标签: java spring hibernate spring-data-jpa

我有同样的问题。我正在使用spring 4.2.2.RELEASE

我有一个基于Java的配置。

@Configuration 
@EnableTransactionManagement 
@ComponentScan(basePackages={"com.mypackage"}) 
public class InfrastructureConfig { 

   @Autowired Environment env;     
   @Autowired private DataSource dataSource; 


   @Bean 
   public JpaTransactionManager transactionManager() { 
       JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); 
       jpaTransactionManager.setEntityManagerFactory(entityManagerFactory()); 
       return jpaTransactionManager; 
   } 

   @Bean 
   public EntityManagerFactory entityManagerFactory() { 
      LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
      em.setDataSource(dataSource); 
      em.setPersistenceUnitName("javaconfigPU"); 
      em.setPackagesToScan("com.mypackage"); 
      em.setJpaVendorAdapter(jpaVendorAdaper()); 
      em.setJpaPropertyMap(additionalProperties()); 
      em.afterPropertiesSet(); 
      return em.getObject(); 
   } 

   public JpaVendorAdapter jpaVendorAdaper() { 
       HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
       ...  
       return vendorAdapter; 
   } 

   private Map<String, Object> additionalProperties() { 
       Map<String, Object> properties = new HashMap<String, Object>(); 
       ...
       return properties; 
   } 

 }

我称之为更新查询的服务

@Service 
public class MyService { 

    @Autowired private MyRepository myRepo; 
    @Transactional 
    public void myMethod(MyEntity myEntity){ 
        myRepo.save(myEntity); 
    } 
}

@transactional来自春天

当我不使用@transactional或者我没有使用属性或者把它放在属性中时,我有一个javax.persitence.TransactionRequiredException。

我的代码在其他应用程序中运行良好,在这个代码中,它在3周前运行良好。

0 个答案:

没有答案