Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate executing extraneous select statements
    primarykey
    data
    text
    <h3>Problem</h3> <p>When executing a get for an entity with a many-to-one relationship with outer join set to true, not-found set to ignore and the entity row on the one side does not exist, an extra select is executed by NHibernate trying to load it even though the previous select which was just executed could not find it.</p> <h3>Question</h3> <p>Why is this extra select executed and is there any way to suppress it?</p> <h3>Context</h3> <p>I am working with a legacy database with no foreign key constraints where I have zero control over the schema. Modifying it is unfortunately out of the question. Keep in mind my actual mappings are more complex than this. This occurs several times for the actual entity and they are often loaded by <code>ICriteria.List&lt;T&gt;()</code> and not <code>Session.Load&lt;T&gt;(id)</code>, resulting in alot of unnecessary queries and a big performance hit.</p> <h3>Simplified Example</h3> <hr> Code <pre><code>ISession Session = ... ... Session.Get&lt;Bid&gt;(1); ... </code></pre> Executed SQL <pre><code>SELECT bid.Id, bid.ItemId, item.Id FROM Bid bid left outer join Item item on bid.ItemId=item.Id WHERE bid.Id=@p0;@p0 = 1 SELECT item.Id FROM Item item WHERE item.Id=@p0;@p0 = 222 </code></pre> Mapping <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Example" namespace="Example"&gt; &lt;class name="Bid"&gt; &lt;id name="Id"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;many-to-one name="Item" column="ItemId" outer-join="true" not-found="ignore" /&gt; &lt;/class&gt; &lt;class name="Item"&gt; &lt;id name="Id"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> Bid Table Contents <pre><code>Id ItemId 1 222 </code></pre> Item Table Contents <pre><code>Id 333 444 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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