Getting consecutive version numbers from Hibernate's @Version usage once per transaction

Posted by Cheradenine on Stack Overflow See other posts from Stack Overflow or by Cheradenine
Published on 2010-04-19T16:45:58Z Indexed on 2010/04/19 17:33 UTC
Read the original article Hit count: 377

Filed under:
|
|

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)..

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about version