开发者

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

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 load开发者_Python百科ed (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


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.


What is the cascade setting for cascading changes from Child to the GrandChildren collection? I think NHibernate throws this exception if the collection is dirty but the cascade setting does not cause the changes to be persisted.


You can use session.Load to reference an existing instance of Child without making a trip to the db. This should do it, I think:

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

But check to make sure that the .Id call doesn't trigger a db trip.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜