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.  [ 7 posts ] 
Author Message
 Post subject: Does null value automatically to 0?
PostPosted: Thu Jul 08, 2004 5:42 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
If you have a nullable coulmn mapped to java int field.
Does Hibbernate automatically convert null to zero when
it loads from database? or you need to specify that somehow?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 4:34 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
No it doesn't if you try and load a null value into an int property you will get an exception. If you wish to have this mapping from null -> 0 then create a custom UserType. Or you could just use Integer in your java objects and null will be stored correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 12:57 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 1:11 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
I would siggest to tune up your database and set NOT NULL for integer field.
In this particular case null and 0 is same for the integer.
But to be on a safe side it is a good practice to have `not null` integer fields in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 1:18 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
IvanLatysh wrote:
I would siggest to tune up your database and set NOT NULL for integer field.
In this particular case null and 0 is same for the integer.
But to be on a safe side it is a good practice to have `not null` integer fields in the database.


How can you be sure null and 0 would be the same thing for this particular instance? Perhaps the integer column represents a foreign key and the '0' record is definitely different than one that is null.

I would say that the safe bet would be to assume that null is never the same as 0 ;)

i.e. in retail if you get a null price or tax back out of the database you would probably want to handle it differently than if it were 0. i.e. maybe it really is free or has no tax on it vs being undefined (null)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 1:38 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
Quote:
How can you be sure null and 0 would be the same thing for this particular instance? Perhaps the integer column represents a foreign key and the '0' record is definitely different than one that is null.

I would say that the safe bet would be to assume that null is never the same as 0 ;)

i.e. in retail if you get a null price or tax back out of the database you would probably want to handle it differently than if it were 0. i.e. maybe it really is free or has no tax on it vs being undefined (null)


You absolutely right about foreign keys and the price.
But in this particular case integer value is mapping to int type.
That meants no null value is possible.

I can see two ways to solve the problem.
1) tune up the database to reflect your code
2) tune up your code to reflect the database

I suggested the first one, because he might see some other aspects of this integer field when he try to default it to null. And it might turn him to second solution, hopefully he will get it right :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2004 2:07 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Yep, I do agree with you your last points. I just didn't agree that NULL and 0 were the same thing ;)

The int can never be NULL but that doesn't mean that the actual datamodel he's building on that NULL isn't a valid value.

It's just a major pet peave of mine where a developer loads an object out of the database as null and then does some conversion behind the scenes in a GUI layer and then sends it back with the believe that 'NULL is the same as X'

Where X can be 0, "", etc. NULL is NULL which is nothing :)

So I just believe that if your datamodel is how you want it then use the appropriate java data types for it.


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