我有同样的问题。我正在使用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周前运行良好。