Note that there are some explanatory texts on larger screens.

plurals
  1. POLazy loading not working for many-to-one relationship when mapping to a non-key field using property-ref
    primarykey
    data
    text
    <p>I have a legacy database that I am mapping using NHibernate. The objects of concern are an Account and a list of Notification objects. The objects look like: </p> <pre><code>public class Notification { public virtual int Id { get; set; } public virtual DateTime BatchDate { get; set; } /* other properties */ public virtual Account Account { get; set; } } public class Account { public virtual int Id { get; set; } public virtual string AccountNumber { get; set; } /* other properties */ } </code></pre> <p>The mapping files look like:</p> <pre><code>&lt;class name="Account" table="Account" dynamic-update="true"&gt; &lt;id name="Id" column="AccountID"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="AccountNumber" length="15" not-null="true" /&gt; &lt;!-- other properties --&gt; &lt;/class&gt; &lt;class name="Notification" table="Notification"&gt; &lt;id name="Id" column="Id"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;!-- other properties --&gt; &lt;many-to-one name="Account" class="Account" property-ref="AccountNumber" lazy="proxy"&gt; &lt;column name="AcctNum" /&gt; &lt;/many-to-one&gt; </code></pre> <p>However, when I create a criteria such as </p> <pre><code>return session.CreateCriteria(typeof(Notification)).List&lt;Notification&gt;(); </code></pre> <p>I am getting a Select N+1 case where each account is loaded even though the Account is never referenced. Why are all of the accounts getting loaded when the many-to-one is mapped as a lazy proxy?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload