代码之家  ›  专栏  ›  技术社区  ›  Bob Palmer

n使用不同的键创建多对一/一对一

  •  1
  • Bob Palmer  · 技术社区  · 15 年前

    嘿,大家好,我正在对NHibernate进行调查,我有一个难题,我已经绞尽脑汁了一段时间,正在处理一个遗留数据库,其中包含一些相当复杂的关系。

    ClaimRoot具有claimGUID的主键。

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

      <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>
    

    提前谢谢! -鲍勃

    1 回复  |  直到 15 年前
        1
  •  1
  •   g .    15 年前

    假设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" 在数据库中生成约束。对于遗留数据库,这并不重要。