Will Spring Data's save() method update an entity in the database if there are no changes in the entity?
Asked Answered
H

1

6

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?

Hessite answered 24/9, 2020 at 11:28 Comment(4)
Does entryRepo extends JpaRepository or CrudRepository?Innovate
entryRepo extends CrudRepository.Hessite
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.Overcheck
@M. Deinum Thank you! I'm definitely going to consider the usage of map instead of `get'!Hessite
S
7

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.

Shiite answered 25/9, 2020 at 7:17 Comment(2)
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 linethis.entryRepo.save(entry); will be executed anyway...Hessite
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?Hessite

© 2022 - 2025 — McMap. All rights reserved.