我想为每个REST查询创建一个事务,REST查询由Jackson 2动态解析的JSON数据组成。在从JSON数据“解析”对象实例化期间,我通过Spring Repository对数据库进行一些输入。当一些数据库约束不被尊重时,一切都正常工作@Transactional不捕获异常并且不回滚。我怎么能解决这个问题,上面你找到了代码:
UploadData类包含一些实体类,以便解析JSON数据,并将数据保存在数据库中。
Controller.java
@Transactional(rollbackFor=Exception.class)
@RequestMapping(value = "/uploaddata", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
public Reponse uploaddata(@RequestBody UploadData post) throws JsonProcessingException {
if (messages != null) {
return new Reponse(-1, messages);
}
try {
return new Reponse(0, post);
} catch (Exception e) {
return new Reponse(1, Static.getErreursForException(e));
}
}
AppConfig.java
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.test" })
@EnableJpaRepositories(basePackages = { "com.test.repositories" })
@EntityScan(basePackages = { "com.test.entities" })
@EnableTransactionManagement
public class AppConfig {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}
UploadData.java
public class UploadData {
@JsonProperty("ADDEDDATA")
private AddedData ADDEDDATA;
}