NHibernate: inverse, cascade

playing-nhibernate-inverse-and-cascade,

nhibernate-inverse

bidirectional associations

In database, there may be biodirectional relationships, e.g. Parent has multiple child, and Child has a parent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#### class definition
class Parent:
- String id
- IList<Child> childs
class Child:
- String id
- Parent parent

#### db definition
table Parent:
- id
table Child:
- id
- parentId

Inverse

Inverse focus on the association. It defines which side is responsible of the association maintenance (create, update, delete), that is, the assignment of parentId column. It doesn’t care about the maintenance of associated objects which is what cascade cares).

By default, invert=false, then the assignment of parentId is maintened when parent is created/updated/deleted. If we set parent.child.inverse=true and the child.parent is not-null, then the assignment of parentId is maintened when child is created/updated/deleted.

many-to-one is always inverse="false" (the attribute does not exist), that is, it means nothing to set child.parent.inverse=true

Cascade

cascade instead will focus on the associated objects. It defines if the current object is responsible of the maintenance of associated objects.

By default, cascade=None, that is, when saving parent, the childs on parent won’t be saved cascadelly.

See cascade stackoverflow

  • none - do not do any cascades, let users handle them by themselves.
  • save-update - when the object is saved/updated, check the associations and save/update any object that requires it (including save/update the associations in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the association.
  • delete-orphan - when the object is deleted, delete all the objects in the association. In addition, when an object is removed from the association and not associated with another object (orphaned), also delete it.
  • all - when an object is save/update/delete, check the associations and save/update/delete all the objects found.
  • all-delete-orphan - when an object is save/update/delete, check the associations and save/update/delete all the objects found. In additional to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.