Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Cascade Delete on children when clearing a collection

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

like image 954
Jamez Avatar asked Dec 11 '22 23:12

Jamez


1 Answers

Cascade means that operations on the parent are cascaded to the child.

  • When inserting the parent, the children are inserted too.
  • When deleting the parent, the children are deleted too.
  • Update is special in NH, but it also means when calling update with the parent, that the children are "updated" too.
  • and so on

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.

like image 134
Stefan Steinegger Avatar answered Apr 06 '23 05:04

Stefan Steinegger