How do I automatically update a value with the current date/time when a row is updated? Every time I hiberSess.update(myObject) I want the last updated date to be updated without having to remember to set it explicitly in the Java code like this:
Code:
myClass.setLastModifiedDate(new Date());
I tried declaring the column in MySQL like this:
Code:
`last_modified_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
That covers the case where I might update the record with a manual SQL query. But Hibernate must be saving the
previous last modified date because if it wrote nothing or null the database would fill the correct value.
Hibernate reverese-engineering maps this field for me as follows:
Code:
<property name="lastModifiedDate" type="timestamp">
<column name="last_modified_date" />
</property>
I don't need millisecond accuracy. What's driving this is a program that runs every night, purging records that were logically deleted a certain number of days ago. If the lastUpdatedDate isn't set at the same time as the isDeleted flag, they may be purged the same night they are logically deleted. I'm especially interested in a solution without using annotations, but this might be worth using annotations for.
Right now, I manually comb my code for errors before every release, searching for any place I might have forgotten to set the lastModifiedDate:
Code:
egrep --exclude-dir='.svn' -rIn -B 20 --color 'hiberSess.(update|saveOrUpdate)' *
Thanks in advance for any help. I really appreciate the brilliance, hard work, and generosity that has gone into making Hibernate what it is.