0

我有以下 UML 结构:

在此处输入图像描述

我正在尝试将其映射为类似于:

<class name="Parent" table="ParentTable">
    <id name="Id">
        <generator class="guid.comb" />
    </id>
    <one-to-one name="Child" class="IChild" property-ref="Parent" cascade="all" />
</class>

<class name="IChild" table="ChildTable" abstract="true">
    <id name="Id">
        <generator class="foreign">
            <param name="property">Parent</param>
        </generator>
    </id>   
    <discriminator column="TypeKey" type="String"/>
    <one-to-one name="Parent" class="Parent" />
    <one-to-one name="Child" class="IGrandchild" property-ref="Parent" cascade="all" />
</class>

<subclass name="ConcreteChild" extends="IChild" discriminator-value="ConcreteChild1">
  <property name="SomeProperty"/>
</subclass>

<class name="IGrandchild" table="GrandchildTable" abstract="true">
    <id name="Id">
        <generator class="guid.comb" />
    </id>
    <discriminator column="TypeKey" type="String"/>
    <many-to-one name="Parent" class="IChild" unique="true" column="ChildTableFk" />
</class>

<subclass name="ConcreteGrandchild" extends="IGrandchild" discriminator-value="ConcreteGrandchild1">
  <property name="SomeOtherProperty"/>
</subclass>

对 SQL 进行操作是行不通的(奇怪的是,使用 SQLite 是行得通的)。NHibernate 首先使用生成的 guid 插入 Parent。然后它会插入具有相同 guid 的孩子。但是在插入孙子时,它会使用 ChildTableFk null 插入它(并且从不尝试更新 FK 值)。

附加点:

  • 如果可能的话,我不希望将 IChild 映射更改为与 FK 使用多对一(我更喜欢共享 PK)。
  • 我无法更改 IGrandChild 映射以使用外部生成器,因为可以在更改对象图后更改孙子对象,而 NHibernate 不支持外部生成器(另一方面, IChild 实例永远不会更改给定 IParent 的生命周期)。

除此之外,欢迎提出任何建议,包括替代映射样式(只要它们支持多态 IChild 和 IGrandchild 对象)。

4

1 回答 1

0

糟糕,似乎我只是忘记在孙子上设置 Parent 属性。应该是我看的第一件事。

于 2013-06-30T13:50:04.460 回答