views:

854

answers:

1

This code

ICriteria crit = service.Session.CreateCriteria(typeof (DatabaseVersion));
crit.Add(Restrictions.Eq("Id.Minor", 4));
IList<DatabaseVersion> list = crit.List<DatabaseVersion>();

causes the following error :NHibernate.QueryException: Type mismatch in NHibernate.Criterion.SimpleExpression: Id.Minor expected type System.Int16, actual type System.Int32

How can i correct this error? I set the type in the mapping file with no luck. I would prefer not having to type cast in code.

Added mapping file as requested.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.NUnit.Domain.DatabaseVersion,Core.NUnit" table="`DatabaseVersion`" lazy="true" >
    <composite-id name="Id" class="Core.NUnit.Domain.DatabaseVersionId,Core.NUnit">
      <key-property name="Major" column="`Major`" />
      <key-property name="Minor" column="`Minor`" type="Int16"/>
      <key-property name="Build" column="`Build`" />
      <key-property name="Revision" column="`Revision`" />
    </composite-id>
    <property name="Description" column="`Description`"/>
    <property name="DateApplied" column="`DateApplied`"/>
  </class>
</hibernate-mapping>
+1  A: 

I think the problem might be that C# cannot implicitly cast an int to a short. It looks like your mapping maps this field to an Int16 (short). The "4" you are passing in is allocated as an int by default. (I don't know why it would have trouble comparing an int to a short though...)

I don't think there is a literal suffix for short. You might be able to do this:

crit.Add(Restrictions.Eq("Id.Minor", (short)4));
Andy White