1

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?

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?.

dev123
  • 477
  • 8
  • 20
  • Possible duplicate of [Spring - @Transactional - What happens in background?](https://stackoverflow.com/questions/1099025/spring-transactional-what-happens-in-background) – franiis Aug 14 '19 at 05:46
  • 1
    You already answered your own question: by annotating your method with Transactional and calling it from another Spring bean. What else do you want to know. What *is* the concrete implementation problem? – JB Nizet Aug 14 '19 at 06:18

1 Answers1

1

As @JB Nizet already states in his comment, this is the exact purpose of @Transactional, so it will all be in one transaction. But ensure that you call the method from another bean and not from another method in the same class! As you already indicated, transactional doesn't work then.

Julian
  • 438
  • 3
  • 13