I have made it my mission to replace the crumbling awful hand-coded DAO apps at my current contract with a nice alternative using hibernate. I think this is the last stumbling block to prove that it can work. (most of the developers are converted PL/SQL programmers who like to have their meetings in SQL and would love SQL on Rails)
We have both a Last Update Date column, and a Created Date column on all tables. There is a trigger on insert that populates the current DB timestamp into the Created Date column, and another trigger on Update that populates the Last Update Date column.
In order to get the optimistic locking working correctly for existing records, I have used the Last Update Date column with the timestamp mapping:
Code:
<timestamp column="LAST_UPD_DATE" name="lastUpdate" generated="always" />
But, it's not "always" generated by the database - only on updates. So when I create a new transient object, the version is null, and the new object is persisted, but then the subsequent SELECT gets a null value for the version, and treats the object as unsaved, eventually producing a TransientObjectException. The data is saved but my object on the Java side can't be modified anymore because now the optimistic locking fails.
I read in the manual:
Quote:
A version or timestamp property should never be null for a detached instance, so Hibernate will detact any instance with a null version or timestamp as transient, no matter what other unsaved-value strategies are specified.
http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-versionSo that means I can't indicate the object is actually saved by another means.
Is there a strategy that would handle this? I know the best solution is to fix the trigger so the Last Update Date is not null for a record in the DB, but the DBA is the sort who greatly enjoys making developers crazy. It seems that if I could have insert="true" and generated="always", it would do the right thing, but it's not allowed:
Code:
org.hibernate.MappingException: cannot specify both insert="true" and generated="always" for property: lastUpdate
I have poked around the code some but I'm not sure where to look to find a way to do this.
Thanks!
-Alex
[ Hibernate 3.1.3, Oracle 10g database ]