Quick revision
The short answer
By default Hibernate's UPDATE writes every column of the entity, so the later save writes back the stale value it loaded for the field it did not change, wiping out the other user's edit. @DynamicUpdate makes Hibernate write only the changed columns, so edits of different fields merge, but it does not detect two edits of the same field. @Version detects any concurrent change, even to different fields, which may reject edits that could have merged. PATCH-style field updates fit some UIs better, and the choice depends on whether concurrent edits of different fields should merge or conflict.
What to remember
In this answer, we saw that one change is lost because Hibernate by default writes all columns, so the later save restores the stale value of the field it did not change. @DynamicUpdate writes only changed columns, letting different-field edits merge but not detecting same-field conflicts. @Version detects every concurrent change but also rejects edits that could have merged, and PATCH with field-level checks gives both merging and detection. The right choice depends on whether the record's fields are independent or must be treated as a whole.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
By default Hibernate's UPDATE writes every column of the entity, so the later save writes back the stale value it loaded for the field it did not change, wiping out the other user's edit. @DynamicUpdate makes Hibernate write only the changed columns, so edits of different fields merge, but it does not detect two edits of the same field. @Version detects any concurrent change, even to different fields, which may reject edits that could have merged. PATCH-style field updates fit some UIs better, and the choice depends on whether concurrent edits of different fields should merge or conflict.
Background to this question
This scenario looks like a contradiction at first: two users changed different fields, so how can one change be lost? The answer lies in what Hibernate actually sends to the database, and it leads to a real design decision about how an application should treat concurrent edits. In this answer, we will reproduce the lost change, see the SQL that causes it, then compare @DynamicUpdate, @Version and PATCH semantics, and finish with how to choose.
1Reproducing the Lost Change
Customer 5 has email old@example.com and phone 111. User A loads the customer to change the email; at the same moment user B loads it to change the phone. Both save.
Notice the second statement. B never touched the email, yet its UPDATE sets email to old@example.com, the value B loaded before A committed. Hibernate, by default, generates one UPDATE statement per entity type that lists every column and reuses it, so whatever field changed, all fields are written from the entity's current state in memory. B's in-memory email was stale, and it went to the database.
2Option 1: @DynamicUpdate Writes Only Changed Columns
Hibernate's @DynamicUpdate annotation tells it to build each UPDATE at flush time, listing only the columns whose values differ from what was loaded.
Now the edits merge naturally, because each statement only touches its own column. The costs are small: the statement cannot be cached and reused, and Hibernate must compare columns at flush, which matters only for very wide entities updated very often. The limit is important, though. If both users edit the email, the later one still silently wins, because nothing compares what they saw. @DynamicUpdate reduces conflicts between different fields; it does not detect conflicts.
3Option 2: @Version Detects Every Concurrent Change
With a @Version column, every UPDATE includes the version that was read, and the second writer fails.
| Option | Different fields | Same field edited twice |
|---|---|---|
| Default (all columns) | One change silently lost | Later write silently wins |
@DynamicUpdate | Both changes kept | Later write silently wins |
@Version | Second save rejected | Second save rejected |
| PATCH with field-level checks | Both changes kept | Conflict detected per field |
Nothing is lost silently now, including two edits of the same field. But B's change of a completely different field is also rejected, which is an unnecessary conflict from the user's point of view: B has to reload and type the phone again. For a busy record edited by many people, these false conflicts can annoy users. Combining @Version with @DynamicUpdate does not change this, since the version check still fails.
4Option 3: PATCH Semantics and Field-Level Updates
Some interfaces are better served by sending only what the user changed. A PATCH request carries just the changed fields, and the server applies only those. To also catch same-field conflicts, the request can carry the old value of each field it changes, and the server applies the change only if the field still has that old value.
This gives the best of both: different fields merge, and the same field is protected. It costs more design work in the API and the client, and it suits forms where users edit a few fields of a large record, such as a CRM contact screen.
5How to Choose
The deciding question is a business one: when two people edit different fields of the same record at once, should their changes merge or should the second person be asked to review? For a customer profile, where email and phone are independent facts, merging is usually right, so @DynamicUpdate, or PATCH with field checks for important fields, fits. For a contract or an order, where fields depend on each other, such as a price and a discount, a change to one may invalidate the other, so the record should be treated as a whole and @Version is right. Many systems use both styles for different entities, and that is fine as long as the choice is deliberate.
6Follow-up questions
Does @DynamicUpdate change performance noticeably?
Rarely. It skips statement reuse for that entity and adds a column comparison at flush. For most entities the difference is negligible, and writing fewer columns can even reduce write volume and index updates.
Can @Version exclude some fields?
Hibernate's @OptimisticLock(excluded = true) on a field or collection stops changes to it from incrementing the version. That helps, for example, for a last-viewed timestamp, but it does not make different-field edits merge in general.
Is the default behaviour a Hibernate bug?
No. Writing every column with one prepared statement is a performance choice that is correct for a single writer. The lost change only appears with concurrent writers and no concurrency control, which is the real gap.
7Conclusion
In this answer, we saw that one change is lost because Hibernate by default writes all columns, so the later save restores the stale value of the field it did not change. @DynamicUpdate writes only changed columns, letting different-field edits merge but not detecting same-field conflicts. @Version detects every concurrent change but also rejects edits that could have merged, and PATCH with field-level checks gives both merging and detection. The right choice depends on whether the record's fields are independent or must be treated as a whole.
Open with
The change is lost because of the SQL Hibernate sends. By default it uses one UPDATE statement per entity that writes every column from the entity in memory.
Then, if they want more
User B loaded the customer before user A committed the new email, so B's entity still holds the old email, and when B saves the phone, the update also sets email back to the old value. A's change disappears with no error.
There are a few options. @DynamicUpdate makes Hibernate write only the columns that changed, so A's update sets only email and B's only phone, and both survive.
But it does not detect two edits of the same field; the later one still wins silently. @Version detects any concurrent change, so B gets an optimistic lock failure and must reload, which is safe but creates conflicts for edits that could have merged.
A third option is PATCH semantics: the client sends only changed fields, ideally with their old values, and the server updates each field only if it still has that old value, which merges different fields and catches same-field conflicts.
I would choose based on the business meaning: for a profile with independent fields I would let them merge, and for something like a contract where fields depend on each other I would use @Version.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Two users edit different fields of the same customer at the same time (one the email...": https://scrivio-interview-library.netlify.app/databases/locking-and-concurrency-control/1d0e30af79/