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>
謝謝大衛。它發生在我身上,我可能會吠叫錯誤的樹,因爲我試圖在這裏使用NHibernate有點像IOC容器,實例化一個具體類型,我只提供一個接口。如果NHibernate支持這個,我認爲它會非常有用。 由於我使用的解決方案的侷限性,將接口修改爲抽象基類是不切實際的。作爲臨時攻擊,我正在使用IOC容器來解析提供給ICriteria的接口的具體類型。 – Ben 2010-04-19 21:59:10
我開始再次看到這一點,我注意到如果映射文件使用UserLocation而不是IUserLocation,並且刪除了union-subclass,則仍然可以在條件中將其作爲IUserLocation引用,並獲取IList。 –
2010-04-20 18:25:21