First Hibernate Application

In this tutorial you will learn about how to create an application of Hibernate 4.

First Hibernate Application

In this tutorial you will learn about how to create an application of Hibernate 4.

Here I am giving an example which will demonstrate you how can you make your application using Hibernate 4. To create an application I will use Eclipse IDE ( to see the basic requirement before creating an application with Hibernate 4 click here ).

Example :

Following steps that I have followed to create an application. These are as follows :

Here is the video tutorial of: "First Hibernate Application"

Step 1 : At first I have created a table named person in MySQL.

CREATE TABLE `person` ( 
`Id` int(10) NOT NULL, 
`Name` varchar(15) default NULL, 

Step 2 : Created a Java Project named coreHibernateExample (you can give the name as you wish).

File -> New -> project / Java Project -> giveProjectName -> Finish (in this example I have given coreHibernateExample).

Step 3 : Add the Hibernate jar files (to see how to add Hibernate's jar files click here )

Step 4 : Created a package named roseindia (you can give the name as you wish) under the src folder.

Select src -> Right Click ->New -> package -> givePackageName -> Finish (in this exmaple I have given roseindia).

Ste 5 : Created a file by following the name convention hibernate.cfg.xml (mandatory ) file under the src folder that the hibernate utilize to create a connection pool and the required environment setup.

Select src -> Right Click -> New -> File ->hibernate.cfg.xml -> Finish.

<?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://
<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.current_session_context_class">thread</property>


Step 6 : Created a POJO class (persistent class) named in the package roseindia.

package roseindia;

public class Person
int id;
String name;
public Person()
public Person(int id, String name) {
super(); = id; = name;
public int getId() {
return id;
public void setId(int id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;

Step 7 : Crated a file named ( you may follow the naming convention ClassName.hbm.xml )  person.hbm.xml under the src folder to map a Person Object to the database table named person

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="roseindia">
<class name="Person" table="person">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>

<property name="name">
<column name="Name" />


Step 8 : Developed a code by writing a java class named PersonDetail in the package roseindia which will persist the Person object in the person table.

package roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class PersonDetail
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args)
Session session = null;
Configuration cfg = new Configuration().addResource(
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
catch (Throwable ex)
System.err.println("Failed to create sessionFactory object."+ ex);
throw new ExceptionInInitializerError(ex);
session = sessionFactory.openSession();

Person person = new Person();
System.out.println("Inserting Record");
Transaction tx = session.beginTransaction();
} catch (Exception e) {
} finally {

Execute the example : You can execute the application as follows

Go to your PersonDetail class Right Click -> Run As -> Java Application or,

Select from the menu bar tab Run -> Run or,

press the Ctrl button along with f11 i.e. CTRL + f11.

Output :

When you will run the application output will be as follows :

1. On console of Eclipse the output will be displayed as :

2. After Successfully execution of the code the record will be saved to the table that you had created in MySQL. as :

Click Download Source Code

Download Source Code

Share on Google+Share on Google+

First Hibernate Application

Posted on: February 16, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: First Hibernate Application  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 13, 2012
Muchas gracias!!

Very useful. Thanks to you!!!
Sanjay Sreedhar
July 18, 2012
SQL Exception

Hi, After following the steps as in the tutorial, I am getting exceptions as shown in the below log Inserting Record Jul 18, 2012 11:31:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 0, SQLState: 08S01 Jul 18, 2012 11:31:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Communications link failure Last packet sent to the server was 0 ms ago. Could not open connection Can someone please assist me with this?
September 26, 2012
mapping resource

if mapping file in other package than src, i am not able to load it in cfg.xml using <mapping resource="roseindia/person.hbm.xml"/>
Chi Ngo
August 28, 2013
First Hibernate Application

I am a new bee in Hibernate. I tried to reproduce the application. I think tx.commit() does not work. I guess the error might come from the fact that the connection with the MySQL database does not work. Thank you very much for your precious help.