In my project I use a IUserType (BooleanM1) that handles boolean values and writes -1 for true and 0 for false values to the database. So far everything works well. The mapping looks like this:
<property name="Active" column="ACTIVE" type="Core.Persistence.NH.Types.BooleanM1,Core.Test"/>
So if I do a query like the following
开发者_StackOverflowvar pList = Session.Query<Test>().Where( c => c.Active ).ToList();
an exception is thrown:
NHibernate.QueryException: Unable to render boolean literal value [.Where[Core.Test.Domain.Test]
(NHibernate.Linq.NhQueryable`1[Core.Test.Domain.Test], Quote((c, ) => (c.Active)), )]
---> System.InvalidCastException: Das Objekt des Typs "NHibernate.Type.CustomType" kann nicht
in Typ "NHibernate.Type.BooleanType" umgewandelt werden.
The BooleanM1 implementation is like this:
{
public class BooleanM1 : IUserType
{
public bool IsMutable
{
get { return false; }
}
public Type ReturnedType
{
get { return typeof(bool); }
}
public SqlType[] SqlTypes
{
get { return new[]{NHibernateUtil.Int16.SqlType}; }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if(obj == null ) return false;
return ((string)obj == "-1" || (string)obj == "1") ? true : false;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if(value == null)
{
((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
((IDataParameter) cmd.Parameters[index]).Value = (bool)value ? -1 : 0;
}
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public new bool Equals(object x, object y)
{
if( ReferenceEquals(x,y) ) return true;
if( x == null || y == null ) return false;
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode();
}
}
Is this an known bug in the linq provider or is there something wrong with my UserType? Any help is appreciated.
I had a similar issue with a UserType that does pretty much the same thing. I found that explicitely stating the equality in my queries resolved the problem.
Try going from:
var pList = Session.Query<Test>().Where( c => c.Active ).ToList();
to:
var pList = Session.Query<Test>().Where( c => c.Active == true ).ToList();
For some reason NHibernate's Linq provider is then able to figure it out.
Have you solved this yet?
Not sure if it is the same issue but I had something similar where the database field was nullable and the custom type specifies that you are returning a "bool" -> It may need to be changed to "bool? for the return type"
public Type ReturnedType
{
get { return typeof(bool?); }
}
Fixed in version 4.1.0. The user type should implement IEnhancedUserType to work correctly with the fix. https://nhibernate.jira.com/browse/NH-2839
精彩评论