In a spring boot application, in a save method.
i created many entity
Pub, Depot, Owner
I try to save some entities
@Transactional
public void saveDepot(DepotPub depotPub) {
Owner own = ownerRepository.findById(depotPub.getOwnerId());
List<Recipient> recipients = own.getRecipients();
Depot dep = new Depot();
...
RecipientUtils.update(recipients, depotPub.getRecipientsRecord();
...
ownerRepository.save(own);
}
public RecipientUtils{
//can add, remove or delete a recipient
public static void update(List<Recipient> recipients, List<RecipientRecord> recipientRecords){
...
}
}
I think because update method RecipientUtils is static is imposible to put Transactional annotation on it.
I don’t know if because we modify entity in this method, if jpa decide to save data and an error happen if the operation in saveDepot will be rollbacked or this scenario can’t happen in update method or that would be better to create a service to update recipient and put transactional annotation on it?