-->
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.  [ 9 posts ] 
Author Message
 Post subject: setLastUpdateDate(), setCreateDate()
PostPosted: Wed Aug 10, 2005 7:49 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 10:23 pm
Posts: 22
All of our tables have lastupdatetime and createtime columns. I need to set these attributes on the objects to be saved/updated at the latest possible time right before the objects are persisted - what options do I have to do this with hibernate? The Interceptor onSave()/onFlushDirty() methods seems one way - I tried it, but could not get it to work.

Thanks in advance for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 8:18 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 9:58 pm
Posts: 25
Location: Valparaiso
You can add two fields of Calendar (or Date time) to your class, one for lastupdatetime and another for createtime, and initialize this fields when you create your instances. The Calendar type is "native" for hibernate. Simple and easy!

Good luck

_________________
Daniel Casanueva R.

Jcode
Valparaiso, Chile


Top
 Profile  
 
 Post subject: Re: setLastUpdateDate(), setCreateDate()
PostPosted: Wed Aug 10, 2005 8:37 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
wiphillyfan wrote:
All of our tables have lastupdatetime and createtime columns. I need to set these attributes on the objects to be saved/updated at the latest possible time right before the objects are persisted - what options do I have to do this with hibernate? The Interceptor onSave()/onFlushDirty() methods seems one way - I tried it, but could not get it to work.

Thanks in advance for your help.


I would also suggest you look into Optimistic Locking options for lastupdatetime and let Hibernate handle this field for you using the <timestamp> mapping.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: setLastUpdateDate(), setCreateDate()
PostPosted: Wed Aug 10, 2005 9:19 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 10:23 pm
Posts: 22
Thanks for the reply. Let me clarify, I already do have these attributes on the classes to be persisted and I am temporarily setting these values
as Calendar when I create instances of these classes on the client layer.

But, I want to set these values later on the server side so that they are more accurate as to when they are actually persisted.

I thought I could do this via the Interceptor interface by setting the values in the onSave() and onFlushDirty() methods, but that is not working - maybe I'm not doing it right. I have something like:

onSave()
{
if( entity instanceof Auditable )
{
((Auditable)entity).setCreateDate(Calendar.getInstance());
}

return false;
}

Is this the wrong way of doing this?


Top
 Profile  
 
 Post subject: Re: setLastUpdateDate(), setCreateDate()
PostPosted: Wed Aug 10, 2005 9:25 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
wiphillyfan wrote:
Thanks for the reply. Let me clarify, I already do have these attributes on the classes to be persisted and I am temporarily setting these values
as Calendar when I create instances of these classes on the client layer.

But, I want to set these values later on the server side so that they are more accurate as to when they are actually persisted.

I thought I could do this via the Interceptor interface by setting the values in the onSave() and onFlushDirty() methods, but that is not working - maybe I'm not doing it right. I have something like:

onSave()
{
if( entity instanceof Auditable )
{
((Auditable)entity).setCreateDate(Calendar.getInstance());
}

return false;
}

Is this the wrong way of doing this?


You can use an Interceptor to do it but, and I don't have my code in front of me, I believe that the object is already in a state where it's ready to be persisted so you can't change the "entity", you have to loop through the property names and then update the state[].

Actually, look at pages 128-129 of the 3.0.5 reference docs and it gives this exact example.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: setLastUpdateDate(), setCreateDate()
PostPosted: Wed Aug 10, 2005 9:31 pm 
Beginner
Beginner

Joined: Thu Jun 23, 2005 10:23 pm
Posts: 22
'you have to loop through the property names and then update the state[]'

I had also tried this and made sure I returned 'true' from the onSave method, but it still did not work for me. I will try again and look at the example you provided. Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 10:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
BTW, I just added support for "db generated values" to CVS earlier this week. One of the manifestations of this is the ability to use the current database timestamp as the optimistic locking value.

Basically, you just do :
Code:
<class ...>
    ...
    <timestamp ... source="db"/>
    ...
</class>


As long as the dialect you are using supports retrieval of the database's current timestamp (via select or direct function call). I got all the ones I knew; noone responded regarding the others on the dev list...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 11:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Also, WIP is the worst radio station *ever*; go eagles! :)


Top
 Profile  
 
 Post subject: setLastUpdateDate(), setCreateDate()
PostPosted: Thu Aug 11, 2005 10:16 am 
Beginner
Beginner

Joined: Thu Jun 23, 2005 10:23 pm
Posts: 22
Thanks alot pksiv. I've looked at the ref doc many times, don't know how I overlooked that. Thanks again. It worked great.

Steve: EAGLES RULE! :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.