【问题标题】:NHibernate mapping child collection without foreign key relationship没有外键关系的 NHibernate 映射子集合
【发布时间】:2017-02-09 00:16:20
【问题描述】:

出于“遗留原因”,我有一个 Account 表和一个 Transaction 表,没有从 Transaction 表到 Account 表的外键引用。 NHibernate Account 表的映射 XML 如下所示:

<class name="Account" table="[tblAccount]" lazy="true">
    <id name="ID" column="ID" type="Int32">
      <generator class="native" />
    </id>
    <version name="NHVersion" column="[NHVersion]" unsaved-value="0" />
    <property name="AccountNumber" column="[AccountNumber]" type="String" not-null="true" length="30" />
    <property name="FormattedAccountNumber" column="[FormattedAccountNumber]" type="String" not-null="true" length="30" />
    <property name="Title" column="[Title]" type="String" not-null="true" length="30" />
    <many-to-one name="AccountCode" column="tblAccountCodeID" class="AccountCode" cascade="none" />
    <one-to-one name="AccountSegment" cascade="all" class="AccountSegment" property-ref="Account" />
    <bag name="Categories" table="tblAccountCategory" lazy="true" inverse="false">
      <key column="tblAccountID" />
      <many-to-many class="Category" column="tblCategoryID" />
    </bag>
</class>

Transaction 表的映射 XML 是:

<class name="Transaction" table="[tblTransaction]" lazy="true">
    <id name="ID" column="ID" type="Int32">
      <generator class="native" />
    </id>
    <version name="NHVersion" column="[NHVersion]" unsaved-value="0" />
    <property name="GLAccount" column="[GLAccount]" type="String" not-null="true" length="30" />
    <property name="Description" column="[Description]" type="String" not-null="true" length="60" />
    <property name="Amount" column="[Amount]" type="Decimal" not-null="true" />
</class>

可以使用GLAccount 列查询给定帐户的交易表行。以下 SQL 有效:

select act.FormattedAccountNumber, act.Title, trn.Date, trn.Amount
from tblAccount act
left join tblTransaction trn on act.AccountNumber = trn.GLAccount
where act.AccountNumber = '011020'

如何定义帐户交易的映射?似乎没有办法为每个表指定列名。也许使用where?我查看了这个question and answer,但它似乎不适用于我的情况。

【问题讨论】:

    标签: c# sql-server nhibernate


    【解决方案1】:

    没有主键的场景有NHibernate内置解决方案:

    FROM tblAccount act
    LEFT JOIN tblTransaction trn 
     ON act.AccountNumber = trn.GLAccount
    

    这就是property-ref 的魔力。

    <class name="Account" table="[tblAccount]" lazy="true">
       ...
       <!-- THE property -->
       <property name="AccountNumber" column="[AccountNumber]" type="String" not-null="true"/>
    
       <!-- collection referenced by THE property -->
       <bag name="Transactions" lazy="true" inverse="false">
          <!-- key column of a target table -->
          <!-- property-ref of our table -->
          <key column="GLAccount" property-ref="AccountNumber" />
          <one-to-many class="Transaction" />
       </bag>
    

    即是父级,下面是子级(多对一使用相同的属性作为引用)

    <class name="Transaction" table="[tblTransaction]" lazy="true">
        ...
        <property name="GLAccount"  column="[GLAccount]" type="String" not-null="true"/>
        <many-to-one name="Account" column="[GLAccount]" property-ref="AccountNumber"  />
        
    

    5.1.11. many-to-one

    ...

    property-ref 属性只能用于映射旧数据,其中外键指的是关联表的唯一键而不是主键。这是一个丑陋的关系模型。例如,假设 Product 类有一个唯一的序列号,它不是主键。 (unique 属性控制 NHibernate 使用 SchemaExport 工具生成 DDL。)

    【讨论】:

    • 感谢您的帮助。这看起来对我有用。我以前用过property-ref,但没有用过&lt;bag&gt;
    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 2011-08-31
    • 2014-04-19
    • 2011-08-08
    相关资源
    最近更新 更多