When editing a form, the user may sometimes not change the form and still click the submit button. In one of the controller methods below, will the save() method perform a query to the database and update the fields even if the user didn't change anything?
PostMapping("/edit_entry/{entryId}")
public String update_entry(
@PathVariable("entryId") Long entryId,
@RequestParam String title,
@RequestParam String text
) {
Entry entry = this.entryRepo.findById(entryId).get();
if (!entry.getTitle().equals(title))
entry.setTitle(title);
if (!entry.getText().equals(text))
entry.setText(text);
this.entryRepo.save(entry);
return "redirect:/entries";
}
And also, are the "if" statements necessary in this case?
entryRepo
extends JpaRepository or CrudRepository? – InnovateentryRepo
extends CrudRepository. – HessiteOptional
in a wrong way. Don't useget
(it will fail if not found), usemap
instead. – Overcheckmap
instead of `get'! – Hessite