Race condition with related optimistic locked entities?

I noticed that committing changes to a single optimistic_force_increment locked entity is performed with a single sql update statement that both updates the entity fields but also tests and set the version. This seems atomic and fine, no race condition here.

But, if I commit changes to two related entities, both locked with optimistic_force_increment, then hibernate performs multiple sql update statements. For each of the entities, the fields are updated separately from the version test and set update even though its the same entity/row. If the version test and set sql statement fails, then the change to the fields have already been written albeit with a similar test to the version. Some other transaction could change the version in between the 2 sql statements, leading to a race condition.

What triggers Hibernate to split updates to the same row into 2 sql update statements in this case, when it doesn’t happen with only a single entity in the transaction? Is it the presence of a join column in one of the entities?

That sounds strange to me. Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.