在 NHIBERNATE 中使用复合键进行多对一映射



我有以下nhibernate映射,我想将复合键值保存到其他实体的单独列中 时间表CellTransactionLine 具有一对一映射。 有什么想法吗? 以下不会在两个外键列中保存值传递 null。

<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
    assembly="ManpowerUK.Indigo.Domain"
    namespace="ManpowerUK.Indigo.Domain.Timesheets">
    <class name="CMSTimesheetCell" lazy="false" table="vw_TimesheetCell" mutable="false">
        <composite-id >
            <key-property name="TimesheetCellId"/>
            <key-property name="TimesheetCellVersion" column="Version"/>
        </composite-id>
        <property name="TimesheetId" not-null="true" />
        <property name="IsRemoved" />
    </class>
</hibernate-mapping>
        
        
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ManpowerUK.Indigo.Domain" namespace="ManpowerUK.Indigo.Domain">
  <class name="TimesheetCellTransactionLine" lazy="false">
      <id column="TransactionLineId"/>
      <many-to-one name="Timesheet" class="ManpowerUK.Indigo.Domain.Timesheets.TimesheetCellWrite" column="TimesheetId" not-null="true" lazy="proxy"/>
      <many-to-one name="CMSTimesheetCell" class="ManpowerUK.Indigo.Domain.Timesheets.CMSTimesheetCell" not-null="true" lazy="proxy" cascade="none">
        <column name="TimesheetCellId"/>
        <column name="TimesheetCellVersion"/>
      </many-to-one>
  </class>
</hibernate-mapping>

我通过添加额外的属性映射来插入组合键来解决

  <property name="TimesheetCellId" not-null="true" />
  <property name="TimesheetCellVersion" not-null="true" />

在获取时,我使用以下映射插入和更新 false 并且级联无。

 <many-to-one name="CMSTimesheetCell" class="ManpowerUK.Indigo.Domain.Timesheets.CMSTimesheetCell" not-null="true" lazy="proxy" cascade="none" insert="false" update="false">
    <column name="TimesheetCellId"/>
    <column name="TimesheetCellVersion"/>
  </many-to-one>

工作的完整映射文件如下。

     <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ManpowerUK.Indigo.Domain" namespace="ManpowerUK.Indigo.Domain">
  <class name="TimesheetCellTransactionLine"  lazy="false">
      <id column="TransactionLineId" />
      <property name="TimesheetCellId" not-null="true" />
      <property name="TimesheetCellVersion" not-null="true" />
           <many-to-one name="CMSTimesheetCell" class="ManpowerUK.Indigo.Domain.Timesheets.CMSTimesheetCell" not-null="true" lazy="proxy" cascade="none" insert="false" update="false">
        <column name="TimesheetCellId"/>
        <column name="TimesheetCellVersion"/>
      </many-to-one>
    </class>
</hibernate-mapping>

我将用利用按代码映射的解决方案来补充这个答案:

 mapping.Class<TimesheetCellTransactionLine>(classMapper =>
 {
      classMapper.ManyToOne(
            line => line.CMSTimesheetCell,
            columns
            (
                 columnMapper => columnMapper.Name("TimesheetCellId"),
                 columnMapper => columnMapper.Name("TimesheetCellVersion")
            )
      );
 });

相关内容

  • 没有找到相关文章

最新更新