gpt4 book ai didi

nhibernate - 多态 NHibernate 映射

转载 作者:行者123 更新时间:2023-12-04 06:54:54 25 4
gpt4 key购买 nike

我有一个接口(interface)IUserLocation和具体类型 UserLocation .

当我使用 ICriteria 时,指定接口(interface) IUserLocation ,我希望 NHibernate 实例化一个具体的集合 UserLocation类型。

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

NHibernate cannot instantiate abstract class or interface MyNamespace.IUserLocation



谁能明白这是为什么? (NHibernate 相关位的源代码 here(我认为))

我的标准:
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>

最佳答案

从文档看来,您的映射文件应该对我执行此操作。不过,我从未尝试过每个具体类(class)的表格。我注意到 NHibernate documentation 中的示例因为它不使用基类的接口(interface)。也许不支持?

我用过Table per concrete class, using implicit polymorphism之前为每个子类单独映射。

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

关于nhibernate - 多态 NHibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2670687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com