I've searched so many places and still can't find the a answer I'm looking for.
I'm using NHibernate 3.2 - Mapping by Code
I Have the following Mapping files:
public class ParentMapping: EntityMapping<Parent>
{
public ParentMapping()
{
Set(x => x.Children, map =>
{
map.Cascade(Cascade.All);
map.Inverse(true);
}, r => r.OneToMany());
}
}
public class ChildMapping: JoinedSubclassMapping<Child> // This is a subclass of something else.
{
public RequiredSkillMapping()
{
ManyToOne(x => x.Parent, map => { map.NotNullable(true); });
}
}
The cascade save works fine.
session.Save(parent) will save the children and associate them correctly.
When I try to call:
var parent = session.Get<Parent>(1);
parent.Children.Clear();
session.Save(parent); or session.SaveOrUpdate(parent) or session.Update(parent)
The entities stay associated to the parent.
I had it working by calling:
foreach(var child in parent.Children)
{
session.Delete(child);
}
parent.Children.Clear();
I was hoping that there would be a way to just save the parent?
Cheers,
James
Cascade means that operations on the parent are cascaded to the child.
The collection itself belongs to the parent, so changes on the collection are stored with the parent. But there is no reason to delete the children when they are not in the collection anymore. NH can't know if you need them for anything else.
There is cascade-delete-orphans.
map.Cascade(Cascade.All | Cascade.DeleteOrphans)
It means that items that had been in the collection, and are removed from the collection get deleted. It may be useful in your case. Note that it is not possible to use items for anything after removing from the collection. You can't even add them to another parent.
To make NH remove unused items automatically correctly in every case would require persistent garbage collection. This is highly inefficient and is not implemented by NH.
"foreach child delete" is OK. That's something you may have to do to tell NH when the children are not used anymore and need deletion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With