imageA couple of hours ago I finished a presentation about NHiberante at the Developer Summit 2010 conference being held here in Stockholm.

I have worked with NHibernate in many projects, some greenfield projects but mostly replacing an existing data layer. I have introduced it to many teams. In those teams I have always been the NHibernate guy. The person that people turn to when faced with some NHibernate related problem. So when I started to prepare for this presentation I asked myself what is it that is causing these problems, what is it that people are missing in there understanding?

So the focus of the talk centered around these areas:

Identity Map (L1 Cache)
Understanding how the identity map is an integral part of NHibernate is very important. If you are not aware that you share instances for entities with the same id you can run into some strange bugs and side effects. The identity map is probably one of the main causes of problems when replacing an old DAL layer (that do not use an identity map) with an ORM.

Unit Of Work
Of course it is important to know how the Unit Of Work in NHibernate works, it’s relation to Flush and Transaction (FlushMode Commit).

Session Managementimage
Important to use the session correctly (i.e. do not open and close it in the data layer).   

Understanding cascading can be tricky, especially how correctly to use cascade=”none” and how that works in relation to transient instances (it doesn’t). It is important to understand that NHibernate persists all changes to modified entities that are associated with an open session, no matter if you set cascade to none. 

Attached & Detached
It is very important to understand what it means that an instance is attached/associated with an open session. In the talk I show the result when you try to persist a modified detached instance. I also show how you can solve that issue by associating the detached instance with an open session (before the modification) by calling session.Lock and how you can as your last option use session.Merge to update modified detached instances.

Lazy Loading & Proxy Objects
In the talk I show how NHibernate’s lazy loading works via proxy objects and what issues you might run into. For example trying to check the concrete type in an inheritance chain won’t work as the type will always be the proxy object.

ID Generation & unsaved-valueimage
It is important to understand how NHibernate determines if an instance is transient or not and how that plays into id generation. If you need to use assigned id you can still let NHibernate know if an instance is transient or not by using a version column.

I ended the talk by showing how to handle concurrency (by using a version column).


If all NHiberante users have a good grasp of the above areas I think they would experience a lot less bugs and head aches. So for those that attended the talk I hope they will not be making the same mistakes I have made and have seen others make. 

Here is a link to download the powerpoint slides


Gurra777 said...

Too bad I missed it. Excellent topic! When is your next conference?

Torkel said...

Don't know.

Anonymous said...


I've been working with Hibernate since 2004 and have just had the opportunity to upgrade to NHibernate 2.1. But what puzzels me a lot is that session.Lock(entity, LockMode.None) does not reassociate the detached entity with the session. The exact code has been working for years with NHibernate 1.2, and is also described here:

At the moment I'm using session.Merge, but it is not the same as it potentially may change the state of the detached object. Do you know if session.Lock() has been broken or it's usage has changed? I'm googling around to get some answers but there is not much information to find. Also, most information I find is pointing to the session.Lock method I have been using for a while.


Torkel said...


Strange, it should work in 2.1 as before. Please post your problem on the nhibernate users list or on stackoverflow.

Charles Strahan said...

Darn - can't believe I missed this presentation. Any chance we can view it online, Torkel?