-->
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.  [ 3 posts ] 
Author Message
 Post subject: default value in column definition
PostPosted: Fri Jan 11, 2008 7:10 pm 
Newbie

Joined: Sun Dec 24, 2006 10:23 pm
Posts: 11
Hi;
how can i tell hibernate to map a default value to a field instead of a null value ?
For example i have a "lastUpdate" field and i would like hibernate to set it to the postgresql function now() .

I found a way to do this for a creationDate column which take only a default value at the first insert with:
Code:
@Column(name = "`creationDate`", insertable = false, updatable = false, columnDefinition = " timestamp default now()")
public Date getCreationDate() {
   return creationDate;
}


Now i would like the same think for:
Code:
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "`updateDate`")
public Date getUpdateDate() {
   return updateDate;
}


So i need updateDate to be updatable and with now() instead of null for the default value when my property is null.
It would save me some programming time as I need to implement this in many classes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 10:41 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
deeeed,

If your property "access" attribute is set to "property" (the default), you can add whatever you want to the getter of your bean, and hibernate would simply call that code. For example:

Code:
public Date getUpdateDate() {
   if (updateDate!=null)
      return updateDate;
  else
     return new Date();
}

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 11, 2008 11:00 pm 
Newbie

Joined: Sun Dec 24, 2006 10:23 pm
Posts: 11
Ah yes thank you!
It's exactly what i wanted.


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