This article is for those people searching for an alternative to the Entity Framework. So, what is wrong with the Entity Framework? This article of mine will neither say anything negative about the Entity Framework (if any) nor the branding of Nhibernate. I am an evangelist for neither of the frameworks.
I like articles with less theory and more code. I have yet to reach this goal of mine. Bear with me this time.
If you are reading this term NHibernate for the first time, you have reasons to continue reading further. We will explain the following topics.
- What Nhibernate is
- Why NHibernate
- Installing NHibernate
- Creating the NHibernate mapping file
- Querying database using NHibernate
- References
NHibernate
Why NHibernate
Installing NHibernate
- Download the file from website.
- After extracting and opening the file you can see the Required_bins folder containing the DLLs and other mapping files.
- Reference the DLLs that are available in this folder to your application.
- Add the namespace using.NHibernate and using NHibernate.cfg to the class that needs mapping.
Installing a NuGet does not need much explanation. In the package manager console type “install-package nhibernate”.
Once NHibernate is downloaded, it must be referenced as specified in Step 4.
Creating a NHibernate mapping file
The initial step is to create an XML mapping file that lets the classes containing POCO to talk to the database. You can even first go ahead with creating classes for mapping.
I have created a class containing POCO and named it as Customer.cs.
I am creating a simple XML file for mapping named Customer.hbm.xml. Here .hbm.xml states that it is a nhibernate mapping file. Here I am defining a hibernate-mapping tag along with the assembly and the namespace. I am creating a mapping file based on these classes and properties for the mapping.

- <?xml version="1.0" encoding="utf-8" ?>
- <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
- <class name="DataTransfer.Customer" table="Customer">
- <id name="CustomerId" column="CustomerId" type="Int32" unsaved-value="0">
- <generator class="native"></generator>
- </id>
- <property name="FirstName" column="FirstName" type="string" length="50" not-null="false"></property>
- <property name="LastName" column="LastName" type="string" length="50" not-null="false"></property>
- </class>
- </hibernate-mapping>
In order to map this perfectly with our database, we need to create a customer table containing firstname, lastname, and customerid. The preceding mapping file has the following attributes.
- Hibernate-mapping: This is the starting tag having the hibernate mapping namespace. It has several attributes and some of them are assembly and namespace.
- Class: Contains the class name for the specific application. It has important attributes like name and table. Here name describes the name of the class and the table defines the name of the database table.
- Id: determines the id attributes we use in our tables. It has subtags like generator. The generator tag has attributes like native, hilo, guid.comb, guid, and so on. I must keep this article as short as possible. Google will help you learn about them.
- Property: determines the column names of the tables. Here the attribute "name" represents the name of the property in customer.cs and the column attribute determines the name of the column.
Querying database using NHibernate
There are several ways to write queries in Nhibernate. They are:
- Using LINQ: You can use LINQ to query.
- HQL: acronym for Hibernate Query Language, the same as LINQ but with slight differences.
- Using Criteria API: An API to make querying easier with extension methods and lambda expressions.
- QueryOver: another API to make querying easy.
Attached is a sample project file for your reference. The application will get feeling comfortable with the basic idea of what Nhibernate is. Please also read about FluentNHibernate. FluentNhibernate is flexible and there are very many plugins available to make mapping easier in NHibernate.
References
- nhibernate
- NHibernate Reference Documentation
- Fluent NHibernate by jagregory
- NHibernate 3.0 CookBook by Jason Dentler

Gowtham RajamanickamPosted Mar 28, 2015, 12:31 PM
great