It is an open source java based solution.
- It provides solution
using mapping between objects.
This mapping is called ORM(Object Relational Mapping).
- It uses MVC
- It has model view and controller all the three components.
components are light weighted.
- Hibernte is an open-source framework
- It is used to establish connection between database
and java or JEE Application using ORM(Object Relational
takes care of the mapping from Java classes to database tables (and
from Java data types to SQL data types)
- It provides
data query and
- It significantly reduce development time
spent with manual data handling in SQL and JDBC.
goal is to relieve the developer from 95 percent of common data
related programming tasks.
is available for download at http://www.hibernate.org/
/ Relational mappings
primary key generation
- Object Relational Mapping
It is a new solution given in java against ejb whcih is heavy weight
It has light weight components.
- It persists data in the database.
provides transaparent solution as the table of the database is hidden.
- It gives all major three operations called CRUD (Create, Update and
Delete) of the database.
- It provides database support with vendor
- Any vendor specific database can be used.
- It has given the
solutions for all the major database
The objects gets persisted in the database.
- It happens by
class configuration with the database table
with the columns.
By the introduction of annotations in Java 5 the mapping is
in entity bean
by the annotation in place of the xml file.
The role of
Hibernate in a Java application
makes use of
persistent objects commonly called as POJO (POJO = "Plain Old Java
Object".) along with XML mapping documents for persisting objects to
term POJO refers to a normal Java objects that does not
serve any other special role or implement any special interfaces of any
Java frameworks (EJB, JDBC, DAO, JDO, etc...).
than utilize byte code processing or code generation, Hibernate uses
reflection to determine the persistent properties of a class
The objects to be persisted are defined in a mapping document, which
describe the persistent fields and associations, as well as any
proxies of the persistent object
mapping documents are compiled at application startup time and provide
framework with necessary information for a class.
are used in support operations, such as generating the database schema
creating stub Java source files.
level architecture of Hibernate
sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Customer newCustomer = new Customer();
newCustomer.setAddress("Address of New Customer");
step in hibernate application is to retrieve Hibernate Session
is the main runtime interface between a Java application and Hibernate
allows applications to create hibernate session by reading
configurations file hibernate.cfg.xml.
specifying transaction boundaries, application can make use of
objects and use session for persisting to the databases.
Integrating and Configuring Hibernate
persistence layer using hibernate mapping document
persistent class needs to be mapped with its configuration file.
represents Hibernate mapping file for Employee class
This pojo (java bean file ) represents the persistent class.
This will get mapped with the specific table in the database.
Hibernate Mapping File
- We need to map the POJO file with the Database table.
- Each class is mapped to the table.
- Each attribute of the pojo file is mapped with the table column.
- There must be one identifier for in the pojo file.
- This identifier will be one of the pojo attribute.
- In mapping file this identifier will be mapped with primary key column.
- This primary key column might be using sequence or auto increment.
- This is generally saved as
mapping documents are straight forward.
element maps a table with corresponding class
<id> element represents the primary key column, and its
attribute in the domain object
<property> elements represent all other attributes
available in the
- Hibernate SessionFactory are thread safe.
- So it is recommended that there should be only copy of Sessionfactory.
- But Hibernate Session are not thread safe.
- So Session can have differrent instances.
- This can be achieved by the use of Singleton.
- We will create static instance of Sessionfactory
- Then create static method returning SessionFactory instace.
configuration file information needed to connect to persistent layer
linked mapping documents.
element <mapping-resource> refers to the mapping document
mapping for domain object and hibernate mapping document.
- One can
either specify the data source name or JDBC details that are required
hibernate to make JDBC connection to the database.
- Inside session-factory tag we will provide the database configuration
- This is done with the help of property tag.
- Property tag is having differrent name and values.
- This is generally saved as hibernate.cfg.xml
- These properties are
In this example we will create one simple application step by step.
- Hibernate can be connected with differrent databases
- It uses dialect for connecting with mysql.
- In this example we will use mysql dialect to connect
|mysql\bin>mysql -u root -p
Enter password: *****
mysql> use test;
mysql> create table stud (roll int primary key, name char(12));
Query OK, 0 rows affected (0.12 sec)
mysql> desc stud;
| Field | Type | Null | Key | Default | Extra |
roll | int(11) | NO | PRI
| name | char(12) | YES | | NULL | |
2 rows in set (0.02 sec)
Make a bean file