Its been suggested that a object Id should not be sent to the view in a hidden Id tag because a malicious user could edit the source html and submit (post) the data back to the wrong record, and that a better way is to store the object in a session var until it returns.
I have read this SO answer but it didn't help me in terms of this question.
I have a DTO OccurencePerson which stores details of the occurence and a collection of people. The form controller is setup like this:
CLASS
@Component
@Controller
@SessionAttributes(value = { "user", "occModelView" })
public class FormController {
GET HANDLER
@GetMapping("/occurence/{occeno}")
public String findOcc(@PathVariable String occno, @ModelAttribute("occViewModel") OccViewModel occViewModel, Model model, HttpSession session, SessionStatus sessionStatus) {
Occurence occ = occurenceRepository.findByoccno(occno);
occViewModel.setOccurence(occ);
occViewModel.setPersons(occPersonRepository.findOccPersonByEpisode(occurence.getId()));
model.addAttribute("occViewModel", occViewModel);
session.setAttribute("occViewModel", occViewModel);
sessionStatus.equals(occViewModel);
return "occurence";
POST HANDLER
@PostMapping("newOccurence")
public String episodeSubmit(@Valid @ModelAttribute OccViewModel occViewModel, BindingResult result) {
if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
for(ObjectError error : errors) {
}
return "occurence";
} else {
occService.saveNewOccurence(occViewModel.getOccurence(), occViewModel.getPersons());
return "redirect:/dash";
How would you use session vars to return a object from Hibernate and then return that object to Hibernate such that it doesn't create a new object?
As it stands at the moment if I remove the hidden tags in the html which store the occurrence id, and persons id and submit the data back it creates a new occurrence/persons.