Home » Java and J2EE » Hibernate First Tutorial

Hibernate First Tutorial

Hibernate First Tutorial


2.Hibernate Architecture

3.Main Components of Hibernate

4.Hibernate Configuration

5.Hibernate Mapping File

6.Mapping Association

7.Basic API’s

8.Hibernate Data source properties

9.Complete Example

1: Hibernate Introduction:

While today’s applications are designed using Object Oriented Approach. As the business data is stored in the databases and applications create/fetches/update and remove these data for the business purposes. . Since both data storage and data handling are wast domain, over the period of time they are many vendors have provided mechanism for data data storage like oracle, db2 etc.. and similarly many application development technologies have enhanced theirs capabilities like J2EE,.Net etc.

The basic concept in Object Oriented approach is we consider the solution by identifying different Objects involved in the applications. Where as in case of Data bases there have evolved concept of RDBMS where data is stored in the Relational databases in tables and records. And application developer has to play the role of bridging this gap of data stored in tables and creating objects to be used in the object oriented programming. Developers has to write this data plumbing code as well as data processing code.

Hibernate came up with the solution of bridging this gap and providing all the necessary arrangements through which developer can just configure the required database and tables and later Hibernate will take care of data management work and developer just need to concentrate on data processing i.e. their application programming. Hibernates main advantage is the mapping of Objects data model with the corresponding table, developer do not need to bother about transforming data bases data in to objects. Object creation updation and persistence work will be done by hibernate.

2: Hibernate Architecture

Hibernate architecture is explained in the given figure below. Hibernates persistence layer lies between the data base layer and the application layer.


Hibernate Architecture

Hibernate Architecture

Detailed Architecture

Second figure explains the details architecture with all the components. Session Factory is singleton object for every databases. For every connection there will be a session created by this session factory, which can internally create mutiple transaction bases on the requirement.

Figure 2:

Hibernate Detailed Architecture

Hibernate Detailed Architecture




3: Main Components of Hibernate

Basic building blocks of the hibernate application are listed below:


3.1 SessionFactory org.hibernate.SessionFactory

This is a thread safe singleton immutable object of the database mapping. There is only one connection factory for each database. Its a factory for the hibernate session and it also maintains the second level cache between the application and database at the process level.

Usage example:<to be added>


3.2 Session org.hibernate.Session

This is single threaded an short lived object represents the conversation between the application and the database or persistence store. This is the wrapper over sql connection and works as the factory for the org.hibernate.Transaction. It mentains the first level cache of the application’s persistent object and collections. This cache is used while looking for the object using object id or while navigating the object graph.

Usage example:<to be added>



3.3 ConnectionProvider org.hibernate.connection.ConnectionProvider

This is optional and not directly exposed to the application developer, it represents the pool of connections. If require developer can implement this.


3.4 TransactionFactory org.hibernate.TransactionFactory

This is connection factory for the org.hibernate.Transaction, this is also not exposed to the developer but if require developer can implement this.



3.5 Transaction org.hibernate.Transaction

This is a single threaded short lived object use by the application to specify the atomicity of the work. It abstract the application from underlying jdbc/JTA implementation. Hibernate Session can maintaine more than one transaction also at a time.



3.6 Persistent Object

These are short lived business object represent the persistence state. They are associated with exactly one hibernate session. Once the session is closed they are free to used by the any layer of the application.



3.7 Transient and Detached Object

These are those instance of the persistence java classes whose objects are not yet associated or loaded with the session. They may have been instantiated by the application but not yet been associated with the hibernate session.



4: Hibernate Configuration File


This is the file where we specify the database level configuration. This file is read while application loads and then hibernate creates the connection pool with the specified database as per the specification. It also consist of entire set of mapping of Java Objects and Database tables.

Org.hibernate.cfg.Configuration object represent the entire set of mapping.

Configuarion and Sessionfactory objects:

SessionFactory factory = new Configuration.configure().buildSessionFactory();

Hibernate allows one Session factory for every database. Though it allows multiple session factory for more than one databases.


This is a simple example of the hibernate configuration 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 name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/mysql


<property name=”hibernate.connection.username”>root</property>

<property name=”hibernate.connection.password”>root</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=”contact.hbm.xml”/>




5: Hibernate Mapping File


We specify individual mapping for each database table. Mapping is declared between class with a table and respective column with the attributes of the class.


Mapping can be defined in following ways:

-Using Java 5 Annotations

-Using Hibernate legacy XML file.


5.1 Mapping using Java Annotation

Simple example of hibernate configuration using Annotation, here we can see we are explicitly specifying the mapping in the bean file itself using java 5 annotation.


import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;


@Table(name = “contact”)

public class Contact implements Serializable {

public Contact() {



@Column(name = “id”)

Integer id;

@Column(name = “firstName”)

String firstName;

public Integer getId() {

return id;


public void setId(Integer id) {

this.id = id;


public String getName() {

return firstName;


public void setName(String name) {

this.firstName = name;




5.2 Using Hibernate legacy XML file.


The traditional way of declaring the mapping is the hibernate configuration file and we can see in this example how we declare the same mapping by XML file.

This is the most simple and popular way of declaring the mapping, we can also specify the mapping between come other class and table in the same file or we can maintain separate mapping file for each table.



<?xml version=”1.0″?>

<!DOCTYPE hibernate-mapping PUBLIC

“-//Hibernate/Hibernate Mapping DTD 3.0//EN”



<class name=”roseindia.tutorial.hibernate.Contact” table=”CONTACT”>

<id name=”id” type=”long” column=”ID” >

<generator class=”assigned”/>


<property name=”firstName”>

<column name=”FIRSTNAME” />


<property name=”lastName”>

<column name=”LASTNAME”/>


<property name=”email”>

<column name=”EMAIL”/>





…………………………….article to be completed soon !



1 Comment

  1. Sameer Gupta says:

    Its a really good to startwith tutorial. I look forward for the next update.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: