-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How can I automatically update a last-modified-date?
PostPosted: Wed Oct 14, 2009 10:02 am 
Beginner
Beginner

Joined: Mon Jul 28, 2008 4:36 pm
Posts: 24
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.