Quick revision
The short answer
We expose the entity's @Version as an ETag header on GET, require the client to send it back in If-Match on PUT, and compare it with the current version before applying the change, returning 412 Precondition Failed when they differ. The comparison must use the version the client saw, not the one we just loaded, so we either check it explicitly or merge a detached object that carries the client's version. Loading the entity again and copying fields without the client's version silently defeats optimistic locking.
What to remember
In this answer, we saw that loading an entity in the PUT request and copying fields onto it checks the wrong version, so changes made while the user was editing are overwritten. Sending @Version as an ETag, requiring If-Match on PUT, and comparing it with the current version, or merging a detached object that carries it, fixes that, while @Version still guards the final moment. Conflicts return 412 or 409 with the current state, so the client can merge and retry.
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.
We expose the entity's @Version as an ETag header on GET, require the client to send it back in If-Match on PUT, and compare it with the current version before applying the change, returning 412 Precondition Failed when they differ. The comparison must use the version the client saw, not the one we just loaded, so we either check it explicitly or merge a detached object that carries the client's version. Loading the entity again and copying fields without the client's version silently defeats optimistic locking.
Why this question is asked
Optimistic locking inside one transaction is easy; the hard case is a user who reads in one HTTP request and writes in another, five minutes later. Between those requests no transaction is open, so the protection has to travel with the client. HTTP already has a standard mechanism for this, conditional requests, and interviewers ask the question to see whether we can join it correctly to JPA versioning. In this answer, we will see the bug in the usual implementation, add ETag and If-Match, and handle the conflict response.
1The Usual Implementation and Its Bug
A typical PUT handler loads the product, copies the fields from the request onto it, and saves. The entity has @Version, so it looks protected.
The version check does run, but against version 8, which we loaded a millisecond ago in this same request. It only protects against a change in that millisecond. The version that matters is 7, the one user A saw on screen at 10:00, and it was never sent to the server.
2Sending the Version to the Client as an ETag
An ETag, entity tag, is an HTTP response header that identifies a version of a resource. Clients and proxies treat it as opaque and send it back in later requests. Our @Version is a perfect source for it.
The value is quoted because the HTTP specification defines an entity tag as a quoted string. The browser or client keeps it alongside the data it shows the user.
3Requiring If-Match and Comparing Versions
On PUT, the client sends the tag back in the If-Match header, which means apply this change only if the resource is still at this version. We parse it and compare it with the current version before changing anything. A missing header is rejected too, with 428 Precondition Required, so a client cannot skip the check by leaving it out.
Notice that the two checks work together. Our explicit comparison catches everything that happened between the user's GET and this PUT. The @Version check in the UPDATE statement still catches a change that commits between our findById() and our commit.
4Why Not Just Set the Version on the Entity?
A common suggestion is to call p.setVersion(clientVersion) on the loaded entity and let Hibernate's check use it. Applications are not supposed to change a version attribute themselves, and for a managed entity Hibernate bases its check on the version it loaded, so this does not reliably work. It does work for a detached object: if we build a Product from the request, including the client's version, and pass it to merge() (which save() does for an existing id), Hibernate compares the detached version with the database and fails if they differ. Both that and the explicit comparison are correct; the explicit comparison is easier to read and lets us return a precise response.
7 messages in order. Play them, or step through them one at a time.
User A's stale edit is refused instead of overwriting B's.
5Responding to a Conflict
A good conflict response helps the client recover. We return 412 Precondition Failed, the standard status for a failed If-Match, with the current representation and its ETag in the body or a link to it. Some APIs return 409 Conflict instead, which is also reasonable as long as the API is consistent. The client can then show the user what changed, let them merge their edit, and retry with the new ETag. We also map ObjectOptimisticLockingFailureException from the final race to the same response in a @ControllerAdvice handler, so both checks look identical to the client.
6Follow-up questions
Can Spring do the ETag check automatically?
ShallowEtagHeaderFilter computes ETags from the response body, which saves bandwidth on GET but is no help for PUT. Spring's ServletWebRequest.checkNotModified() handles conditional GET with an ETag we supply. For updates, the explicit comparison shown here is still the usual approach; check your version for newer helpers.
What about PATCH?
The same rule applies. PATCH changes only some fields, so conflicts are rarer, but without If-Match a PATCH can still be based on an old view, for example lowering a price that someone else already lowered. Requiring If-Match keeps the behaviour consistent.
Should the ETag be the raw version number?
It can be, since it reveals nothing sensitive. Some teams hash the id and version together, or use a weak ETag, but a quoted version number is simple and works with every client.
7Conclusion
In this answer, we saw that loading an entity in the PUT request and copying fields onto it checks the wrong version, so changes made while the user was editing are overwritten. Sending @Version as an ETag, requiring If-Match on PUT, and comparing it with the current version, or merging a detached object that carries it, fixes that, while @Version still guards the final moment. Conflicts return 412 or 409 with the current state, so the client can merge and retry.
Open with
The issue is that the five minutes between the GET and the PUT are outside any transaction, so the version the user saw has to travel with the client.
Then, if they want more
On GET I return the entity's @Version as an ETag, the number 7 in quotes for example. The client sends it back on PUT in If-Match, and I require it, returning 428 if it is missing.
In the service I load the product and compare its current version with the client's; if they differ, someone changed it while the user was editing, and I return 412 Precondition Failed with the current state and its ETag so the client can show the difference and let the user merge.
If they match, I apply the change, and @Version still protects the small window between my read and my commit; I map that exception to the same 412.
The common mistake is to load the entity again and copy fields onto it: the check then uses the version just loaded, not the one the user saw, so it overwrites the other change. Setting the version field on a managed entity is not reliable either.
The alternatives that work are the explicit comparison, or merging a detached object that carries the client's version, which Hibernate checks against the database.
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 "In a REST API, a user loads a product, edits it for five minutes and saves it with PUT....": https://scrivio-interview-library.netlify.app/databases/locking-and-concurrency-control/6890b68152/