christian wrote:
You can also use a default value in your SQL DDL when creating your table and set the property to update="false", insert="false".
Please correct me if I understand this incorrectly, but won't using the update="false" approach cause the default value to be used only on an INSERT into the table? I am also trying to do something similar to what the first poster is asking to do.
I have a table that has a timestamp field that defaults to the current timestamp (current_timestamp in DB2/400). By setting insert="false" and update="false", an inserted record correctly defaults to the current timestamp, because Hibernate (2.1.2) correctly omits the timestamp field from the generated SQL INSERT statement. However, omitting the field in updates will leave the timestamp field unchanged. This can be overriden in SQL by using "default" in the values list, just like Hibernate uses for the primary key field for the IDENTITY field in my table on inserts. Is there a way to configure Hibernate to always use "default" on inserts/updates, and read the timestamp normally on reads?
The reason I am shying away from using the <timestamp> configuration is that I would like the timestamp to be set by the database, not the application server. For optimistic locking, I cannot guarantee that each node in the clustered application environment has the exact same system time, and there's a chance of wackiness in the locking if the system clocks on the nodes become significantly out of sync (or are geographically separated and not using UTC).
My plan B is to write a trigger to handle the timestamp updates, but I'd like to see what is possible from the Hibernate side of things.
TIA for any help.