NHibernate Polymorphic Mappings

I have an interface IUserLocation

and a concrete type UserLocation

.

When I use ICriteria by specifying an interface IUserLocation

, I want NHibernate to instantiate a specific type UserLocation

.

I created an HBM mapping file using the type-specific table strategy (shown below). However, when I query NHibernate using ICriteria I get:

NHibernate cannot instantiate abstract class or interface MyNamespace.IUserLocation

Can anyone understand why this is the case? (source code for the corresponding NHibernate bit is here (I think))

My ICriteria:

var filter = DetachedCriteria.For<IUserLocation>()
                             .Add(Restrictions.Eq("UserId", userId));

return filter.GetExecutableCriteria(UoW.Session)
             .List<IUserLocation>();

      

My mapping file:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="MyNamespace.IUserLocation,MyAssembly" abstract="true" table="IUserLocations">
    <composite-id>
      <key-property name="UserId" column="UserId" type="System.Guid"></key-property>
      <key-many-to-one name="Location" column="LocationId" class="MyNamespace.ILocation,MyAssembly"></key-many-to-one>
    </composite-id>
    <union-subclass table="UserLocations" name="MyNamespace2.UserLocation,MyAssembly2">
      <property name="IsAdmin" />
    </union-subclass>    
  </class>  
</hibernate-mapping>

      

+2


a source to share


1 answer


From the documentation, it looks like your mapping file should do this with me. However, I have never tried a table for a specific class. And I notice examples in the NHibernate documentation as it doesn't use interfaces for the base class. Perhaps this is not supported?

I used Table for a specific class using implicit polymorphism before with a separate mapping for each subclass.



<class name="MyNamespace.UserLocation,MyAssembly" table="UserLocations">
    ...
</class>

      

+3


a source







All Articles