-->
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: Best Practices for fields: DateCreated, DateUpdated
PostPosted: Mon Nov 27, 2006 4:41 pm 
Newbie

Joined: Mon Nov 13, 2006 12:43 pm
Posts: 11
Location: US
Hi,

I am using 1.2.0 Beta 2 with SQL Server 2005.

Each of objects store following fields:

DateCreated (Timestamp, when the object was created)
DateUpdated (Timestamp, when the object was last updated)
DateDeleted (Timestamp, when the object was marked as being deleted)

Are there any best practices or guidelines about implementing these types of fieds? Surely, someone here has already tried this before.

I came up with following for DateCreated. I have set the database column in SQL Server to default value of (GetDate())

<property name="DateCreated" type="Timestamp" insert="false" update="false"/>

This way, the SQL Server database INSERT the correct Timestamp for DateCreated field. Is there any way to achieve the same effect for 'DateUpdated' field?

Many thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 8:14 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Yes, for the DateUpdated field, use the <timestamp...> tag (or the <version...> tag which does the same thing). However, you have to set your database column to Date type, not timestamp type.

NH will automatically update the field and you have the side benefit of optimistic locking on updates and deletes.

As for the DateDeleted field, I don't know. Maybe you can use an interceptor so you don't have to manage this field in the client code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 2:02 am 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
An alternative is using the Interceptor interfaceprovided by NHibernate.
See http://www.hibernate.org/hib_docs/nhibernate/html/manipulatingdata.html#manipulatingdata-interceptors for details.

Regards
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 7:12 pm 
Newbie

Joined: Mon Nov 13, 2006 12:43 pm
Posts: 11
Location: US
Klaus,

Thanks for the link. It shows how to do it quite easily in Interceptor API. I would prefer to keep the Timestamp code in database tier (i.e. implement it in DB side)... that way I don't have to keep our Database and Web server times in sync-with each other. The times should be the same, but just in case...

Greenis,

Wow, this 'timestamp' property is pretty cool! Thanks for pointing me to it. I am curious to see if it uses SqlServer's GetDate() method, or .NET's DateTime. I.e. does it run on middle tier or db tier.... let me find this out...

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 7:45 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I'm pretty sure the timestamp tag is hard-coded to use the .NET DateTime.Now() method in NH. However, you can always submit a feature request.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 5:21 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
We use a trigger in the DB to handle this. The advantage here is that the value is coordinated correctly with the create time since both are generated by the db server (which might have a different date/time than the server where the NH application is run).

Something I've always wanted is a way to indicate that the value of a property is set to the value result of calling a DB function like SQL Server's GETDATE() function so you could do this without a trigger. Might be possible, but I haven't figured out how...

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 8:12 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
One thing to watch out for here is that A SQL Timestamp and DateTime are two different beasts. A Timestamp does not translate to a DateTime. A Timestamp is simply a mechanism for indicating the order that events happened inside the database. If you need a Timestamp for some third party utility, you won't be able to switch to DateTime. Just a warning.

We take care of this in the business layer ourselves. We initialize values in our constructors and modify values in our property setters where appropriate. Though I understand why this is not always the ideal approach.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 6:31 pm 
Newbie

Joined: Mon Nov 13, 2006 12:43 pm
Posts: 11
Location: US
Greenis:

Thanks again. Yeah I found out too that <timestamp> uses .NET DateTime.


Symon:

I agree with you. Having an equivalent of calling SQL Server's built in function would be very helpful in this case. Short of that, the only way to do this in the database is to use Triggers.

Onevenxtchuck:

Modifying values in property constructors is definitely a valid approach. Personally, I like the Interceptor way over setting DateUpdated etc in each property setter. It seems less prone to programming errors.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 6:43 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
desiguy,

All a matter of taste.

We code gen our business layer so we don't miss any setters. Most of the devs here myself included are a bit paranoid, probably overly, of putting any logic in the DB. To some, the interceptors cut a little too close to that vein (not to mix metaphores) so we dropped the modify time management in the business layer. Besides, it's one less thing to worry about changing if Management flips out over using *gasp* an open source project in their new web site. =)

Take care

Chuck


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.