title: NHibernate mapping one to many not on primary key
body: <p>I'm working with a legacy database, and I have the following tables:</p>
  
  <pre><code>Person                  ExternalFile
  ------                  ------------
  Id           (int, PK)  Key           (string)
  ConnectionId (int)      Type          (int)
  Name         (string)   ConnectionId  (int)
  Firstname    (string)   Path          (string)
                          Id            (int, PK)
  </code></pre>
  
  <p>A personal has many external files.</p>
  
  <p>Example given:</p>
  
  <pre><code>Person A                ExternalFile1          ExternalFile2
  --------                -------------          -------------
  Id: 1                   Key: 'WN'              Key: 'WN'
  ConnectionId: 29        Type: 4                Type: 4
  Name: 'Foo'             ConnectionId: 29       ConnectionId: 29
  Firstname: 'Bar'        Path: 'C:/file1.txt'   Path: 'D:/file2.txt'
                          Id: 1                  Id: 2
  </code></pre>
  
  <p>Can I map this so that a user has a bag of ExternalFiles?</p>
  
  <p>My mappingfiles</p>
  
  <p>ExternalFile:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Not.Relative" assembly="Not"&gt;
    &lt;class name="ExternalFile" table="tbl_externalfiles" lazy="false"&gt;
      &lt;id name="Id" column="`Id`"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;property name="Key" column="`CDKey`" /&gt;
      &lt;property name="ConnectionId" column="`KeyValue`" /&gt;
      &lt;property name="Type" column="`DocType`"  /&gt;
      &lt;property name="Path" column="`Path`" /&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Person:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Not.Relative" assembly="Not"&gt;
    &lt;class name="Person" table="tbl_person" lazy="false"&gt;
      &lt;id name="Id" column="`Id`"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;property name="ConnectionId" column="`pairid`" /&gt;
      &lt;property name="Name" column="`name`" /&gt;
      &lt;property name="Firstname" column="`firstname`"  /&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>My query would look like this:</p>
  
  <pre><code>SELECT * 
  FROM tbl_externalfiles
  WHERE KeyValue = @p0
  AND   CDKey = @p1
  AND   DocType = @p2
  
  @p0 = 29, @p1 = 'WN', @p2 = 4
  </code></pre>
  
  <p>So I have to give 3 parameters to the bag from persons to External files,</p>
  
  <p>1: the ConnectionID of the person</p>
  
  <p>2: 'WN' &lt;-- always the same (the key)</p>
  
  <p>3: 4 &lt;-- always the same  (the type)</p>
  
  <p>On a many to one this works when I map to a composite ID, but I can't use that in this case.</p>
  
  <p>Is a bag even possible here? Should I just use an extra query to fetch my ExternalFiles where I use a criteria to query.</p>
  
  <p>EDIT:</p>
  
  <p>I know I can do the following for only the connectionId</p>
  
  <pre><code>&lt;bag name="Files" lazy="false"&gt;
    &lt;key column="KeyValue" property-ref="ConnectionId"/&gt;
    &lt;one-to-many class="ExternalFile" /&gt;
  &lt;/bag&gt;
  </code></pre>
  
  <p>But I don't know how to get the Type and Key parameters in there aswell.</p>
  
  <p>Thanks</p>