[英]Save, delete and update within a single transaction in Spring Data JPA
I have a following method: 我有以下方法:
@Transactional
public void saveUpdateAndDelete(Entity newEntity, Entity updatedEntity, long deleteEntityId) {
entityRepository.save(newEntity);
entityRepository.findById(updatedEntity.getId())
.ifPresent(e -> e.setName(updatedEntity.getName));
entityRepository.deleteById(deleteEntityId);
}
How can I assure that all statements in saveUpdateAndDelete
method will be executed within a single transaction? 如何确保
saveUpdateAndDelete
方法中的所有语句将在单个事务中执行?
Edit: the question purpose is to solve some implementation problem, not about how @Transactional
is handled by Spring by creating proxy classes, which is explained here: Spring - @Transactional - What happens in background? 编辑:问题的目的是解决一些实现问题,而不是通过创建代理类来解决Spring如何处理
@Transactional
问题,在此进行解释: Spring-@Transactional-后台会发生什么? . 。
As @JB Nizet already states in his comment, this is the exact purpose of @Transactional
, so it will all be in one transaction. 正如@JB Nizet在其评论中已经指出的那样,这是
@Transactional
的确切目的,因此将全部处理在一个事务中。 But ensure that you call the method from another bean and not from another method in the same class! 但是,请确保从另一个bean而不是从同一类中的另一个方法调用该方法! As you already indicated, transactional doesn't work then.
正如您已经指出的,那时事务性不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.