O'Reilly logo

NHibernate 3.0 Cookbook by Jason Dentler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using session.Merge

session.Merge is perhaps one of the most misunderstood features in NHibernate. In this recipe, I'll show you how to use session.Merge to associate a dirty, detached entity with a new session. This is particularly handy when recovering from StaleObjectStateExceptions.

Getting ready

Using our Eg.Core model from Chapter 1 and the Configuring NHibernate with App.config recipe from Chapter 2, set up a console application.

How to do it...

  1. Add the following code to your Main method:
    var book = CreateAndSaveBook(sessionFactory); book.Name = "Dormice in Action"; book.Description = "Hibernation of the Hazel Dormouse"; book.UnitPrice = 0.83M; book.ISBN = "0123"; using (var session = sessionFactory.OpenSession()) { using (var tx = session.BeginTransaction()) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required