Parent foreign key is null when saving children cascaded and parent has assigned id.

Description

I have a one-to-many relationship between Parent and Child.

The relevant part of the mappings:
Parent.hbm.xml:

<id name="Id" column="ParentId" unsaved-value="none">
<generator class="assigned" />
</id>

<bag name="Children" access="field.camelcase" lazy="true" cascade="all">
<key column="ParentId"/>
<one-to-many class="Foo.Child, Foo"/>
</bag>

Child.hbm.xml:

<id name="Id" column="ChildId" unsaved-value="0">
<generator class="native" />
</id>

<many-to-one name="Parent" class="Foo.Parent, Foo" column="ParentId" not-null="true"/>

When trying to Save() the Parent with Children, NHibernate cascades down to the children and tries to insert the children first, but sets the foreign key parameter (ParentId) to null. Of course, this violates the NOT NULL constraint in the database.

I played around with inverse, but I don't think it is related to the issue. I know if I' save the Parent without children first and then add the children and Flush(), it would work (I tried it before).

Might be a problem with the mapping but I couldn't figure out the solution (looked at source, forums, etc.)

Environment

.NET Framework 1.1, NHibernate beta-0.7
100% Done
Loading...

Activity

Show:

Donald Mull 
December 20, 2005 at 6:11 PM

My co-worker discovered tht it was when you don't set the parent reference on the child as in:

You do:
parent.Children.Add(newChild);

But you DON'T do:
child.Parent = parent;

Cascade should take care of this situation though.

Former user 
December 20, 2005 at 2:17 PM

This must be a different problem then, possibly solvable using inverse and/or cascades.

Donald Mull 
December 20, 2005 at 2:09 PM

A comment on the old forum from Mike Doerfler: 'change the Child id to something like a "guid" or "guid.comb" or some other strategy that doesn't require a db insert to get the id. '

This is still a problem when using guid or guid.comb (guid.comb in my case).

Former user 
May 12, 2005 at 12:19 PM

Need to do something about it, but probably after 1.0.

GaborG 
March 2, 2005 at 5:36 PM

If this is expected (that's how it works), is there any way to tell NHibernate to behave in the right way.. imho eliminating the constraint is not an option and also, if I'm working in a schema that is set (this the case), you can't really change the type of the id.

Unfortunately, I also can't save the parent first without any children because both the parent and its children get created and initialized in the part of the business layer where there's no access to the data layer by design.

If there are no other solutions, I will add an nhibernate option to my dogfood version of NHibernate 0.7 that does not cascade into one-to-many relationships but inserts first.

Won't Fix

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created March 1, 2005 at 7:40 AM
Updated September 26, 2006 at 12:24 AM
Resolved September 26, 2006 at 12:24 AM
Who's Looking?