-->
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.  [ 4 posts ] 
Author Message
 Post subject: Use database default value if property is null?
PostPosted: Wed Jul 30, 2008 7:06 am 
Newbie

Joined: Mon Oct 16, 2006 7:55 am
Posts: 5
Hello!

I would like that a database's default value is used when doing an INSERT if a property is null.

For example:
<class name="Person" table="Person">
[....]
<property name="faktor" type="double">
<column name="faktor" default="1.0" />
</property>
</class>

In the Java code I create a new object Person, do not fill in the property faktor and save it in the database. But instead of the default value "1.0", NULL is saved. But I would like that 1.0 is saved.

Can someone help me out?

Thanks in advance,
Martin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 8:14 am 
Newbie

Joined: Wed Jul 30, 2008 7:48 am
Posts: 10
Location: NOIDA
Hibernate does not support default value. You can handle it on database lavel(using trigger or assigning default value).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 8:35 am 
Newbie

Joined: Thu Mar 27, 2008 11:45 am
Posts: 10
Location: Russia
You can set default value in constructor:

Code:
public Person() {
   this.faktor=SOME_DEFAULT_VALUE;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 10:06 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It might actually be easier to just put it into the table creation at the database level. You could also override the setter of the POJO to return 1.0 when the object is null.

Just ideas...

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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