I have a parent class that contains a list of children. I have the parent and child mapped bidirectional with a has-many and an inverse on the parent with cascade.all turned on. If I modify an object in the child list, but no property on the parent, nHibernate does not save the child. If I modify a property on the parent everything saves fine. Is this by design or is there a special property I need to set?
1
votes
Do you have the child mapped in the parent's mapping, the parent mapped in the child's mapping, or both?
– hackerhasid
both as follows: (Parent) HasMany<Initiative>(x => x.Initiatives).KeyColumns.Add("ProjectId").AsBag().Cascade.SaveUpdate().LazyLoad().Inverse(); (Child) References<Domain.Project>(x => x.ParentProject).Column("ProjectId").Cascade.SaveUpdate();
– j rigs
I believe that you must call save on the Parent in order for it to save the Children.
– snicker
Thanks, but I am calling save on the parent. The issue is that if a do not modify a simple property on the parent the save for the child isn't fired. If I modify a simple property on the parent the child changes save fine.
– j rigs
2 Answers
0
votes
This might have something to do with the way you are adding the children to the collection. In a bidirectional, you have to manage both sides of the relationship in the code. Consider the example from the Fluent Nhibernate Getting Started Guide. Check the Store Entity.
A Store has many Employees. The Staff property of Store is collection of Employees. The relationship is setup as bidirectional.
Store has the following method
public virtual void AddEmployee(Employee employee)
{
employee.Store = this;
Staff.Add(employee);
}
As you can see, the childs Parent property needs to be set to the parent object. If this is not done, then Nhibernate will not be able understand who the parent of the child is and cannot automatically save the child if only the child is modified and the SaveOrUpdate(parent) is called.
You need to do both.