NHibernate executes external select commands

Problem

When executing get for an object with a many-to-one relationship with an external connection set to true, the set to ignore was not found, and the entity string does not exist on one side, NHibernate makes an additional choice, trying to load it, even if the previous select that was just executed could not find it.

Question

Why is this additional choice made and is there a way to suppress it?

Context

I work with an outdated database without foreign key restrictions when I have zero control over the schema. Unfortunately, there is no way to modify this. Keep in mind that my actual comparisons are more complicated than that. This happens several times for the actual object, and they are often loaded by ICriteria.List<T>() rather than Session.Load<T>(id) , which leads to a lot of unnecessary queries and a big result.

Simplified example


The code
 ISession Session = ... ... Session.Get<Bid>(1); ... 
SQL executed
 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 
mapping
 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Example" namespace="Example"> <class name="Bid"> <id name="Id"> <generator class="native"/> </id> <many-to-one name="Item" column="ItemId" outer-join="true" not-found="ignore" /> </class> <class name="Item"> <id name="Id"> <generator class="native"/> </id> </class> </hibernate-mapping> 
Bid Table Content
 Id ItemId 1 222 
Table of Contents Table of Contents
 Id 333 444 
+4
source share
2 answers

OK, it looks like this is definitely a bug in NHibernate, as you can see here . At the moment, it is marked as insignificant, so I think that voting can lead to his profile and get a correction.

+1
source

Without setting this value, I would suggest that the key should replace external-join = "true" with fetch = "join" in a one-on-one association. By default, the select mode for the one-to-one association is "select", and I suspect this causes a selection.

If you change this mapping:

  <many-to-one name="Item" column="ItemId" fetch="join" not-found="ignore" /> 

he should behave as you expect.

0
source

All Articles