tags:

views:

39

answers:

2

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

A: 

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.

Jamie Ide
ive not made any changes hence why its not loaded?i just want to make a new `parent`, with the same child as an existing `parent`. cascade for hasone is `saveupdate` and for hasmany is `alldeleteorphan`
Andrew Bullock
Are you initializing the GrandChildren collection in Child's constructor? Please show the mappings.
Jamie Ide
A: 

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.

Gabe Moothart