Please enable Javascript to correctly display the contents on Dot Net Tricks!

Introduction to N-hibernate

  Author : Shailendra Chauhan
Posted On : 11 Aug 2011
Total Views : 20,482   
Updated On : 16 Jul 2013

NHibernate is a .NET based object persistence library for relational databases. Basically it is a framework that allows us to talk to a relational database in an object-oriented way. We can store/persist objects in a database and load those objects from the database later on. NHibernate automatically translates our programming language syntax to a language that the database understands. NHibernate also generates the required SQL statements to fetch, insert, update and delete data.

If we use NHibernate, then we never have to write any code that deals with the fact that there is an impedance mismatch between the way we develop applications in .NET and how a database works. NHibernate has abstracted away this mismatch for us.

What is O/RM?

O/RM is an acronym that stands for object/relational mapping. In a nutshell, an O/RM framework is used to persist model objects in a relational database and retrieve them. It uses metadata information to interface with the database. This way, your data-layer code knows nothing about the database structure; the O/RM tool becomes middleware that completely hides the complexity.

The heart of an O/RM is the mapping. Basically mapping technique binds the object and relational worlds. By mapping, we express how a class and its properties are related to one or more tables in the database. This information is used by the O/RM tool engine to build SQL code to retrieve data and transforms it into objects. The mapping information is generally expressed as an XML file. As an alternative, some O/RM tools use attributes on the classes and their properties to maintain mapping data.

Why NHibernate?

In the past, we carefully hand-crafted our data access layers for the applications we wrote. We spent as much as 50% or more of our overall time to implement and maintain this data layer.It was not a very challenging task though, and a lot of repetitive coding was involved.

As we were not the only developers facing the problem of writing the data access code,some people started to write and publish a framework that would solve this problem. This was the birth of the ORM frameworks such as NHibernate. Nowadays, accessing relational data from an object-oriented application is a solved problem

Thus, writing your own data layer is a waste of time. Somebody even used a much more pronounced phrase to pinpoint this fact, "Writing your own data access layer is like stealing money from your client".

Installing NHibernate

NHibernate is an open source project (OSS). Thus, the code and/or the binaries are freely available and can be downloaded from SourceForge. Before you can start coding, you must first install NHibernate. The following list presents a couple of blogs that are either mostly dedicated to NHibernate or contain a lot of information around this framework:

  1. The latest version of NHibernate can be downloaded from

  2. NHibernate meta blog at

  3. Fabio Maulo's blog at Fabio is the leader of the NHibernate project.

  4. James Gregory's blog at James is the leader of the Fluent NHibernate project.

Once you’ve downloaded and installed NHibernate, you’re ready to create a new solution and start using it.

Free Interview Books
23 MAY
Xamarin Forms : Build Cross-platform Apps (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
ASP.NET MVC with Angular4 Development (Online)

06:30 AM-08:30 AM IST(+5.30 GMT)

Know More
29 APR
Xamarin Forms : Build Cross-platform Apps (Classroom)

5:00 PM-6:30 PM

Know More
29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

Know More
27 APR
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

Know More
22 APR
NodeJS Development (Classroom)

11:00 AM-12:30 PM

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

03:30 PM-05:30 PM

24 FEB
Angular 2 with Type Script (Online)

09:00 PM-11:00 PM IST(Mon,Wed,Fri)