Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | March 26, 2019

Scroll to top


No Comments

Why use NHibernate ? Advantages of NHibernate

Why use NHibernate ? Advantages of NHibernate
Mudassir Shahzad

An Object relational mapping (ORM), as shown from the name, is used to map the objects to a relational database. Now, to clarify, by relational database we mean a database with relations(tables) and mapping an object means doing the necessary configuration so that we can automatically save the object as a whole(in the db).

Object-Relational Mapping (ORM) is the concept of mapping an application’s business objects to relational database tables so that data can be accessed and updated entirely through the object model of an application(of course, without writing the queries manually). That’s what an ORM does, doesn’t it ?

Now, as far as the industry is concerned, there are a lot of ORM tools available and in use. Nhibernate, being one of them, is used for the .Net-land.

Yeah, NHibernate is a port of the popular Hibernate.


Now, if you ask me “what is the best tool ?” or “Is Nhibernate better than Mybatis or Entity Framework?”

I would say – It depends. Like if you ask me “What is the best car?” – So is the answer. It depends.

Coming to the advantages.


Why O/R mapping ?


1. Productivity

The developer no more need to write all the SQL manually. It also helps the developer to concentrate on the business logic of the project instead of giving time to manual SQL queries and the consequent bugs. This helps increase the project productivity.


2. Maintainability

It generates the necessary SQL queries on the backend, making it easy for the developer to maintain the code.


3. Portability

NHibernate is portable. Regardless, of the database, you can write your code and run it with every database just by making a few change in the hibernate configuration file.


Why NHibernate should be chosen over other O/R mapping technologies ?

1. Productivity / Maintainability / Portability

NHibernate provides all the above mentioned O/R mapping features.


2. Its Open Source

NHibernate is open source and is the right choice for the companies who tend to work using open source software.


3. Its Popular

As discussed, NHibernate is a port of popular Hibernate O/R (notice the dropped “N” 😉 ) mapping and there is a lot of support for bugs. It has been used in many projects, so there is a 99% chance that if you come across an error or problem using it, you can find its solution on the internet because there are many discussion boards and websites writing about it (including me 😉 ).

4. First and Second Level Caching

When using NHibernate the first level cache is automatically enabled as long as one uses the standard session object. We can avoid using a cache at all when using the stateless session provided by NHibernate, though.

When NHibernate is loading an entity by its unique id from the database then it is automatically put into the so called identity map. This identity map represents the first level cache and its lifetime is coupled to the current session. As soon as the current session is closed, the content of the respective first level cache is cleared.

The life time of the second level cache is tied to the session factory and not to an individual session. Once an entity is loaded by its unique id and the second level cache is active the entity is available for all other sessions (of the same session factory). Thus once the entity is in the second level cache NHibernate won’t load the entity from the database until it is removed from the cache.
To enable the second level cache we have to adjust our configuration file. We have to define which cache provider we want to use. There exist various implementations of a second level cache. For our sample we use a Hashtable based cache which is included in the core NHibernate assembly. Please note that you should never use this cache provider for production code but only for testing. Please refer to the chapter “Second Level Cache implementations” below to decide which implementation fits best for your needs. You won’t have to change your code if you change the cache provider though.

5. Consistency

If code generation is used, the generated is extremely consistent because standard methods are used to generate it.


To sum up, NHibernate is new in the industry but its making its way due to the advantages discussed above. Plus, there is a very good feedback from the developers who used it.

This has been my struggle with nh, and I think it rocks as an ORM in the .Net-land.




(official nhibernate docs)


Do share it!



Submit a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Object moved

Object moved to here.