Getting consecutive version numbers from Hibernate's @Version usage once per transaction
- by Cheradenine
We use Hibernate with the following version definition for optimistic locking et. al:
<version name="version" access="field" column="VERSION" type="long" unsaved-value="negative"/>
This is fine and dandy; however, there is one small problem, which is that the first version for some entities is '0', and for others, it is '1'.
Why this is happening, is that for some object graphs, an entity will be subject to both onSave and flushDirty - this is reasonable, such as if two object are circular dependencies. However, the version number gets incremented on both occasions, leading to the above '0' / '1' discrepancy.
I'd really like the version number only to ever increment once per transaction. However, I can't see a simple way to do this in the hibernate versioning implementation, without hacking about with an Interceptor (which was how I generated a column value for version before, but I wanted hibernate to do it itself)..