-->
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.  [ 9 posts ] 
Author Message
 Post subject: how to set default value?
PostPosted: Wed Jun 13, 2007 1:25 am 
Beginner
Beginner

Joined: Thu May 17, 2007 2:41 pm
Posts: 48
I have a class with int property. when I allow the property to be left null, it has problem loading from the database.

How are you suppose to make sure the column value defaults to 0? (I thought the java class should have done that already). there does not seem to be a default-value attribute to set in the hbm. Do you simply make

int instancevariable = 0;

explicit in the Java class?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 1:41 am 
Newbie

Joined: Mon Jun 11, 2007 4:42 am
Posts: 7
Location: Italy
You can do it in your own DBMS, when you define the table. If you already have a table you can modify the settings.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 1:54 pm 
Beginner
Beginner

Joined: Thu May 17, 2007 2:41 pm
Posts: 48
of course you can do anything you want to going directly to the database. is it definitive that there is nothing in hibernate which addresses this?


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 3:04 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
A Long property can have a defailt value like this:
Code:
     <property name="myLong" type="long" >
        <column name="aLong" default="0"/>
     </property>

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 17, 2007 9:47 pm 
Beginner
Beginner

Joined: Thu May 17, 2007 2:41 pm
Posts: 48
I have an int type, and in the hbm file
<property name="doctype" >
<column name="DOCTYPE" default="1" />
</property>

but, the default value is still NULL in the data schema.

See anything wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 4:50 am 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
which version of hibernate are you using ?

This is the behaviour of latest hibernate + annotations:

private Integer value;

generates a nullable column

private int value

generates a column with not null constraint

Seems like you want to store ints not nullable Integers. In case of hbm files (instead of annotations) there is a chance you will have to not-null="true" attribute to column definition.

Please rate if that helped.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Mon Jun 18, 2007 3:58 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Well on looking deeper it appears that default is only honored in case of bulk insert.

so here you would have to set the default value in your code while declaring the property. It seems to work in my test case.

*Please do rate if it helps*

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 5:32 pm 
Beginner
Beginner

Joined: Thu May 17, 2007 2:41 pm
Posts: 48
I am using hibernate 3.2.

there may be a little misunderstanding in my exact situation.

actually I am talking about a object reference not a simple property. Because I use int values for id's, hibernate creates a int(11) data type, which defaults to NULL. So, it is neither Integer nor int in the java class.

How would either of you handle such a case?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 5:50 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
aaooww......

This is no case to handle as such :-). It basically depends on your design if you would like it to be null or not. In case you don't want it to be null the you set it to not-null else allow null.

In any case ids should not have a default value. SHOULD NOT.

Regards,
Jitendra


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