Hibernate 捕获异常、更改实体、提交并重新显示异常
我有以下情况:Hibernate 捕获异常、更改实体、提交并重新显示异常,hibernate,spring-boot,spring-data-jpa,Hibernate,Spring Boot,Spring Data Jpa,我有以下情况: MyStoredEntity myStoredEntity = myStoredEntityService.get(id) try { myStoredEntityService.doSomething() } catch (GeneralException) { myStoredEntity.setFail(true) throw e; } 所有这些代码都在@Transactionalpropagation=Propagation.REQUIRE
MyStoredEntity myStoredEntity = myStoredEntityService.get(id)
try {
myStoredEntityService.doSomething()
} catch (GeneralException) {
myStoredEntity.setFail(true)
throw e;
}
所有这些代码都在@Transactionalpropagation=Propagation.REQUIRES\u NEW中。基本上,我想调用doSomething,如果抛出异常,则设置实体的字段,提交并重新抛出异常。但是,它不起作用,因为事务被标记为回滚。对于运行时异常,您可以使用@Transactional annotation的NOROLLBAFF参数来捕获您想要捕获的而不进行事务回滚的异常。大概是这样的:
@Transactional(noRollbackFor = {SomeServiceRuntimeException.class})
public foo() {
MyStoredEntity myStoredEntity = myStoredEntityService.get(id);
try {
myStoredEntityService.doSomething();
} catch (SomeServiceRuntimeException e) {
myStoredEntity.setFail(true);
myStoredEntityService.save(myStoredEntity);
throw e;
}
}
你能把你的全部密码都贴出来吗?我想知道你在哪里调用这个方法。