Hibernate is an open-source object-relational mapping technology for .Net Framework. It provides the framework required for mapping model to relational database and generate queries.
Hibernate is a framework that provides the Establishing of the database connections, and writing queries to perform the CRUD operations.
It is an open-source Java framework and it is used to store and process the data.
Hibernate is the most prominent Object Relational Mapper (ORM) tool in Java today and it is highly acclaimed by developers. It is also an award-winning framework.
It is used to develop persistence logic. It is a special installable framework that provides an abstraction layer on one or more technologies and reduce the complexity for the development process.
Benefits of Hibernate Framework:
- Open Source: Hibernate framework is open source. So, it is available for everyone without any costs.
- Lightweight: Hibernate framework is lightweight. So, it is less in size means the installation package is not big in size.
- Fast Performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework.
- Hibernate is distributed through NuGet as a single DLL, with no other library dependencies where other framework like Entity Framework consists of multiple DLLs, coming in many NuGet packages.
- Hibernate has lazy loading of associated entities, collections, and even single properties. There is also explicit and eager loading.
- Database Independent Query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don’t need to write database queries.
For example, we need to fetch first 10 records of a table.
So in SQL server, it will implemented as below:
Select top 10 column_name from table_name order by column_name asc;
In hibernate it will implemented as below:
Session.CreateQuery(“Select column_name from table_name order by column_name asc”).SetMaxResults(10).List();
- Automatic Table Creation: Hibernate framework provides the facility to create the tables of the database automatically. So, there is no need to create tables in the database manually.
- Simplifies Complex Join: Fetching data from multiple tables is easy in the hibernate framework.
- Minimize Code Changes: Hibernate minimizes code changes when we add a new column to a database table.
Consider the following scenario if we want to add new column to a database table.
1. We need to add new field in the model class.
2. We need to change the “Select” method to include the new column.
3. We need to change the “Insert” method to add a new value into the new column.
4. We need to change the “Update” method to update an existing value in the new column.
1. We need to add new field into the class.
2. We need to modify the Hibernate XML mapping file to include the new column.
Integrate Hibernate Framework with Asp.Net:
Step 1: Create a new project in the visual studio and then add an ASP.Net web application.
Step 2: Create a new database in SQL server to connect with our application and then create a new table in the database.
Step 3: Installing Hibernate
Now, Install the NHibernate package using the NuGet package. There are different ways to install the NHibernate.
1. NuGet Package Manager
Go to NuGet package manager and install as shown below.
2. Package Manager Console
Go to package manage console and execute the following command
Once NHibernate package installed, you can see that package in the references.
Step 4: Create NHibernate configuration file
We have created the database and tables, installed the necessary libraries to work on NHibernate.
Now, create the NHibernate configuration file so that the application can connect to the database. This configuration file contains the information required to connect the application to the database.
Create a XML or config file under the Models folder, named hibernate.cfg.xml.
You can create the config file by go to the Right click on Models > Add > New Item
Then enter the database connection to connect the application with database like below.
Step 5: Create the model class
Right click on Model > Add > Class
Create a class, named Profile(For example) under the Models folder and enter the fields on which you want to perform crud operations
Step 6: Creating the mapping file
Create a folder called Mappings and create a new XML file (like you did in Step-3), named Profile.hbm.xml. It is important to have the hbm as it is the part if the file name, which denotes that this file is a mapping file.
The purpose of this file is to create a mapping between the model class and the respective table in the database.
Step 7: Managing NHibernate Session
Create a class called NHibernateSession.cs under the Models folder and configure the model class with hibernate like below
Step 8: Add Controller file
Add a new controller called ProfileController. This has the methods defined to Create, Read, Update and Delete functions on the Profile entity(table).
Each of this method make use of the NHibernate session to initialize and open a session and perform the necessary transaction.
Step 9: Create necessary Views for List, Create, Details, Edit and Delete with related models.
Create View for List
Right click on List > Add View
The output of the List will look like below
Create View for Add
Right click on Create > Add View
The output of the Create New will look like below
Create View for Details
Right click on Details > Add View
The output of the Details will look like below
Create View for Edit
Right-click on Edit > Add View
The output of the Edit will look like below
Create View for Delete
Right click on Delete > Add View
The output of the Delete will look like below
Manektech builds enterprise-level web applications using ASP.NET and other Microsoft Technologies. Our ASP .Net Developers draws experience in all major industries to provide custom ASP.NET Application Development to meet the most challenging IT problems for the world's leading enterprises and startups. As a Microsoft Gold Certified Partner, we implement industry-best practices for full-cycle ASP.Net Application Development right from analysis, design, implementation, testing and integration of systems.
Need a consultation?
Drop us a line! We are here to answer your questions 24/7