Skip to content

Exception "collection is not associated with any session" when using Future to load an entity with NoLazy children #3424

Closed
@lpnam0201

Description

@lpnam0201

Please see attached file for reproducible project (also included database script)

Step to reproduce:
In the context of my example project I have a Post and a Comment table.
A Post has a one-to-many relationship to Comment
PostMap is also configured to use CollectionLazy.NoLazy against Comments

Step 1. I try to query a Post, then retrieve it via a FutureValue, which works.
Step 2. Do a session.Clear()
Step 3. Later on (still in the same Session), I tried another query but it throws an exception:

Unhandled exception. NHibernate.HibernateException: collection is not associated with any session
   at NHibernate.Collection.AbstractPersistentCollection.ForceInitialization()
   at NHibernate.Engine.StatefulPersistenceContext.InitializeNonLazyCollections()
   at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces)
   at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
   at NHibernate.Loader.Criteria.CriteriaLoaderExtensions.LoadAllToList[T](IList`1 loaders, ISessionImplementor session)
   at NHibernate.Impl.SessionImpl.List[T](CriteriaImpl criteria)
   at NHibernate.Impl.CriteriaImpl.List[T]()
   at NHibernate.Criterion.QueryOver`1.List()
   at NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List()
   at NoLazyNHibernate.Program.Main(String[] args) in

After step 1, when inspecting the session.GetSessionImplementation().PersistenceContext, I found that it has 1 item in the private field nonLazyCollections, which doesn't seem to go away even if session.Clear() is used.
It seems loading a Post using Future will cause NHibernate to leave a dangling NonLazyCollections (for Comment entity) in its StatefulPersistenceContext.

Please help check. Thanks.

NoLazyNHibernate.zip

Note:
There's a different in implementation I found when comparing Java's Hibernate implementation with NHibernate's implementation.

In the same StatefulPersistenceContext.clear(), NHibernate doesn't clear the nonlazyCollections, but Hibernate does. I'm wondering if this is a bug or is it intended ?

Hibernate:
https://github.com/hibernate/hibernate-orm/blob/9e4a56c651a7a2a5c373aeb670ceb569b36d1e67/hibernate-core/src/main/java/org/hibernate/engine/internal/StatefulPersistenceContext.java#L253

NHibernate:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions