title: NHibernate - load 100.000 records each with a small lazy-loaded collection
body: <p>I have an app that has an object e.g. Person with a small list of custom attributes for each person. This collection is loaded lazily through nhibernate and 9/10 times it's only loaded for one person and not for a IList.</p>
  
  <p>This 1/10 times though I need to do an export to excel of all the people in the database and i need to include their custom attributes. So if I am correct about this (and given the mapping below) nhibernate will issue 1 query for 100.000 records and 100.000 queries for their custom attributes.</p>
  
  <p>Is there any way to issue 2 queries - 1 for the records and 1 for all the custom attributes - and have nhibernate create the objects correctly? or maybe just some way to avoid this N+1 problem?</p>
  
  <p>The underlying db is an SQL Server 2008 and the app is a ASP.NET MVC app</p>
  
  <p>Person mapping is</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt;   &lt;class name="Core.Person, Core" table="people" optimistic-lock="none"&gt;
  
      &lt;id name="ID" column="id" type="System.Guid" unsaved-value="00000000-0000-0000-0000-000000000000"&gt;
        &lt;generator class="guid" /&gt;
      &lt;/id&gt;
      &lt;many-to-one name="Company" column="company_id" not-null="true" fetch="join" /&gt;
  
      &lt;property name="Name" column="name" type="String" /&gt;
      &lt;property name="DoB" column="dob" type="DateTime" not-null="true" /&gt;
  
      &lt;map name="CustomFields" lazy="true" table="custom_fields" cascade="all-delete-orphan"&gt;
        &lt;key column="person_id" /&gt;
        &lt;index column="name" type="System.String" /&gt;
        &lt;element column="value" type="System.String" /&gt;
      &lt;/map&gt;
         &lt;/class&gt; &lt;/hibernate-mapping&gt;
  </code></pre>