-->
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.  [ 12 posts ] 
Author Message
 Post subject: Getting createdDate from Database
PostPosted: Mon Nov 26, 2007 5:04 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Hi all ,
I have a situation where i need to insert a created_date in the databse column but the date that is generated should not be application server generated but the database server generated because the two servers can be located in different timzones.Using hibernate and SQL-Server 2005 can i do that.Deos anyone have solution to this problem.In other words i need to call the getdate() function of sql server when inserting records.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 7:48 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Try this.

i) Specify DEFAULT GETDATE() for created_date in Table Definition
ii) Mark the property with generated="insert" ( or always ) as appropriate

More information on Generated Properties is available in the Hibernate Tutorial.

-------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 8:22 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Thank you promod.
I will try the first solution and let you know.
Regarding second solution ,I googled for Generated properties or documentation reegarding generated properties,coundnt find some tutorial,example or properdocumentation.can u please point me to some link or tutorial regarding this.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 8:28 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,

I was referring this link,
http://www.hibernate.org/hib_docs/refer ... -generated

It's simple and descriptive.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:22 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Hi promod ,

I read the documentation and tried to implement that as shown below

Code:
<property name="createdDate" column="created_dateTime" insert="false" update="false" generated="insert"  />


Since the column is not allowing nulls,it throws

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'created_dateTime', table Audit; column does not allow nulls. INSERT fails.

Any Inputs


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:44 pm 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Have you specified the default value ( GETDATE() ) in table definition ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 12:08 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Hi promod ,

No i am afraid i havent specified it.I will specify it and keep you posted.I thought they were two different solutions


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 3:06 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
Thanks Its working .Sorry for being late.I tried that and it is working for created_date.But for modified date it is not working .Is their any solution in which whwnever i modify the row,modified column generates the database timestamp?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 4:37 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Not familiar enough with SQL Server to say if you can do that as part of the column definition (MySQL has a feature for that). But a general solution would be to write a database trigger, and SQL Server certainly supports that, which sets the modified_date field on update.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 4:58 am 
Newbie

Joined: Wed Nov 21, 2007 12:58 am
Posts: 10
thanks,
i will try that option.But when thing is that it is working for created_date
column but not for modified date column ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 6:41 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
It doesn't work for updates because the default value clause only takes effect on insert when that column is not mentioned in the SQL insert statement.


Top
 Profile  
 
 Post subject: Using Database getdate()
PostPosted: Mon Apr 14, 2008 1:58 pm 
Newbie

Joined: Mon Apr 14, 2008 1:08 pm
Posts: 1
Location: Nashville, TN
We are trying to do the same thing - simply use the database (SQL Server 2000 in our case) function getdate() for create and update timestamps (as opposed to the app server generating the datestamps). I would have guessed that the implementation of the mapping insert=false update=false generated=insert/always would have simply used the native database getdate() function based on the generated type (use getdate() for SQL Server on insert for generated types of insert or always, and only use getdate() on update for generated type of always) as opposed to us having to use triggers or the default syntax in our database. If this is the case, then the use of the generated attribute seems very misleading - why not just use update=false and insert=false? Using generated=insert implies that Hibernate will generate this using the underlying database date generation function. Is that not true?

_________________
CMahy


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