title: nHibernate query exception: no row with given identifier exists
body: <p>After modifying the datamodel a little bit, certain queries started giving me this exception. However, even after reading various forum-threads and blogs, I don't understand why this happens in my case. So I hope that someone here can help. First, an example query:</p>
  
  <pre><code>var criteria = Session.CreateCriteria(typeof(SomeEntity));
          criteria.Add(Expression.IdEq(id));
          criteria.SetFetchMode("_PrivateProperty", FetchMode.Eager);
          criteria.CreateAlias("PublicProperty", "alias");
          criteria.Add(Expression.Eq("alias.Id", aliasId));
  </code></pre>
  
  <p>If I modify the query by either removing (1) SetFetchMode or (2) CreateAlias for the public property, everything works fine.</p>
  
  <p>The private property is usually loaded lazily, but in this case I want to load it together with its parent entity to combat select N+1.</p>
  
  <p>So, why doesn't it work as displayed above, and does it work when I remove certain parts of the query?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Generated SQL:</p>
  
  <pre><code>SELECT * FROM SomeEntity this_ 
  inner join PublicProperty alias4_ on this_.SomeEntityId=alias4_.SomeEntityId 
  inner join ReferencedProperty rp2_ on alias4_.RPId=rp2_.RPId 
  left outer join PrivateProperty v1_ on this_.SomeEntityId=v1_.SomeEntityId 
  WHERE this_.SomeEntityId= @p0 and rp2_.RPId= @p1
  </code></pre>
  
  <p>When run in SQL Server Management Studio, it works as expected. I get two results. In between modifying the datamodel and the query, the data in the database hasn't changed.</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Modification in model.
  Original mapping:</p>
  
  <pre><code>References(d =&gt; d.PublicProperty, "PublicPropertyId").Cascade.SaveUpdate();
  </code></pre>
  
  <p>New mapping:</p>
  
  <pre><code>HasManyToMany(d =&gt; d.PublicPropertys)
              .Access.CamelCaseField(Prefix.Underscore)
              .Table("SomeEntityPublicProperty")
              .ParentKeyColumn("SomeEntityId")
              .ChildKeyColumn("PublicPropertyId")
              .Cascade.SaveUpdate()
              .Inverse();
  </code></pre>
  
  <p><strong>UPDATE3</strong></p>
  
  <pre><code>HasMany&lt;PrivatePropertyEntity&gt;(Reveal.Member&lt;SomeEntity&gt;("_PrivateProperty"))
              .Table("SomeEntity_PrivateProperty")
              .KeyColumn("SomeEntityId")
              .Cascade.AllDeleteOrphan()
              .LazyLoad().Inverse();
  </code></pre>