views:

476

answers:

2

Hi I want to write a FindByExample(object o) method. So I tried this:

public IList<T> FindByExample(T o)
{
    return Session.CreateCriteria(typeof(T)).Add(Example.Create(o)).List<T>();
}

(It's in a generic class)

It should work fine, but if T has a property of an enum type, it throws this exception: "Type mismatch in NHibernate.Criterion.SimpleExpression: EnumProperty expected type System.Int32, actual type EnumType"

The mapping is this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...>

<class name="OrdenDeCompra" table="ordenDeCompra" lazy="false">

<id name="Id" column="id_ordenDeCompra" type="Int32">
  <generator class="increment" />
</id>

...

<property name="EnumType" column="id_enum" 
          type="Int32" not-null="true" />
...

</class> </hibernate-mapping>

How do I do search by Enum?

+7  A: 

Change the type of the map for the enum from Int32 to the type of the enum.

<property name="EnumType" column="id_enum" 
          type="namespace.here.enumTypeName, assemblyname" not-null="true" />

Edit: The integer value of the enum will still be stored in the DB as an int though.

Chad Ruppert
A: 

Hey Thanks, that works!

no problem. Might you accept the answer? :)
Chad Ruppert