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: map oracle sysdate
PostPosted: Wed Apr 21, 2004 11:12 am 
Newbie

Joined: Fri Apr 16, 2004 11:53 am
Posts: 7
Hi all,

I have a class like

class Bar{

Integer a;
String b;
java.util.Date timestamp;

}

When I want to create a new record in database, I have a sql like this

insert into tableBar (colA,colB, colTimestamp) values(?,?,sysdate).


Now with hibernate, how can I config the mapping file to let the generated sql use 'sysdate' instead of the value of the timestamp of the Bar object?


thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 7:00 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I don't think you can. The point of hibernate is to avoid using database lingo in your java code (IMHO). If you want to manage date on the java side, just create a new date, and set the field. Or, use the <timestamp> property, and let hibernate do it for you.

If you want to set w/ db time, a trigger is prob the best bet (oracle)...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 7:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If you want it to use sysdate only on insert, probably the best approach would be to give the column a defined default of sysdate and to map that particular property with its "insert" attribute set to false.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 9:52 am 
Newbie

Joined: Fri Apr 16, 2004 11:53 am
Posts: 7
Hi Steve,

How to
steve wrote:
give the column a defined default of sysdate.
? use formula? can you give me an example?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 9:57 am 
Newbie

Joined: Fri Apr 16, 2004 11:53 am
Posts: 7
thanks jlawmi and steve.

yes we can create a trigger for it, but it is just too heavy to use for such a simple problem.

i more like steve's idea, but don't know how.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 12:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
step #1, add a default value to the table column:
CREATE TABLE my_table (
my_column DATE DEFAULT SYSDATE
);

(or use the ALTER command if your unable to recreate the table)


step #2, in your mapping for this property in Hibernate define the property as not insertable in which case Hibernate will not include it in the insert column list (which is needed for default values to be applied).

<property name="myDateProperty" insert="false" .../>


But like I said, this only works if sysdate should only be used for default insert value (i.e., not updated on update).


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.