-->
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.  [ 2 posts ] 
Author Message
 Post subject: initialize default value in default constructor?
PostPosted: Thu Dec 28, 2006 8:01 pm 
Newbie

Joined: Wed Nov 02, 2005 10:49 pm
Posts: 12
Hibernate Reference Documentation 20.1.1. Customizing the schema says that:

Quote:
The default attribute lets you specify a default value for a column (you should assign the same value to the mapped property before saving a new instance of the mapped class).

Code:
<property name="credits" type="integer" insert="false">
    <column name="credits" default="10"/>
</property>

[/quote]

What "you should assign the same value to the mapped property before saving a new instance of the mapped class" means? does it means that the default attribute only applied to hbm2ddl tool?

If that's right, how can I initialize property's default value, especially for String field, because of underlying DB's NOT NULL contraint?

Is it possible to this job in POJO default constructor? sometimes I got the following erros:

Code:
public class ScheduleItem {
    private Integer mId;
    private String consignee;
    private String consigneePhone;
    ...
   
    public ScheduleItem() {
        consignee = "";
        consigneePhone = "";
        oldFurnitureMoveTo = "";
        mOtherNote = "";
        mOtherNote2 = "";
        mOtherNote3 = "";
        mConfirmLog = "";
    }
    ...
}

org.hibernate.AssertionFailure: null id in ScheduleItem entry (don't flush the Session after an exception occurs)


Could anybody do me a favor? thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 30, 2006 11:15 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
I guess yes. There are a lot of properties that are only aimed at schema generation (foreign-key attribute, <comment>, length, not-null, and so on).

However it's not necessary to init every string to "". It's more useful for a default age, for example. But defaulting empty string is certainly unuseful I think, because for example Oracle considers null and empty strings as equivalent, I seem to remember.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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