You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The B class is a base class from which several other classes derive, so I’m using the "no-proxy" attribute value. While using the SQL Profiler, I’ve noticed that the following code:
Will generate a first SQL query to fetch the corresponding A instances, but will then make many queries to fetch B instances. Upon further inspection, I found that:
The problem does not appear if I use the default lazy-loading behaviour.
Not all B instances are fetched: only those that appear more than once in the collection returned by the LINQ query are eager loaded.
I looked in the NHibernate source and here is what seems to happen:
For an instance of B with the Id "123" that appears twice in the collection returned by the LINQ query, a first call is made to NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad. The method contains the following code:
// look for a proxyobjectproxy= persistenceContext.GetProxy(keyToLoad);if(proxy!=null){return ReturnNarrowedProxy(@event, persister, keyToLoad, options, persistenceContext, proxy);}else{if(options.IsAllowProxyCreation){return CreateProxyIfNecessary(@event, persister, keyToLoad, options, persistenceContext);}else{// return a newly loaded objectreturn Load(@event, persister, keyToLoad, options);}}
Here, the proxy variable is null, so CreateProxyIfNecessary is called. However, CreateProxyIfNecessary will execute the following line of code:
It appears that Unwrap is set to true for properties with the lazy="no-proxy" attribute (the logic is apparently in the ClassBinder.InitLaziness(HbmLaziness? laziness, ToOne fetchable, bool defaultLazy) method).
Ultimately, the call to GetImplementation will call the following line of code:
This will then result in a SQL query to fetch the element.
Is there something that mandates that these elements have to be eager loaded when they appear more than once in a collection, or is it an incorrect behavior ?
The text was updated successfully, but these errors were encountered:
Hello,
I'm currently working on a project in the following environment:
I have an XML mapping file that looks like this:
The B class is a base class from which several other classes derive, so I’m using the "no-proxy" attribute value. While using the SQL Profiler, I’ve noticed that the following code:
Will generate a first SQL query to fetch the corresponding A instances, but will then make many queries to fetch B instances. Upon further inspection, I found that:
I looked in the NHibernate source and here is what seems to happen:
For an instance of B with the Id "123" that appears twice in the collection returned by the LINQ query, a first call is made to NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad. The method contains the following code:
Here, the proxy variable is null, so CreateProxyIfNecessary is called. However, CreateProxyIfNecessary will execute the following line of code:
Which means that on the second call to ProxyOrLoad, the proxy variable is not null and ReturnNarrowedProxy is called, which contains this code:
It appears that Unwrap is set to true for properties with the lazy="no-proxy" attribute (the logic is apparently in the ClassBinder.InitLaziness(HbmLaziness? laziness, ToOne fetchable, bool defaultLazy) method).
Ultimately, the call to GetImplementation will call the following line of code:
This will then result in a SQL query to fetch the element.
Is there something that mandates that these elements have to be eager loaded when they appear more than once in a collection, or is it an incorrect behavior ?
The text was updated successfully, but these errors were encountered: