简体   繁体   English

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

[英]many-to-one mapping with composite key in nhibernate

I have following nhibernate mapping , I want to save composite key values into separate column in other entity TimesheetCellTransactionLine with one to one mapping. 我有以下nhibernate映射,我想通过一对一映射将组合键值保存到其他实体TimesheetCellTransactionLine中的单独列中。 any ideas ? 有任何想法吗 ? following doesnt save the values passes null in both foreign key column. 以下不保存值在两个外键列中均传递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>

I solved by adding additional property mapping to insert the composite keys 我通过添加其他属性映射来插入组合键来解决

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

and while fetching I used following mapping with insert and update false and cascade none. 在获取时,我使用了以下映射,插入和更新为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>

full mapping file which works is as follows. 完整的映射文件的工作原理如下。

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

I'll complement this answer with the solution that utilises mapping-by-code: 我将利用利用代码映射的解决方案来补充这个答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM