Paul McKenzie
Paul McKenzie

Reputation: 20096

null Hibernate @Version, why no StaleObjectStateException?

We have an entity class that uses @Version.

@Version
protected Long auditVersion;

What if there are two threads both trying to insert the first occurrence of the entity? Both instances of the entity will have auditVersion set to null. It seems that Hibernate does not take any notice of the optimistic locking fail, I do not get the StaleObjectStateException I expected.

Why not?

Upvotes: 2

Views: 809

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

Because optimistic locking is used to handle concurrent updates and removals. If two threads try to insert two entities with the same ID, the unique constraint on the primary key is sufficient to detect the conflict. No need for optimistic locking for this.

Upvotes: 4

Related Questions