title: NHibernate - can't get both sides of a OneToMany relationship
body: <h1>Context</h1>
  
  <p>We have Users and Invites. A User can have many associated Invites, an Invite is associated with one user.</p>
  
  <p>I'm representing this via a column <code>usersk</code> on the <code>invite</code> table that corresponds to the <code>psk</code> primary key column on the <code>user</code> table. </p>
  
  <p>There is also a concept of a Match, but that is only relevant to this problem to the extent that an Invite's primary key is a composite of its User foreign key and its Match foreign key.</p>
  
  <p>I want to end up with:</p>
  
  <ol>
  <li>User has a property that is a collection of Invites they're involved in</li>
  <li>Invite has a property that is the User it's associated with</li>
  </ol>
  
  <p>At a high level, my approach is creating a Set of the Invites tied to a User via a OneToMany relationship, and then having a ManyToOne mapping from the Invite to the User.</p>
  
  <h1>The Error</h1>
  
  <p>Here's my ManyToOne setup:</p>
  
  <pre><code>ManyToOne(x =&gt; x.User, map =&gt; { 
      map.PropertyRef("PSK");  
  });
  </code></pre>
  
  <p>Throws this error:</p>
  
  <pre><code>{"ERROR: 42703: column invit0_.user does not exist"}
  </code></pre>
  
  <p>Well that's correct, because there's no user column. But trying to specify what column to use gives me a different error:</p>
  
  <pre><code>ManyToOne(x =&gt; x.User, map =&gt; { 
      map.PropertyRef("PSK"); 
      map.Column("usersk"); 
  });
  </code></pre>
  
  <p>throws an error:</p>
  
  <pre><code>{"Error performing LoadByUniqueKey[SQL: SQL not available]"}
  </code></pre>
  
  <p>due to inner exception:</p>
  
  <pre><code>{"The given key was not present in the dictionary."}
  </code></pre>
  
  <h1>Tools</h1>
  
  <p>MVC4 WebApi, NHibernate Conformist ByCode mapping</p>
  
  <h1>Code</h1>
  
  <p>If I just try to get the first goal accomplished, creating a Set of Invites for a property on the User, things work swimmingly. That code is below:</p>
  
  <p>User.cs</p>
  
  <pre><code>public class User {
      public User() { }
      public virtual int PSK { get; set; }
  
      public virtual ICollection&lt;Invite&gt; Invites { get; set; }
  }
  </code></pre>
  
  <p>UserMap.cs</p>
  
  <pre><code>public class UserMap : ClassMapping&lt;User&gt;
  {
      public UserMap()
      {
          Lazy(true);
  
          Schema("public");
          Table("user");
          Id(x =&gt; x.PSK, map =&gt;
          {
              map.Column("psk");
              map.Generator(Generators.Sequence, g =&gt; g.Params(new {sequence = "user_psk_seq"}));
          });
  
          Set(x =&gt; x.Invites, 
              mapping =&gt;
              {
                  mapping.Key(k =&gt;
                  {
                      k.Column("usersk");
                  });
                  mapping.Inverse(true);
              },
              r =&gt; r.OneToMany());
      }
  }
  </code></pre>
  
  <p>Invite.cs</p>
  
  <pre><code>public class Invite {
      public virtual int MatchSK { get; set; }
      public virtual int UserSK { get; set; }
      public virtual User User { get; set; }
  
      #region NHibernate Composite Key Requirements
      public override bool Equals(object obj) {
          if (obj == null) return false;
          var t = obj as Invite;
          if (t == null) return false;
          if (MatchSK == t.MatchSK
           &amp;&amp; UserSK == t.UserSK)
              return true;
  
          return false;
      }
      public override int GetHashCode() {
          int hash = GetType().GetHashCode();
          hash = (hash * CONSTANT) ^ MatchSK.GetHashCode();
          hash = (hash * CONSTANT) ^ UserSK.GetHashCode();
  
          return hash;
      }
      #endregion
  }
  </code></pre>
  
  <p>InviteMap.cs</p>
  
  <pre><code>public class InviteMap : ClassMapping&lt;Invite&gt; {
      public InviteMap() {
          Lazy(true);
  
          Schema("public");
          Table("invite");
          ComposedId(compId =&gt;
              {
                  compId.Property(x =&gt; x.MatchSK, m =&gt; m.Column("matchsk"));
                  compId.Property(x =&gt; x.UserSK, m =&gt; m.Column("usersk"));
              });
  
          ManyToOne(x =&gt; x.User, map =&gt; { 
              map.PropertyRef("PSK");  
          });
      }
  }
  </code></pre>
  
  <p>With this in place, fetching a User gives me a populated Invites collection exactly as expected, provided I comment out the ManyToOne code in InviteMap.</p>
  
  <h1>Conclusion</h1>
  
  <p>Help? I'm really hoping that this is some obvious conceptual issue I've missed. But... feel free to disappoint me on that front. </p>
  
  <p>Thanks for your time.</p>