【发布时间】:2017-12-11 03:41:58
【问题描述】:
我正在使用 spring-boot 1.5.4 和 hibernate-core 5.2.10。
我有一个控制器,它调用服务方法(让我们将其命名为 pService)以在某些逻辑之后保存实体
pService.save 是这样的:
@Transactional(readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public <S extends SomeEntity> S save(S entity) throws ServiceException {
//some logic
entity.setAttrX("original text");
S justSavedEntity = someEntityRepository.save(entity); // POINT 1
//we termporarily change one attribute just to get a legible representation calling another service method (lets name it eService)
eService.process(justSavedEntity);
return justSavedEntity; //POINT 2
}
eService.process 是这样的:
@Transactional(readOnly = false, propagation = Propagation.MANDATORY)
public void process(SomeEntity entity) {
//some logic
entity.setAttrX("someText");
//method ends here
}
因为在 POINT 1 处没有其他调用来保存其中的一个,所以我希望将文本“原始文本”保存在数据库中,但我要保存的文本是在 POINT 2 处更改的文本。
那么,为什么我保存的是“someText”而不是“原始文本”?
我可以在数据库日志中看到,在 POINT 1 之后运行 SQL INSERT 命令,当 pService 返回时,运行不需要的 SQL UPDATE 命令,将“原始文本”更改为“someText”导致错误。
为什么会发出这个“额外”的 UPDATE 命令?
提前致谢!
【问题讨论】:
标签: hibernate spring-boot java-8 spring-data-jpa spring-transactions