【问题标题】:NHibernate Many to One / One to One with differing keysNHibernate 多对一/一对一使用不同的键
【发布时间】:2009-11-24 10:41:49
【问题描述】:

大家好,我正在尝试使用 NHibernate 并遇到一个难题,我一直在摸索一段时间,正在使用具有一些相当复杂关系的旧数据库。

ClaimRoot 有一个 claimGUID 的主键。 ClaimRoot 有一个与 claimGUID 关联的 Claimdetails 包(这是一种享受)。

问题在于 ClaimRoot 还与 ClaimFinancials 具有可选的一对一关系(并非所有 ClaimRoot 都有 ClaimFinancials,但大多数都有)。但是 ClaimFinancials 的 PK 是一个 FormID 字段。该字段存在于 ClaimRoot 中,但不是 PK。

我在下面发布了一个映射,删除了额外的列以保护无辜者。

  <class name="ClaimRoot" table="tbl_ClaimRoot" schema="DB1.dbo">
    <id name="ClaimGUID">
      <generator class="guid"/>      
    </id>
    <property name="FormID" />
    <property name="LastFormNoteText" />
    <bag name="ClaimDetails" inverse="true">
      <key column="ClaimGUID"/>
      <one-to-many class="ClaimDetails"/>
    </bag>
  </class>

  <class name="ClaimDetails" table="tbl_ClaimDetails" schema="DB2.dbo">
    <id name="RowID">
      <generator class="native"/>
    </id>
    <property name="ClaimGUID" />
    <property name="SeqNo"/>
    <property name="B1A_InsID" />
    <many-to-one name="Root" column="ClaimGUID" foreign-key="ClaimGUID"/>
  </class>


  <class name="ClaimFinancials" table="tbl_ClaimFinancials" schema="DB1.dbo">
    <id name="FormID">
      <generator class="native"/>
    </id>
    <property name="CreatedDate"/>
    <property name="SubmittedDate" />
  </class>

提前致谢! -鲍勃

【问题讨论】:

  • 只是一个背景资料仅供参考 - 我将这些关系放在一起的原因是,我们当前的 POCO 有方法,这些方法的 Sprocs 更新 ClaimFinancials 和 ClaimRoot,或者 ClaimFinancials 和 ClaimDetails 在单个方法调用中,因为紧关系,我不想拆开旧版应用程序。

标签: nhibernate nhibernate-mapping many-to-one


【解决方案1】:

假设 FormID 仅用于链接 ClaimRoot 和 ClaimFinancials,听起来您需要从 ClaimRoot 到 ClaimFinancials 的多对一关系。将 ClaimRoot 上的 FormId 属性替换为多对一。

  <class name="ClaimRoot" table="tbl_ClaimRoot" schema="DB1.dbo">
    ...
    <many-to-one name="ClaimFinancials" column="FormID" />
    ...
  </class>

即使“多”一侧只有“一”,也可以使用多对一关系。如果您正在生成模式,则可以指定 unique="true" 以在数据库中生成约束。使用旧数据库,这无关紧要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多