title: NHibernate Many to One / One to One with differing keys
body: <p>Hey all,  I'm kicking the tires on NHibernate and have a conoundrum I have been scratching my head over for a bit now, working with a legacy database with some fairly complex relationships.</p>
  
  <p>ClaimRoot has a primary key of a claimGUID.
  ClaimRoot has a bag of Claimdetails associated by claimGUID (this works a treat).</p>
  
  <p>The problem is that ClaimRoot also has an optional one to one relationship with ClaimFinancials (not all ClaimRoots have ClaimFinancials, but most do).  But the PK for ClaimFinancials is a FormID field.  This field exists in the ClaimRoot, but is not the PK.</p>
  
  <p>I've posted a mapping below with extra columns removed to protect the innocent.</p>
  
  <pre><code>  &lt;class name="ClaimRoot" table="tbl_ClaimRoot" schema="DB1.dbo"&gt;
      &lt;id name="ClaimGUID"&gt;
        &lt;generator class="guid"/&gt;      
      &lt;/id&gt;
      &lt;property name="FormID" /&gt;
      &lt;property name="LastFormNoteText" /&gt;
      &lt;bag name="ClaimDetails" inverse="true"&gt;
        &lt;key column="ClaimGUID"/&gt;
        &lt;one-to-many class="ClaimDetails"/&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  
    &lt;class name="ClaimDetails" table="tbl_ClaimDetails" schema="DB2.dbo"&gt;
      &lt;id name="RowID"&gt;
        &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;property name="ClaimGUID" /&gt;
      &lt;property name="SeqNo"/&gt;
      &lt;property name="B1A_InsID" /&gt;
      &lt;many-to-one name="Root" column="ClaimGUID" foreign-key="ClaimGUID"/&gt;
    &lt;/class&gt;
  
  
    &lt;class name="ClaimFinancials" table="tbl_ClaimFinancials" schema="DB1.dbo"&gt;
      &lt;id name="FormID"&gt;
        &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;property name="CreatedDate"/&gt;
      &lt;property name="SubmittedDate" /&gt;
    &lt;/class&gt;
  </code></pre>
  
  <p>Thanks in advance!
  -Bob</p>