Quick revision
The short answer
Changing an OrderLine updates only the order_line row, so the Order's version does not change and two concurrent edits of different lines are not detected. To make the order the unit of concurrency control, we lock it with LockModeType.OPTIMISTIC_FORCE_INCREMENT, through em.lock(order, ...), em.find with that mode, or @Lock on a repository query. Hibernate then increments the order's version at flush even though none of its own columns changed, so the second transaction fails its version check.
What to remember
In this answer, we saw that @Version only protects the row it is declared on, so concurrent edits of different OrderLines leave the Order's version unchanged and can break order-level rules. Locking the order with OPTIMISTIC_FORCE_INCREMENT, through em.find, em.lock or @Lock on a repository query, makes Hibernate increment the order's version at flush, so the second transaction fails its check. Applied to every write path, this makes the aggregate root the unit of concurrency control.
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.
Changing an OrderLine updates only the order_line row, so the Order's version does not change and two concurrent edits of different lines are not detected. To make the order the unit of concurrency control, we lock it with LockModeType.OPTIMISTIC_FORCE_INCREMENT, through em.lock(order, ...), em.find with that mode, or @Lock on a repository query. Hibernate then increments the order's version at flush even though none of its own columns changed, so the second transaction fails its version check.
Why this question is asked
In domain-driven design, an aggregate is a cluster of objects, such as an order and its lines, that must stay consistent as a whole, with one aggregate root, the order, as its entry point. Rules like the order total may not exceed the credit limit depend on all lines together, so two people editing two different lines at once can break them even though neither touched the same row. Interviewers ask this to see whether we know how versioning behaves across parent and child rows. In this answer, we will reproduce the problem, look at the force-increment lock and its variants, and see where to apply it.
1Why the Parent's Version Does Not Change
@Version protects the row of the entity it is declared on. When we change a field of an OrderLine, Hibernate updates the order_line row, and only the OrderLine's own version, if it has one, is checked and incremented. The orders row is untouched, so the Order's version stays the same.
Each transaction checked the credit limit against the lines it saw and found it fine. Neither saw the other's change, and nothing compared them. The invariant belongs to the order, but nothing told the database that both transactions changed the order.
2Forcing the Increment
LockModeType.OPTIMISTIC_FORCE_INCREMENT tells Hibernate to treat the entity as changed: at flush it increments the version with update orders set version = ? where id = ? and version = ?, even if no other column of the order changed. Now both transactions touch the orders row with a version check, so the second one to flush finds the version already moved on and fails.
6 messages in order. Play them, or step through them one at a time.
Both transactions now meet on the orders row.
The two transactions now conflict on the aggregate root even though they edited different lines. The second one rolls back, and if it retries with fresh data, it sees the total of 1200 and the credit rule rejects it.
3Three Ways to Apply It
We can request the mode when loading, as above, or lock an already loaded entity with em.lock(order, LockModeType.OPTIMISTIC_FORCE_INCREMENT). In Spring Data JPA, @Lock on a repository method applies the mode to the query.
A clear convention is to have one repository method, such as findForUpdate or loadAggregate, that every command changing the aggregate uses. Then no write path can forget the lock.
4Optimistic or Pessimistic Force Increment
There is also PESSIMISTIC_FORCE_INCREMENT. It takes a database row lock on the order immediately, with SELECT ... FOR UPDATE, and increments the version straight away. Other writers then wait instead of failing later. The optimistic variant holds no lock and only fails at flush, which suits user edits with rare conflicts; the pessimistic one suits short, frequent conflicts where waiting is cheaper than retrying. With the optimistic variant the increment happens at flush, so the conflict usually surfaces at commit.
5Making the Aggregate Root the Unit of Concurrency
The deeper point is a design rule: every change to anything inside the aggregate goes through the root, and every such change bumps the root's version. The order's version then means the version of the whole order, including its lines. That also makes it a good value for an ETag in a REST API, because a client holding version 5 of the order knows its view of all lines is current.
Hibernate's own behaviour around collections can help or confuse here. Adding or removing a line through a collection mapped on the order may bump the order's version, depending on the mapping and Hibernate version, but changing a field of an existing line never does. Rather than depend on those details, the explicit force increment states the rule in code.
6Follow-up questions
Why not just add @Version to OrderLine?
That protects each line against concurrent edits of the same line, which is still useful. It does not detect edits of two different lines, because each transaction checks only its own row, so the order-level rule can still be broken.
Does the force increment cost much?
One extra UPDATE of the parent row per transaction, which is cheap. The real cost is more conflicts: two edits of different lines that would have been fine now conflict, so the aggregate should be small enough that concurrent edits are rare.
Is the order's version incremented twice if I also change an order field?
No. If the order is dirty anyway, Hibernate increments its version once as part of the normal update; the forced increment only matters when the order itself has no changes.
7Conclusion
In this answer, we saw that @Version only protects the row it is declared on, so concurrent edits of different OrderLines leave the Order's version unchanged and can break order-level rules. Locking the order with OPTIMISTIC_FORCE_INCREMENT, through em.find, em.lock or @Lock on a repository query, makes Hibernate increment the order's version at flush, so the second transaction fails its check. Applied to every write path, this makes the aggregate root the unit of concurrency control.
Open with
By default, changing a child row only updates that child. If two transactions edit two different lines of order 42, both update order_line rows, the orders row is never touched, and the order's version stays at 5, so nothing detects that both changed the order.
Then, if they want more
That matters when there is a rule over the whole order, like a credit limit on the total. The fix is to lock the order with LockModeType.OPTIMISTIC_FORCE_INCREMENT. I can do it with em.find passing that lock mode, with em.lock on a loaded order, or in Spring Data with @Lock on a query method like findForUpdate.
Hibernate then issues update orders set version = 6 where id = 42 and version = 5 at flush, even though no order column changed. Both transactions now hit the orders row with a version check, the second one updates zero rows and gets ObjectOptimisticLockingFailureException, and on retry it sees the other change.
I would make every command on the aggregate load it through that one method, so the root's version really means the version of the whole aggregate, which also makes it a good ETag. If conflicts are frequent and short, PESSIMISTIC_FORCE_INCREMENT locks the row immediately so writers wait instead.
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 "How would you force a version increment on an Order when one of its OrderLines changes...": https://scrivio-interview-library.netlify.app/databases/locking-and-concurrency-control/f6ac2dc3c6/