Quote:
but that doesnt work when a child collection is null which it is on a new object
i don't think that is correct for an IList. I thought I remember reading somewhere that ILists do allow the addition of an element if the collection is null. Even so, I (needlessly) initialize the collection in my constructor:
Code:
class ParentItem {
private int id;
private IList childItems;
public ParentItem() {
this.id = -1;
this.childItems = new ArrayList();
}
}
and, when creating the Parent -> Child relationship:
Code:
ParentItem item = new ParentItem();
ChildItem child = new ChildItem();
item.ChildItems.Add(child);
child.ParentItem = item;
session.SaveOrUpdate(item);
actually, personally, i would change the constructor for the ChildItem to require the parent parameter (since the child cannot normally exist without the parent), thereby automatically setting both sides of the relationship:
Code:
ParentItem item = new ParentItem();
item.ChildItems.Add(new ChildItem(item));
session.SaveOrUpdate(item);
don't forget, you really do need to manage both sides of the relationship. especially once you start implementing the second-level cache. if you do not set both sides of the relationship and the second-level cache is in play, your cached items will not be updated properly.
-devon