NHibernate: Object Persistence

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:

  • Compare database identifier values, which the application and database use.
  • Compare the values of selected properties in the class that are guaranteed to be unique.
  • Compare a property, or combination of properties, called the business key that is unique for each class instance with the same database identity.