Nhibernate: remove orphans but not delete children when parent is removed

I am looking for a way to configure nhibernate so that orphans are automatically deleted, but if the parent is deleted and the child exists, a request is made to delete the parent, but not for children. Basically, I would like to configure my cascading parameter as "save-update-orphan", but this is not supported.

<set name="children" inverse="true" cascade="all-delete-orphan" access="field"> <key column="ParentId" /> <one-to-many class="Parent" /> </set> 

The all-delete-forph syntax does not work for me, as it automatically deletes the children when the parent is deleted.

Update To try to be clearer ... When I explicitly delete a child from the parent collection, I want the child to be deleted. When I explicitly delete a parent, I do not want the children to leave.

+4
source share
2 answers

I do not think that you can do what you want through configuration.

The only option I can think of is to disable the removal of the orphan and manually remove the child when you want to remove it.

NHibernate Cascades: different between all, all-delete-orphans and save-update

Here is what each cascading option means:

  • none - do not do any cascades; let users process them themselves.
  • save-update - when saving / updating an object, check the settings and save / update any required object (including saving / updating assoications in many, many, many scenarios).
  • delete - when deleting an object, delete all objects in assoication.
  • delete-orphan - when an object is deleted, delete all objects in assoication. In addition to this, when an object is removed from proportionality, and not with another object (orphaned), also delete it.
  • all - when an object saves / updates / deletes, check the settings and save / update / delete all found objects.
  • all-delete-orphan - when an object saves / updates / deletes, check the settings and save / update / delete all found objects. In addition, when an object is deleted from a partition and not assoated with another object (orphaned), also delete it.
+4
source

Well, you have to delete the child if this is the one you want to delete. That is, do not try to delete the child through the parent unless you want the parent to be deleted.

0
source

Source: https://habr.com/ru/post/1387680/


All Articles