I have a save method on a @Controller that either saves an object and updates an audit table, or populates an error object and re-renders the edit view. Basically, if you try to make an invalid change to an object, it fills in the error details so you can fix your problem before the save is accepted.
Unfortunately, the object is being persisted to the database, even with invalid content, I believe because the @Transactional annotation causes the persistence action on method completion.
What is the clean/correct way to “abort” the @Transactional aspect of this method? Right now, I am detaching the entity, but that seems wrong.
@Transactional
@PostMapping("/save")
public ModelAndView save(@ModelAttribute(SELECTED_ENTRY) @Valid T entry, BindingResult bindingResult, Model model) {
errorCheck(entry, bindingResult);
if (bindingResult.hasErrors()) {
entityManager.detach(entry); // <-- This seems wrong, as I have to re-attach / re-load the entry each time
return new ModelAndView(getEditView(), model.asMap()); // <-- Save is happening here because of @Transactional
}
repository.save(entry);
updateAudit(entry);
return new ModelAndView(REDIRECT + getBasePath(), model.asMap());
}