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
In the context of my example project, I have a Post table and a Comment table. A Post has a one-to-many relationship with Comment. The PostMap is configured to use CollectionLazy.NoLazy for Comments.
I wrote four unit tests in the attached project to demonstrate an issue. I use Future/FutureValue to get Posts/Post, and I expected the related child Comments to be loaded and usable outside the session. However, it throws a LazyInitializationException.
I then tried another approach using QueryOver.List (which returns a list of entities) or QueryOver.SingleOrDefault, and in this case, I can use the Comments as expected.
Please see my comment below for the reason and discuss that. Thank you a lot.
I found where the Session handles NoLazy properties when executing a query from QueyOver.List(). It is in the Loader.cs#DoQueryAndInitializeNonLazyCollections() persistenceContext.InitializeNonLazyCollections();
And for QueryOver.FutureValue(), the process is totally different. We basically build a list of IQueryBatchItem from ICriteria. Then add to QueryBatch. Whenever we try to get a Value, the QueryBatch will loop through list List _queries, and use a different approach to load all itemsQueryBatch.Execute() => ExecuteBatched().
In conclusion, FutureValue uses a different approach to load data without loading NoLazyCollection directly as does with List().
I think we can improve FutureValue to correct that, it's not simple to do so I can not fix it by myself.
VoNhatSinh
changed the title
Future/FutureValue does not load NoLazy children
QueryOver.Future/FutureValue does not load NoLazy children like QueryOver.SingleOrDefault()/List()
Jul 15, 2024
Please see the attached file for a reproducible project (also includes database script, and unit test)
FutureDoesNotLoadNoLazyChildNHibernate.zip
Steps to reproduce:
In the context of my example project, I have a
Post
table and aComment
table. APost
has aone-to-many
relationship withComment
. ThePostMap
is configured to useCollectionLazy.NoLazy
for Comments.I wrote four unit tests in the attached project to demonstrate an issue. I use
Future
/FutureValue
to get Posts/Post, and I expected the related childComments
to be loaded and usable outside the session. However, it throws a LazyInitializationException.I then tried another approach using
QueryOver.List
(which returns a list of entities) orQueryOver.SingleOrDefault
, and in this case, I can use theComments
as expected.Please see my comment below for the reason and discuss that. Thank you a lot.
The text was updated successfully, but these errors were encountered: