NHibernate: Object Persistence
- NHibernate Object Persistence: Persistent Objects
- NHibernate Object Persistence: Compare Persistent Objects
- NHibernate Object Persistence: Transitive Persistence
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.
- When an object is first created using the new operator, their state is transient, which means these objects aren't managed by the Persistence Manager (ISession) and not associated with any database table row.
- For an object to transition from a transient state to a persistent state, invoke the Save() method of the Persistent Manager (ISession), or create a reference to the object from another object that is already persistent.
- A persistent object is any object that is managed by the Persistent Manager (ISession) and is transactional.
- When an object is persistent it has a database identity. Meaning the object has a primary key value set as its database identifier.
- For an object to transition from a persistent state to a detached state, invoke the Delete() or the Close() method of the Persistent Manager (ISession).
- A detached object is an object that is no longer managed by the Persistent Manager (ISession), but the objects still have state in the database.
- A detached object can transition back to a persistent state, by reattaching the object to a new Persistent Manager (ISession). The object state will then be synchronized with the database again, at the end of the transaction.
- An understanding of the identity scope within NHibernate needs to be understood to take advantage of the ability to reattach detached objects with a new Persistent Manager (ISession).
NHibernate Scope of Object Identity
NHibernate persistent objects are identifiable in two ways:
- Database Identity - the identifier of the persistent object, which corresponds to the primary key value of the database row.
- .NET Identity - the identifier of the persistent object stored by the Persistent Manager (ISession).
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:
- A persistence layer with no identity scope makes no guarentee that if a row is accessed more than once, that the same .NET object instance will be returned.
- A persistence layer using transaction-scope identity guarantees that in the context of a single transaction, only one .NET object instance represents a particular row in the database.
- A persistence layer using process-scope identity guarantees that in the context of a entire process, only one .NET object instance represents a particular row in the database.
The NHibernate Persistent Manager (ISession) typically makes use of transaction-scope identity.