Managing State and Saving Data

In the previous chapter, the LINQ to NHibernate API was discussed. Like the IQuery, ICriteria, and QueryOver APIs, LINQ to NHibernate provides functionality that enables the retrieval of data from a database in an object-oriented manner. Up to now, only the retrieval of data has been covered. This chapter, however, covers data insertion and entity state management. This is where many of NHibernate's strengths lie. It is also a more complicated aspect of NHibernate, however, and therefore more diffi cult to comprehend and implement. This chapter clarifi es the following topics:

  • An overview of concurrency
  • Using NHibernate versioning
  • Implementing a custom data type using IUserType
  • Inserting data into a database
  • Using the first- and second-level caches
  • Understanding the Evict(), Merge(), and Persist() methods
  • Implementing batch processing


Of course, a discussion about NHibernate isn't complete without covering the management and manipulation of data. It is relatively difficult to find good examples of inserting data using NHibernate, probably because there are so many options and implementation possibilities. The task, or program requirement, at hand determines which technique is the best in a given situation.

Before using NHibernate's methods, such as Save(), SaveOrUpdate(), Update(), and so on, which add or modify data on the database, it is important to understand the three different instance states an object can have: transient, ...

Get Working with NHibernate 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.