-->
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.  [ 8 posts ] 
Author Message
 Post subject: Empty int saved as 0 not NULL
PostPosted: Mon Jun 04, 2007 6:25 am 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
Hi,

When I save an object that has empty int variables, those get saved into the DB as 0, not as null. Where can I make a change to get NHibernate to save the record with a null value?

Thanks,
Jerry


Top
 Profile  
 
 Post subject: Re: Empty int saved as 0 not NULL
PostPosted: Mon Jun 04, 2007 7:02 am 
Newbie

Joined: Mon May 14, 2007 10:38 am
Posts: 10
jerryau wrote:
When I save an object that has empty int variables, those get saved into the DB as 0, not as null. Where can I make a change to get NHibernate to save the record with a null value?


What do you mean for "empty int"? An int variable cannot be empty, it can be only a valid number.
I think that if the column can be null you must use a Nullable type.
Try to declare your property and variable type like this: int?

In this way the property accepts also null values.

Davide


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 8:31 am 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
If I have a class:
Code:
public class Cat
{
   private string name;
   private int age;

   public string Name
   {
      get { return name; }
      set { name = value;}
   }

   public int age
   {
      get { return age; }
      set { age = value; }
   }
}

Whenever I try to read a record from the DB where age has a NULL value in the DB, NHibernate assigns 0 for the age variable. Same thing happens when saving an unassigned age variable, the DB get's a 0.
I need to get and save NULLs instead of 0 for unassigned age, as returning or saving a 0 into the DB isn't the correct value, because the Cat might by 5 years old not 0 and I have just left that value out; therefore I'm storing and working with an incorrect age.
Would normal practice be, instead of using "private int age" to use "private Int32 age"? Or what's the recommended way to work with such data?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 9:03 am 
Newbie

Joined: Mon May 14, 2007 10:38 am
Posts: 10
Try with this (using Nullable values):

Code:
public class Cat
{
   private string name;
   private int? age;

   public string Name
   {
      get { return name; }
      set { name = value;}
   }

   public int? Age
   {
      get { return age; }
      set { age = value; }
   }
}


Davide


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 9:56 am 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
Tried the "int?" type, and now I get this message:
Code:
"Could not cast the value in field age_13_0_ of type Int32 to the Type SerializableType.  Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type."

Do I need to make a change anywhere else besides that class?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 10:20 am 
Newbie

Joined: Mon May 14, 2007 10:38 am
Posts: 10
jerryau wrote:
Tried the "int?" type, and now I get this message:
Code:
"Could not cast the value in field age_13_0_ of type Int32 to the Type SerializableType.  Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type."

Do I need to make a change anywhere else besides that class?


As I known you don't need any special configuration. For more information try to look at this link:
http://www.hibernate.org/hib_docs/nhibe ... ml_single/
and search for "Nullable<Int32>"

Consider that nullable types are supported only NHibernate 1.2 I think.

Davide


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 11:00 am 
Newbie

Joined: Thu Jun 22, 2006 4:04 am
Posts: 8
Location: Prague
.NET 2.0 Nullables works just fine in 1.0.3
Just set "type" attribute for property mapping to "non-nullable type"
See example....

Class:
Code:
public int? Age
   {
      get { return age; }
      set { age = value; }
   }


Mapping:
Code:
<property name="Age" type="int">
   <column name="Age" sql-type="int" not-null="false"/>
</property>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 2:22 pm 
Newbie

Joined: Sat Jun 02, 2007 8:55 am
Posts: 7
After migrating to NHibernate 1.2, the "int?" nullable works perfectly. Thank you very much for your help Davide. :)
Liwoj, if I had known that it would work in 1.0.3 a bit sooner it would have saved me a big headache when migrating (wasn't easy :) ), but at least I've got the newest version of NHibernate now :)

Thanks,
Jerry


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