title: Can I reattach a collection in NHibernate?
body: <p>I have objects of which some have Lists of Documents, other Objects, ... whatever. I do not want to use <code>lazy=false</code> on the relationship because it makes the application very slow. I can reattach objects with Lock, so other properties or one-to-many relationships load when they should. But for collections I always get "could not initialize a collection" if I try to access them. It does not work if I call <code>Lock(obj)</code> on the object that is connected to that location.</p>
  
  <p>I want my mapping to look like this:</p>
  
  <pre><code>&lt;set name="DocumentList" table="material_document" cascade="all" lazy="true"&gt;
    &lt;key column="material_id"/&gt;
    &lt;many-to-many class="Document"&gt;
      &lt;column name="document_id"/&gt;
    &lt;/many-to-many&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p>Is there a method to reattach? Or is there a mapping setting?</p>
  
  <p><strong>update1:</strong></p>
  
  <p>this is the mapping on the material side:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  &lt;class name="Material" table="material"&gt;
  &lt;!-- Defining PK --&gt;
  &lt;id name="Id" type="integer" column="id"&gt;
      &lt;generator class="sequence"&gt;
        &lt;param name="sequence"&gt;material_id_seq&lt;/param&gt;
      &lt;/generator&gt;
  &lt;/id&gt;
  &lt;!-- Defining Properties --&gt;
  &lt;!-- Defining FK-Relations --&gt;
  &lt;set name="DocumentList" table="material_document" cascade="all" lazy="false"&gt;
    &lt;key column="material_id"/&gt;
    &lt;many-to-many class="Document"&gt;
      &lt;column name="document_id"/&gt;
    &lt;/many-to-many&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>The Document mapping file does not have any information about the relationship. The database name is alright, everything else works. The tables are called <code>material</code>, <code>material_document</code> and <code>document</code>.  My classes are <code>public</code> and the properties <code>public virtual</code>. What can be wrong with my entities? Do you mean something with the data is wrong or something could have happened to the objects in the program...?</p>
  
  <pre><code>List material = LoadAllMaterials();
  //some code, that causes the session to be closed, a new session will be opened
  foreach (Document d in material.DocumentList) { //causes the exception
      //do something
  }
  </code></pre>
  
  <p>Before that, I want to fetch the documents, without loading them beforehand.</p>
  
  <p><strong>update 2:</strong>
  how I currently reattach objects:
  I set force true when I know there is a proxy and it doesn't work another way... And I have to check if it's in the session because evict throws an Exception if I call it on an object that is not in the session.</p>
  
  <pre><code>public void Reattach(Type type, BaseObject o, bool force)
  {
      bool inSession = o.IsInSession();
  
      if (force &amp;&amp; inSession)
      {
          GetSession().Evict(o);
          GetSession().Lock(type.ToString(), o, LockMode.None);
      }
      else {
  
          o = (BaseObject)GetSession().GetSessionImplementation().PersistenceContext.Unproxy(o);
  
          if (!inSession) {
  
              GetSession().Lock(type.ToString(), o, LockMode.None);
          }
      }
  }
  </code></pre>
  
  <p>and this is my IsInSession() method:</p>
  
  <pre><code>public virtual bool IsInSession() {
  
      ISession session = HibernateSessionManager.Instance.GetSession();
  
      var pers = session.GetSessionImplementation()
            .GetEntityPersister(GetType().ToString(), this);
  
      NHibernate.Engine.EntityKey key = new NHibernate.Engine.EntityKey(Id,
          pers, EntityMode.Poco);
  
      bool isInSession = false;
  
      try
      {
          object entity = session.GetSessionImplementation().PersistenceContext.GetEntity(key);
  
          if (entity != null)
          {
              isInSession = true;
          }
      }
      catch (NonUniqueObjectException) {}
  
      return isInSession;
  }
  </code></pre>