title: Initialize associated entity or collection fails with Stateless Session in NHibernate
body: <p>I'm trying to make an association with an entity from 3rd party dll. For some reasons, I have to use <code>StatelessSession</code>. With <code>StatefullSession</code> it works but not with Stateless. The idea is to allow 3rd party to add a table and have its data fetched when I query the main table.</p>
  
  <p>I have three projects: my main project, model project with the <code>IExtender</code> interface, and 3rd party project with <code>Extender</code> class (not referenced by the main project).</p>
  
  <p><strong>My data schema:</strong></p>
  
  <pre><code>Table Data
     Id INT identity,
     more fields...
  
  Table Extender
     Id INT PK, FK from Data
     Name NVARCHAR
     more fields...
  </code></pre>
  
  <p><strong>Model project code:</strong></p>
  
  <pre><code>public interface IExtender
  {
      int Id { get; set; }
  }
  </code></pre>
  
  <p><strong>Main project code:</strong></p>
  
  <pre><code>public class Data
  {
      public virtual int Id { get; set; }
      public IExtender Extender { get; set; }
      //public IList&lt;IExtender&gt; Extenders { get; set; }
  }
  </code></pre>
  
  <p><strong>Main project hbm:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="..." assembly="..." auto-import="true"&gt;
  
  &lt;class name="Data" table="Data" lazy="false"&gt;
      &lt;id name="Id" column="Id" type="int" &gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;one-to-one name="Extender" foreign-key="Id" 
             class="Model.IExtender, Model" lazy="false"/&gt;
  
  &lt;!--&lt;bag name="Extenders" cascade="all" inverse="true" lazy="false"&gt;
    &lt;key column="Id" /&gt;
    &lt;one-to-many class="Model.IExtender, Model"/&gt;
  &lt;/bag&gt;--&gt;
  
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>3rd party project:</strong></p>
  
  <pre><code>public class Extender : IExtender
  {
      public virtual int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><strong>3rd party hbm:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model" assembly="Model" auto-import="true"&gt;
  
    &lt;class name="IExtender" table="IExtender" lazy="false" abstract="true"&gt;
  
      &lt;id name="Id" column="Id" type="int" &gt;
        &lt;generator class="assigned" /&gt;
      &lt;/id&gt;
  
      &lt;union-subclass table="Extender" name="Extension.Extender, Extension" lazy="false"&gt;
        &lt;property name="Name" column="Name" not-null="true" type="String" /&gt;
      &lt;/union-subclass&gt;
  
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>3rd party project puts its dll in the main project bin folder.</p>
  
  <p>Now my code to get the data looks like:</p>
  
  <pre><code>var sessionFactory = new Configuration().Configure()
     .AddAssembly(Assembly.LoadFile(System.Environment.CurrentDirectory + @"\Extender.dll"))
     .AddAssembly(Assembly.GetExecutingAssembly())
     .BuildSessionFactory();
  
  var session = sessionFactory.OpenStatelessSession();
  var criteria = session.CreateCriteria&lt;Data&gt;();
  var data = criteria.List&lt;Data&gt;().ToList();
  Console.WriteLine(data.First().Extender.Id); // fails because Extender is null
  </code></pre>
  
  <p>If I use <code>StatefullSession</code>, it is working fine with a single entity or a bag. If I use <code>StatelessSession</code> and a bag instead (and a list as the commented rows above), I get an error:</p>
  
  <blockquote>
    <p>collections cannot be fetched by a stateless session</p>
  </blockquote>
  
  <p>If I remove the <code>lazy="false"</code> from the bag, I get this error (no matter if the collection is virtual or not):</p>
  
  <blockquote>
    <p>Initializing[MyProject.Data#1]-failed to lazily initialize a
    collection of role: MyProject.Data.Extenders, no session or session
    was closed</p>
  </blockquote>
  
  <p><strong>EDIT:
  I upgraded my NHibernate version to the latest and now it is working when I use a bag, but when I use a single entity with one-to-one relation it is always null.</strong></p>
  
  <p>By the way, no matter if there is an error or not, I see in the final sql query that <code>Extender</code> table is being joined properly.</p>
  
  <p>Thanks for your help.</p>