-->
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.  [ 5 posts ] 
Author Message
 Post subject: Automagic updating of timestamps
PostPosted: Thu Dec 01, 2005 1:14 pm 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
I have an object that I am persisting to a database that has created and modified dates. Those two dates map to columns of MySQL type 'timestamp'.

I don't want to sound lazy, but I'm wondering if there is a way to have Hibernate update those dates for me rather than having to manually do it.

Using insert and update as indicators should make it fairly easy to know whether to update a timestamp or not.

Anyone have any comments or ideas about this?

Thanks!

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 1:32 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
CREATE TABLE MY_TABLE (MY_TIME TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 1:49 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
The above takes care of creation timestamp. For modified date, in Oracle we have a trigger:
Code:
CREATE OR REPLACE TRIGGER CATEGORY_R BEFORE UPDATE ON CATEGORY
  REFERENCING OLD AS OLD NEW AS NEW
  FOR EACH ROW
BEGIN
     :new.date_last_mod := SYSDATE ;
END CATEGORY_R ;

I saw a sample mysql trigger, looked similar altho syntax may need fixing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 1:57 pm 
Newbie

Joined: Thu Dec 01, 2005 1:02 pm
Posts: 5
Location: Nijmegen, Nederland (nl)
But won't Hibernate get confused if you use triggers and other database 'magic'? For autoincrement keys you need to specify a generator, is there something like that for this?

_________________
-Stijn

http://gx.nl
http://bergland-it.nl
http://dutch-politics.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 2:23 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
oh, that's probably true. How about doing it in the preSave() and preUpdate() methods? Not exactly automatic, but would eliminate having it in multiple places in application code.


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

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.