流畅的 nHibernate - 具有多个表的 DiscrimminateSubClassesOnColumn?
几天来,我一直在尝试研究子类并加入 Fluent nHibernate 中的子类,但没有取得任何进展。我查看了维基百科,但它似乎没有给我足够的信息,并且谷歌搜索返回给我的旧结果使用了贬值的代码或看似不相关的结果。
我只想要一个简单的例子,如果可能的话,
我有四个表,如下所示:
+--------------+ +---------------+ +---------------+
| Animal | | AnimalType | | Reptile |
+==============+ +===============+ +===============+
| Id | | Id | | AnimalId |
| AnimalTypeId | | Description | | TongueLength |
| version | +---------------+ +---------------+
+--------------+
因此 Animal 表有一个引用 AnimalType 表的 FK,一个仅包含 1 个 Id 的查找表,作为测试,
1 = Reptile
这个想法是,AnimalTypeId 列是我的鉴别器列,它分割了我的子类,并且每个动物表(Reptile)都有 FK 主键,这些主键根据 AnimalTypeId 引用 Animal 表中的 Id。
所以我的 C# 类看起来像这样
public class Animal
{
public virtual int Id {get;set;}
public virtual AnimalType AnimalType {get;set;}
public virtual int Version {get; protected set;}
}
public class AnimalType
{
public virtual int Id {get;set;}
public virtual string Description {get;set;}
}
public class Reptile : Animal
{
public virtual int AnimalId {get;set;}
public virtual float TongueLength {get;set;}
}
如何将其映射为每个表的子类?
这是我当前的映射。
public class AnimalMap : ClassMap<Animal>
{
public AnimalMap()
{
Table("Animal");
Id(x => x.Id);
DiscriminateSubClassesOnColumn("AnimalTypeId",0)
.AlwaysSelectWithValue();
Version(x => x.Version);
}
}
public class ReptileMap: SubclassMap<Reptile>
{
public ReptileMap()
{
DiscriminatorValue(1);
Table("Reptile");
KeyColumn("AnimalId");
Map(x => x.TongueLength);
}
}
public class AnimalTypeMap : ClassMap<AnimalType>
{
public AnimalTypeMap ()
{
Table("AnimalTypeMap");
Id(x => x.Id);
Map(x => x.Description);
}
}
它几乎可以工作,只是似乎没有发现子类来自不同的表。 HBM 映射如下。
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" discriminator-value="0" schema="dbo" name="NamespaceDefault.Animal, SolEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Animal">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<discriminator type="Int32" force="true">
<column name="AnimalTypeId" />
</discriminator>
<version name="Version" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Version" />
</version>
<subclass name="NamespaceDefault.Reptile, SolEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="1">
<property name="TongueLength" type="System.Float, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="TongueLength" />
</property>
</subclass>
</class>
</hibernate-mapping>
编辑: 早些时候存在重复实体的问题。解决方案链接在下一条评论中。
I've been trying to wrap my head around subclasses and joined subclasses in Fluent nHibernate for a few days now without making any sort of headway. I've looked at the wiki but it doesn't seem to give me enough information, and googling returns me old results that uses depreciated code or are seemingly unrelated.
I just want a simple example, if that's at all possible
I have four tables that look like this:
+--------------+ +---------------+ +---------------+
| Animal | | AnimalType | | Reptile |
+==============+ +===============+ +===============+
| Id | | Id | | AnimalId |
| AnimalTypeId | | Description | | TongueLength |
| version | +---------------+ +---------------+
+--------------+
So the Animal table has a FK that references the AnimalType table, a look up table containing just 1 Id, as a test,
1 = Reptile
The idea is that the AnimalTypeId column is my discriminator column that splits up my subclasses, and each of the animal tables (Reptile) have FK primary keys that are referencing the Id from the Animal table depending on its AnimalTypeId.
So my C# classes look like this
public class Animal
{
public virtual int Id {get;set;}
public virtual AnimalType AnimalType {get;set;}
public virtual int Version {get; protected set;}
}
public class AnimalType
{
public virtual int Id {get;set;}
public virtual string Description {get;set;}
}
public class Reptile : Animal
{
public virtual int AnimalId {get;set;}
public virtual float TongueLength {get;set;}
}
How do I map this as a subclass-per-table?
Here's my current mapping.
public class AnimalMap : ClassMap<Animal>
{
public AnimalMap()
{
Table("Animal");
Id(x => x.Id);
DiscriminateSubClassesOnColumn("AnimalTypeId",0)
.AlwaysSelectWithValue();
Version(x => x.Version);
}
}
public class ReptileMap: SubclassMap<Reptile>
{
public ReptileMap()
{
DiscriminatorValue(1);
Table("Reptile");
KeyColumn("AnimalId");
Map(x => x.TongueLength);
}
}
public class AnimalTypeMap : ClassMap<AnimalType>
{
public AnimalTypeMap ()
{
Table("AnimalTypeMap");
Id(x => x.Id);
Map(x => x.Description);
}
}
It almost works, it just doesn't seem to be picking up that the subclass comes from a different table. The HBM mapping is below.
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" discriminator-value="0" schema="dbo" name="NamespaceDefault.Animal, SolEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Animal">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<discriminator type="Int32" force="true">
<column name="AnimalTypeId" />
</discriminator>
<version name="Version" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Version" />
</version>
<subclass name="NamespaceDefault.Reptile, SolEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" discriminator-value="1">
<property name="TongueLength" type="System.Float, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="TongueLength" />
</property>
</subclass>
</class>
</hibernate-mapping>
edit:
There was an issue earlier with duplicate entities. Solution is linked in next comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论