2010-04-19 128 views
2

我有一个接口IUserLocation和具体类型UserLocation多态NHibernate映射

当我使用的ICriteria,指定接口IUserLocation,我想NHibernate的实例化具体UserLocation类型的集合。

我已经使用table per concrete type策略创建了一个HBM映射文件(如下所示)。然而,当我查询NHibernate的使用的ICriteria我得到:

的NHibernate不能实例化抽象类或接口MyNamespace.IUserLocation

任何人都可以看到这是为什么? (为NHibernate的here的相关位的源代码(我认为))

我的ICriteria:

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

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

我的映射文件:

<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> 

回答

3

从它看起来像你的映射文件中的文件应对我做。我从来没有试过每个具体类的表。我注意到NHibernate documentation中的例子没有使用基类的接口。也许它不被支持?

我以前使用过Table per concrete class, using implicit polymorphism,每个子类都有单独的映射。

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

谢谢大卫。它发生在我身上,我可能会吠叫错误的树,因为我试图在这里使用NHibernate有点像IOC容器,实例化一个具体类型,我只提供一个接口。如果NHibernate支持这个,我认为它会非常有用。 由于我使用的解决方案的局限性,将接口修改为抽象基类是不切实际的。作为临时攻击,我正在使用IOC容器来解析提供给ICriteria的接口的具体类型。 – Ben 2010-04-19 21:59:10

+1

我开始再次看到这一点,我注意到如果映射文件使用UserLocation而不是IUserLocation,并且删除了union-subclass,则仍然可以在条件中将其作为IUserLocation引用,并获取IList 。 – 2010-04-20 18:25:21