1
votes

I have following nhibernate mapping , I want to save composite key values into separate column in other entity TimesheetCellTransactionLine with one to one mapping. any ideas ? following doesnt save the values passes null in both foreign key column.

<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>
2
Your TimesheetCellTransactionLine appears to be extracted from a bigger mapping, and the snippet you provide looks invalid and untested (subclass outside of a class definition). Can you write a mcve please?Frédéric

2 Answers

1
votes

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.

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

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")
            )
      );
 });