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.