NHibernate: Object Persistence

Transitive Persistence

Summary

Transitive persistence is a technique to transfer persistence to transient and detached objects automatically.

An instance of an object will automatically become persistent if the application creates an object reference to that instance from another instance that is already persistent. This guarantees referential integrity.

Alternatively, NHibernate provides the ability to specify a cascade style for each association mapping, which offers more flexibility and control. To enable transitive persistence for a particular mapped association, override the default entity associations in metadata with one of the the following attributes:

Property Attribute Attribute Description
cascade="none" Default. NHibernate will ignore the association.
cascade="save-update" NHibernate will navigate the association when the transaction is committed and when an object is passed to Save() or Update(), and save newly instantiated transient instances and persist changes to detached instances.
cascade="delete" NHibernate will navigate the association and delete persistent instances when an object is passed to Delete().
cascade="all" NHibernate will cascade both save:update and delete, as well as calls to Evict and Lock.
cascade="all-delete-orphan" Same as cascade="all" but, in addition, NHibernate will delete any persistent entity instance that has been removed from the association.
cascade="delete-orphan" NHibernate will delete any persistent entity instance that has been removed from the association.