-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to define Object property default value ?
PostPosted: Tue Mar 23, 2004 11:55 am 
Newbie

Joined: Fri Feb 20, 2004 4:56 pm
Posts: 14
Hi,

Basically, I would like to save object within a table and automatically
update on column of the same table which log current date when insert occured.

How can I specified within the O/R mapping that the default
value associated to a dedicated column named DATE_UPDATED in the current date (GETDATE for sql server ) ?

If this can be done at O/R mapping level, I will normally never used
setDateUpdated(TheCurrentDate) on the saved object itself.

can someone provide to me O/R mapping sample code ?

Thanks in advance.
zoeeoz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 12:54 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Use the LifeCycle or Interceptor interfaces. I think Interceptor is the recommended one for audit date times like this.

Something like this in the onSave() should do it.

Code:
    public boolean onSave( Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types ) throws CallbackException
    {
        List properties = Arrays.asList( propertyNames );

        // find the index of the created date field
        propertyIndex = properties.indexOf( "DATE_UPDATED" );

        // set to the current date
        state[ propertyIndex ] = nowDate;
    }


then use this interceptor as an argument to openSession().

Cheers.

Mike.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can also use a default value in your SQL DDL when creating your table and set the property to update="false", insert="false".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 5:14 pm 
Newbie

Joined: Wed Mar 17, 2004 7:44 pm
Posts: 7
Location: Chicago Area, IL, US
christian wrote:
You can also use a default value in your SQL DDL when creating your table and set the property to update="false", insert="false".


Please correct me if I understand this incorrectly, but won't using the update="false" approach cause the default value to be used only on an INSERT into the table? I am also trying to do something similar to what the first poster is asking to do.

I have a table that has a timestamp field that defaults to the current timestamp (current_timestamp in DB2/400). By setting insert="false" and update="false", an inserted record correctly defaults to the current timestamp, because Hibernate (2.1.2) correctly omits the timestamp field from the generated SQL INSERT statement. However, omitting the field in updates will leave the timestamp field unchanged. This can be overriden in SQL by using "default" in the values list, just like Hibernate uses for the primary key field for the IDENTITY field in my table on inserts. Is there a way to configure Hibernate to always use "default" on inserts/updates, and read the timestamp normally on reads?

The reason I am shying away from using the <timestamp> configuration is that I would like the timestamp to be set by the database, not the application server. For optimistic locking, I cannot guarantee that each node in the clustered application environment has the exact same system time, and there's a chance of wackiness in the locking if the system clocks on the nodes become significantly out of sync (or are geographically separated and not using UTC).

My plan B is to write a trigger to handle the timestamp updates, but I'd like to see what is possible from the Hibernate side of things.

TIA for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 2:41 am 
Newbie

Joined: Fri Feb 20, 2004 4:56 pm
Posts: 14
Hi,

How to write it within the O/R XML mapping file if it is possible ?


Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 5:16 pm 
Newbie

Joined: Wed Mar 17, 2004 7:44 pm
Posts: 7
Location: Chicago Area, IL, US
*Bump*

Anyone have any ideas on this?


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