Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: "collection was not processed by flush()" caused by lazy loading issue

Tags:

nhibernate

I have two classes:

class Parent
{
    public virtual Child Child { get; set; }
}

class Child 
{
    public virtual IList<GrandChild> GrandChildren { get; set; }
}

I have an instance of Parent loaded from my ISession, Parent.Child is lazy loaded (NOT loaded at this point). Child.GrandChildren is also lazy loaded.

If I do this:

session.Save(new Parent { Child = existingParent.Child } );

I get collection [Child.GrandChildren] was not processed by flush()

If I cause existingParent's Child property to be loaded, simply by accessing it:

var x = existingParent.Child.Name

the problem goes away. Why is this happening, and how do I solve it - preferably without having to change my fetching strategy?

**Edit: ** Parent has a FK to Child

I'm using NH 2.1.2.4000

Thanks

like image 381
Andrew Bullock Avatar asked Jul 06 '10 16:07

Andrew Bullock


1 Answers

I had a similar issue, the comment from @Jamie Ide helped me realized what the problem was. I was initializing the collection inside the constructor, which made NHibernate think that the collection was dirty, even if it wasn't required to save that specific object at that point.

The exception I got was: ClassName: ERROR | NHibernate.AssertionFailure: collection [CollectionName] was not processed by flush()

I still want to do this initialization, but I guess I have to find some other solution to that problem.

like image 141
Suh Avatar answered Nov 16 '22 00:11

Suh