3

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?

DanilWH
  • 33
  • 6
  • Does `entryRepo` extends JpaRepository or CrudRepository? – Ayush28 Sep 24 '20 at 11:42
  • `entryRepo` extends CrudRepository. – DanilWH Sep 24 '20 at 12:50
  • ifs, no. Update if nothings changes, jpa will prevent it and won't issue a statement (unless you have some dynamically calculated fields like last updated timestamp). Also you are using the `Optional` in a wrong way. Don't use `get` (it will fail if not found), use `map` instead. – M. Deinum Sep 24 '20 at 12:58
  • @M. Deinum Thank you! I'm definitely going to consider the usage of `map` instead of `get'! – DanilWH Sep 24 '20 at 14:27

1 Answers1

4

What exactly happens during a call to save(…) depends on the underling persistence technology. Fundamentally there a re two categories of implementations:

  1. Implementations that actively manage entities. Examples of this are JPA and Neo4j. Those implementations keep track of the entities returned from the store and thus are able to detect changes in the first place. You pay for this with additional complexity as the entities are usually instrumented in some way and the change detection of course also takes time even if it ends up not detecting any changes. On the upside though the only trigger updates if needed.

  2. Implementations that do not actively manage entities. Examples are JDBC and MongoDB. Those implementations do not keep track of entities loaded from the data store and thus do not instrument them. That also means that there is no way of detecting changes as all the implementation sees is an entity instance without any further context.

In your concrete example, a MongoDB implementation would still issue an update while JPA will not issue an update at all if the request params do not contain differing values.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • So, as I see it, talking about the 1'st category of implemenations, the "if" statements won't make the process somehow faster because the line`this.entryRepo.save(entry);` will be executed anyway... – DanilWH Sep 26 '20 at 06:41
  • But if I add a boolean variable like `boolean changed = false;` right above the series of the "if" statements so that the variable will be turned into `true` in the "if" block where a field was changed, then I wrap `this.entryRepo.save(entry);` in a "if" block that will be executed if the `changed` variable keeps the `true` value (that is some changes were noticed) will it be faster to detect the changes or the presence of those"if" statements is just useless? – DanilWH Sep 26 '20 at 06:45