The user guide states the following (highlighting added by me):
A version or timestamp property can never be null for a detached instance. Hibernate detects any instance with a null version or timestamp as transient, regardless of other unsaved-value strategies that you specify. Declaring a nullable version or timestamp property is an easy way to avoid problems with transitive reattachment in Hibernate, especially useful if you use assigned identifiers or composite keys.
Shouldn’t this read “non-nullable”?
I’ll give you my scenario:
I want to add optimistic locking to an existing application with an existing database. I added a nullable int column to my tables and an Integer field with the Version annotation to my entities. Since I didn’t update the existing rows, all version columns are NULL.
Now when loading data, I run into the problem that Hibernate detects my instances as transient, as the documentation above states.
So wouldn’t it be better to add a not-nullable int column with the default value 0 to my existing tables and an int property to my entities? Or would it have to be a not-nullable int column in the database with an Integer property in my entities, so Hibernate can properly recognize new entities?
Thank you very much in advance!