-->
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: MappingException when implementing a Nullable Int32
PostPosted: Wed Apr 12, 2006 6:26 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
I have a test table called NullTest:

NltId: Unique id for each row, not null
NltInt: Nullable int column
NltVarchar: Nullable varchar column
NltVersionNo: Not null version number

I have created a class called NullInt32 so I can save and load null values to the NltInt column:

Code:
namespace NHibernateNullTest
{
   
   public class NullInt32 : IUserType
   {
      private static NullableType int32Type = new Nullables.NHibernate.NullableInt32Type();
      private const int NULL_INT = -9999;

      public NullInt32() {}
      
      #region IUserType Members
      
      public new bool Equals(object x, object y)
      {     
         if(x==y) return true;
   
         int lhs = (x==null) ? NULL_INT : (int)x;
         int rhs = (y==null) ? NULL_INT : (int)y;
   
         return int32Type.Equals(lhs, rhs);
      }
   
      public SqlType[] SqlTypes
      {
         get  { return new SqlType[] { int32Type.SqlType }; }
      }
   
      public System.Data.DbType[] DbTypes
      {
         get { return new DbType[] { int32Type.SqlType.DbType }; }
      }
   
      public object DeepCopy(object value)
      {
         return value;
      }
   
      public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
      {
         if(value.Equals(NULL_INT))
            ((IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
         else
            int32Type.Set(cmd, value, index);
      }
   
      public System.Type ReturnedType
      {
         get { return typeof(System.Int32); }
      }
   
      public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
      {
         object obj = int32Type.NullSafeGet(rs, names);
         if (obj == null)
            obj = (object)NULL_INT;
         return obj;
      }
   
      public bool IsMutable
      {
         get { return int32Type.IsMutable; }
      }
   
      #endregion

   }
}


And have a mapping file:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
    namespace="NHibernateNullTest" assembly="NHibernateNullTest">

    <class name="NullTest" table="NullTest">

        <id name="Id" unsaved-value="-9999">
            <column name="NltId" sql-type="Int32" not-null="true"/>
            <generator class="increment" />
        </id>

      <version name="VersionNo" column="NltVersionNo" type="Int32" access="property" />
         
        <property name="NltInt" column="NltInt" type="NHibernateNullTest.NullInt32, NHibernateNullTest" />
        <property name="NltVarchar" column="NltVarchar" />
    </class>

</hibernate-mapping>


My object class is:

Code:
namespace NHibernateNullTest
{
   /// <summary>
   /// Summary description for NullTest.
   /// </summary>
   public class NullTest
   {
      private int id;
      private int nltInt;
      private string nltVarchar;
      private int versionNo;

      public NullTest()
      {
         this.id = ngNINECore.NextUniqueID();
      }

      public int Id
      {
         get { return this.id; }
         set { this.id = value; }
      }

      public int NltInt
      {
         get { return this.nltInt; }
         set { this.nltInt = value; }
      }

      public string NltVarchar
      {
         get { return this.nltVarchar; }
         set { this.nltVarchar = value; }
      }

      public int VersionNo
      {
         get { return this.versionNo; }
         set { this.versionNo = value; }
      }

   }
}


When I execute:
IList list = session.CreateCriteria(typeof(NullTest)).List();

I get the exception:

A mapping exception occurred ---> NHibernate.MappingException: Invalid mapping information specified for type NHibernateNullTest.NullTest, check your mapping file for property type mismatches ---> System.InvalidCastException: Specified cast is not valid.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 3:54 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi.

Why don't you use the .NET 2.0 Nullable Types?
I'm using it with no problems.

Bye


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 5:28 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
I am stuck with .NET 1.1 for the moment. Any idea why the above would not work in 1.1


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 4:27 pm 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
My solution using .NET 1.1 was to have private methods used by NHibernate to map the nullable fields using the Nullables.dll. So an example method for representing a person's age as an integer is:

Code:
private NullableInt32 NullableAge
{
   get
   {             
      if (this.age == A_NULL_CONSTANT)
         return NullableInt32.Default;
      else
         return this.age;
   }
   set
   {
      if (value == NullableInt32.Default)
         this.age = A_NULL_CONSTANT;
      else
         this.age = value.Value;
   }
}


Calling code would use the usual public method to get Age as a normal Int.

In the mapping file the property would be mapped:

Code:
<property name="NullableAge" column="AgeColumn" type="Nullables.NHibernate.NullableInt32Type, Nullables.NHibernate"/>


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.