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.  [ 4 posts ] 
Author Message
 Post subject: Insert a null value in a numeric database column
PostPosted: Fri Oct 06, 2006 2:17 pm 
Newbie

Joined: Fri Oct 06, 2006 1:25 pm
Posts: 5
Hi
Perhaps somebody can help me here?
I'd like to add a null value in a numeric database column

I defined a property in a mapping file. It's type is float and is Is-null attribute is set to false
I defined too a property class with the same type

When i saved (Save instruction) my class, the property class is initialised to zero even if the database column can store a null value.

Is it possible to store null value with NHibernate?

Thanks for you help
Christophe


Top
 Profile  
 
 Post subject: More information
PostPosted: Fri Oct 06, 2006 2:52 pm 
Newbie

Joined: Fri Oct 06, 2006 1:25 pm
Posts: 5
Another information: I work with Visual studio 2003 and the 1.1 .Net framework version


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 5:16 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
In your mapping, you specify isNull=false, which means the column does not allow nulls.

The 2nd thing you should do is implement a custom IUserType. In .NET 2.0 we have nullable types, and NHibernate can work with that.

In 1.1, however, if you create a custom type you can specify a magic value that MEANS null, for example Int32.MinValue. If you initialize your integers to this, and create a NullIntType that implements IUserType correctly, then you can specify that when setting a value to a command's parameter, if the value is ####, then use DBNull.Value, otherwise use the value of the integer. Likewise when the iusertype gives you a datareader, you pull the value from the reader and convert it to the integer.

Hope this helps.

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 11:55 pm 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
you also can use the NullableType library. If you use NHibernate 1.0.2 you have to download the NHibernate Contrib package. In NHibernate 1.2 Beta it is included. Your code should look like this:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="MyAssembly" namespace="MyNamespace">
   <class name="MyClass" table="My_Table">
      ...
      <property name="NullableFloat" column="Float_Value" type="Nullables.NHibernate.NullableDoubleType, Nullables.NHibernate" not-null="false" />
      ...
   </class>
</hibernate-mapping>


Code:
using Nullables;

namespace MyNamespace
{
   public sealed class MyClass : BaseInternalModel
   {
      ...
      private NullableDouble mNullableFloat;
      ...
      public MyClass()
      {
      }
      ...

      public NullableDouble NullableFloat
      {
         get { return mNullableFloat; }
         set { mNullableFloat = value; }
      }
      ...
   }
}


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