-->
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.  [ 5 posts ] 
Author Message
 Post subject: Default value instead of NULL
PostPosted: Mon Oct 06, 2008 11:14 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I am sure I have read somewhere that one can provide default value for Hibernate to provide for an attribute in case a null is found in the database. But I don't know how and I can't find the piece of documentation anymore.

Can anybody help with this?

Jens Schauder

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 12:02 pm 
Newbie

Joined: Fri Aug 03, 2007 9:39 am
Posts: 2
Usually you do the inverse thing: a null value set in the bean will be set in a default value in database.

You get this using default attribute within column element inside a property element:

Code:
<property name="property">
   <column name="PROPERTY_VALUE" default="..." />
</property>


Instead, if you want to set an attribute with a default value, in case in the database there is a null value in corresponding field, I think the best way is to modify your setter methods like this:

Code:

private final static Object DEFAULT_VALUE = ...;

private Object property;

public void setProperty(Object property) {
   if (property == null) {
        property = DEFAULT_VALUE;
   }
}


Pay attention that in this way, you cannot ever have a null value for your property: do you want this?

Daniele
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 1:06 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Hi daniele,
thanx for the effort, but this is not what I am looking for.

The setter thing is close, and it is what we are actually using right now. But the problem with this is, that Hibernate consideres this a change so the session becomes dirty and an update is caused.

The feature I am looking for was intended for legacy databases which might have a null value which really should some special instance. I am actually wondering if this could be used to avoid the problem of embeddable objects being null when all attributes are null.

(http://opensource.atlassian.com/project ... owse/HB-31)

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: Default value
PostPosted: Thu Nov 20, 2008 10:42 am 
Newbie

Joined: Wed Nov 19, 2008 2:04 pm
Posts: 4
Hi,

I tried to configure default value sysdate for a date column.

<column name="xxx" default="sysdate"/>

when i ran the application it was not storing the sysdate in the column ?


Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2008 8:21 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I don't see how this is an answer to my question ... don't know looks more like another question to me.

Anyway: AFAIK the default attribute sets a default value for the column in the database. At least with oracle this attribute is only used, when a insert statement doesn't specify any value for that column. But hibernate normally specifies a a value (NULL in case of doubt) to all columns, thus the default value never get used.

You can configure hibernate somehow to just update the columns that actually changed (I think), but I don't remember how, and I don't know if it applies to inserts.

Just set the attribute to the desired default value in your java code (e.g. in the constructor), or if you need the value from the database, set insertable and updatable to false. At least I think this should work.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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