Why does NH re-initializing a collection?

22 views
Skip to first unread message

HappyNomad

unread,
Dec 8, 2012, 8:33:47 PM12/8/12
to nhu...@googlegroups.com
In AbstractPersistentCollection, there is this method:

        /// <summary> Called by the <tt>Count</tt> property</summary>
        protected virtual bool ReadSize()
        {
            if (!initialized)
            {
                if (cachedSize != -1 && !HasQueuedOperations)
                {
                    return true;
                }
                else
                {
                    ThrowLazyInitializationExceptionIfNotConnected();
                    CollectionEntry entry = session.PersistenceContext.GetCollectionEntry(this);
                    ICollectionPersister persister = entry.LoadedPersister;
                    if (persister.IsExtraLazy)
                    {
                        if (HasQueuedOperations)
                        {
                            session.Flush();
                        }
                        cachedSize = persister.GetSize(entry.LoadedKey, session);
                        return true;
                    }
                }
            }
            Read();
            return false;
        }

Notice that the call to Read() if the collection is already initialized.  Read() calls Initialize() which calls InitializeCollection().  Is it just me, or does this indeed make no sense?  Why would you initialize a collection that is already initialized?  I am asking because this unneeded activity by NH is causing an issue in my app.  The scenario arises when I try to find out the Count of a collection that was just been read from the database.

HappyNomad

unread,
Dec 8, 2012, 9:37:18 PM12/8/12
to nhu...@googlegroups.com

My collection was in fact NOT initialized!  I was passing the wrong property name to ICriteria.SetFetchMode, which should probably be refactored to take a lambda expression instead of a string as to avoid such problems.  Well, I still think the NH code I posted is confusing but at least it works.

Oskar Berggren

unread,
Dec 10, 2012, 3:52:26 AM12/10/12
to nhu...@googlegroups.com
2012/12/9 HappyNomad <happyn...@gmail.com>:
>
> My collection was in fact NOT initialized! I was passing the wrong property
> name to ICriteria.SetFetchMode, which should probably be refactored to take

This is what the QueryOver API is for.

/Oskar
Reply all
Reply to author
Forward
0 new messages