Printed from A Developer website designed for Developers

NHibernate: Object Persistence

Persistent Objects

In NHibernate the ISession interface is responsible for managing object state. For this reason, the ISession object is often referred to as the Persistence Manager.

NHibernate defines three distinct states for any object: Transient, Persistent and Detached.




NHibernate Scope of Object Identity

NHibernate persistent objects are identifiable in two ways:

The conditions under which .NET identity is equivalent to the database identity is referred to as the scope of object identity.

NHibernate uses the .NET identity (memory location of the object / value of the object) and the database identity (location in the database) to associate the object with the corresponding data row in the database.

There are three common type of scope:

The NHibernate Persistent Manager (ISession) typically makes use of transaction-scope identity.

Compare Persistent Objects

The comparison of persistent objects comes into play when attaching objects that have been previously detached from various ISession instances into a new ISession instance.

NHibernate can examine the identifier of a transient or detached object on reattachment and treat the object appropriately.

.NET equality is defined by the implementation of the Equals() and GetHashCode() methods in the domain model's persistent classes. The Equals() and GetHashCode() methods can be easily overwritten should the logic around object equality need to be modified.

Different approaches to compare persistent objects include:

Transitive Persistence

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.