I have two hibernate entities: Order and its Items (one-to-many with save-update cascade, nothing special).
Two users initiate an update of the same Order by adding a new Item at the same time which triggers Session.saveOrUpdate(order) operation in two concurrent threads.
Both Order and Item have @Version column to support optimistic lock so this concurrent edit fails with OptimisticLockException as it supposed to be.
Then I want to increase database consistency and add an unique constraint to the Item (on one of its columns).
Repeating the case above I get constraint violation instead of OptimisticLockException!
Looks like hibernate does this:
- INSERT new item into ITEM table (constraint voilation!)
- check optimistic lock
- UPDATE order table
Is it possible to force Hibernate to check the optimistic lock BEFORE inserting the child items?