.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Introduction to Hibernate

Posted By:satyapriyanayak       Posted Date: December 31, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know about Hibernate in java.

Hibernate: - It is a framework to implement object relational mapping. It provides facility to persist state of object. Whenever a program creates object of a class then the state of object can be available till the program executes. It helps to keep state of an object after exist of the creator program. ORM is a mechanism to store state of an object into relational storage (Dbms). If the program uses jdbc then the program must know name of the table and columns to store values of variables through sql commands. The user of hibernate need not to know table and column name .The user of hibernate uses only object of the classes. Each record available in the table becomes an object in the program.

Architecture of hibernate



This is a class whose objects required to be persisted. The user application creates the objects to store its states into the permanent storage. The user application can change, fetch or remove state of the object at any time. This class contains a set of variables to represent state and their corresponding setter and getter methods.


This is a set of Xml files to established relation between pojo and table of dbms. It maps class name with table name and variable name with column name.


This is an xml file to content information about dbms and mapping files. This files also contains name of the dialect classes to convert HQL (Hibernate Query Language) into SQL.Hibernate provides different dialect classes for different DBMS.

Hibernate API

This is a set of classes and interfaces available from hibernate to established connection with dbms. The user program uses this api to configure the hibernate and to send Hql .The dialect classes present in this Api send the corresponding Hql to Dbms.


This is a program or user interface available to end-users. It uses the hibernate api to configure the api and uses Pojo or javabean to keep the state of object intact.

Installation of hibernate

1.Get the binary distribution file of hibernate (hibernate-distribution-3.3.1.ga-dist.zip) and extract it into any folder.Get it from the below url http://sourceforge.net/projects/hibernate/files/hibernate3/3.3.1.GA/hibernate-distribution-3.3.1.GA-dist.zip/download

2.Search for all .jar files present in extracted folder and make those files available in lib folder of the context. The lib folder must be created in the WEB-INF folder.

Configuration of hibernate

Create a file named as hibernate.cfg.xml inside classes folder. Use property tag to specify information about Dbms, dialect class and mapping files.


Search for hibernate.cfg.xml inside extracted folder of hibernate. Copy any of these files and make it available inside classes folder.

hibernate.cfg.xml file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="emp.hbm.xml"/>    

Here database is mysql
Database name in mysql - test
Table name-emp

Creating hibernate POJO or javabean

1.Create a class in a package as the bean class.
2.Provide required variables as the properties.
3.Provide setter and getter methods for each variables.
4.Store the package folder inside classes folder.
5.Compile the file as any ordinary java file.

Creating hibernate mapping table

1.Create a file inside classes folder having the following naming convention classname.hbm.xml

Search for any. hbm.xml files inside the extracted folder of hibernate. Copy any of the files to keep inside classes folder of the context. Remove all the tags present inside
<hibernate-mapping> and </hibernate-mapping>

2.Provide the tags to map class name with table name.

3.Provide the tags to specify name of the primary key present in the table with name of the variable present in the class.

4.Supply name of the .hbm file in hibernate.cfg.xml file

emp.hbm file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  <class name="mypack.Emp" table="emp">
       <id name="empno" type="int" column="empno" >
       <generator class="assigned"/>       
      <property name="ename">
         <column name="ename" />
      <property name="sal">
        <column name="sal"/>

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend