views:

175

answers:

2

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key.

Has anyone ever had a one-to-many where the child object gets inserted but not updated resulting in a row in my table with a null in the foreign key column?

I want the default behaviour for a standard one-to-many. I don't want to have to add the parent as a property to the child.

Thanks.

A: 

This would happen if you didn't have cascade="save-update" on your set/bag

or if you set your session's FlushMode to 'None' or 'Commit' and saved the child using your childRepository and neglected to save the object containing the collection using its repository.

reach4thelasers
A: 

I think you have to set parent reference in child item.

class Parent {
  public virtual IList<Child> Children;
}

class Child {
  public virtual Parent Parent;
}

Parent p = new Parent();
Child c = new Child();
c.Parent = p;
p.Children = new List<Child>();
p.Children.Add(c);

Now when you save this transient object p you will have the right foreign key in the child table.

Petr Felzmann