Learning a simple hibernate program

Here I will explain Hibernate sample program using Java Application in Eclipse IDE. In this example we will insert record in MySQL database.

Follow the instructions step by step to run your Simple Hibernate Program in Eclipse IDE

Step – 1 Creating Java Project

Step – 2 Project Structure For Hibernate Example

Step – 3 Create hibernate.cfg.xml(Configuration file)

Step – 4 Create a Pojo File(mapping to database table)

Step – 5 Creatingusercontact.hbm.xml

Step – 6 Create The Class To Insert Records

Step – 7 Adding Jar Files

Step – 8 Run Your Main Class File

Step- 1 Creating Java Project

Start Eclipse and go to File -> New -> Project -> Java Project

On clicking next you will have to enter the Project name and your project gets created.

Step- 2 Project structure for Hibernate example

– The project structure for Hibernate_Example is shown below:

Step- 3 Create hibernate.cfg.xml (Configuration file)


<property  name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>

The above shown is the property is for loading the Drivers in MySQL database.

<property name=”hibernate.connection.url”>jdbc:mysql://localhost/usercontact</property>

The above property is used to open connection to database named usercontact which runs in localhost.

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

The above property shows that the username for the database connection is root.

<property name=” hibernate .connection. password”> </property>

The above property shows that there is no password used for the database connection.

<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>

The dialect property  is org.hibernate.dialect.MySQLDialect which tells the Hibernate that we are using MySQL Database.

Hibernate supports many databases for Object/Relational Mapping and Transparent Object Persistence for Java. We can use following databases dialect type property:

<mapping resource=”usercontact.hbm.xml”/>

The above property is used for mapping  usercontact table.

<?xml version=<em>'1.0'</em> encoding=<em>'utf-8'</em>?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD//EN"




<property name=<em>"hibernate.connection.driver_class"</em>>com.mysql.jdbc.Driver</property>

<property name=<em>"hibernate.connection.url"</em>>jdbc:mysql://<span style="text-decoration: underline;">localhost</span>/<span style="text-decoration: underline;">usercontact</span></property>

<property name=<em>"hibernate.connection.username"</em>>root</property>

<property name=<em>"hibernate.connection.password"</em>></property>

<property name=<em>"hibernate.connection.pool_size"</em>>10</property>

<property name=<em>"show_sql"</em>>true</property>

<property name=<em>"dialect"</em>>org.hibernate.dialect.MySQLDialect</property>

<property name=<em>"hibernate.hbm2ddl.auto"</em>>update</property>

<!-- Mapping files -->

<mapping resource=<em>"usercontact.hbm.xml"</em>/>



Step- 4 Create a pojo file (mapping to database table)

Hibernate uses the Plain Old Java Objects (POJOs) classes to map to the database table. We can configure the variables to map to the database column. Here is the code for usercontact.java:

package com.demojava;

public class usercontactVO {

private String firstName;

private String lastName;

private int phone;

private String email;

private long id;

public String getFirstName() {

return firstName;


public void setFirstName(String firstName) {

this.firstName = firstName;


public String getLastName() {

return lastName;


public void setLastName(String lastName) {

this.lastName = lastName;


public int getPhone() {

return phone;


public void setPhone(int phone) {

this.phone = phone;


public String getEmail() {

return email;


public void setEmail(String email) {

this.email = email;


public long getId() {

return id;


public void setId(long id) {

this.id = id;



Step- 5 Creating usercontact.hbm.xml

This file is used to map the usercontact object to usercontact table in database. Here is the code for usercontact.hbm.xml:

<?xml version=<em>"1.0"</em> encoding=<em>"UTF-8"</em>?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"



<class name=<em>"com.demojava.usercontactVO"</em> table=<em>"usercontact"</em>>

<id name=<em>"id"</em> type=<em>"long"</em> column=<em>"ID"</em> >

<generator class=<em>"assigned"</em>/>


<property name=<em>"firstName"</em>>

<column name=<em>"FIRSTNAME"</em> />


<property name=<em>"lastName"</em>>

<column name=<em>"LASTNAME"</em>/>


<property name=<em>"email"</em>>

<column name=<em>"EMAIL"</em>/>


<property name=<em>"phone"</em> type=<em>"int"</em>>

<column name=<em>"PHONE"</em>/>




Step- 6 Creating the class to insert records.

Before creating a class we should be clear about hibernate session.

Hibernate session: Hibernate Session is the main runtime interface between a Java application and Hibernate. SessionFactory allows applications to create hibernate session by reading hibernate configurations file hibernate.cfg.xml.

On session.save(contact) will save the information into database.

package com.demojava;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class main1


public static void main (String[] args) {

Session session = null;


// This step will read hibernate.cfg.xml and prepare hibernate for use

SessionFactory sessionFactory = new


session =sessionFactory.openSession();

Transaction tx=(Transaction)session.beginTransaction(); // Add this soon after initializing session object

//Create new instance of usercontactVO and set values in it by reading them from form object

System.out.println("Inserting Record");

usercontactVO contact = new usercontactVO();










catch(Exception e){



finally {

// Actual contact insertion will happen at this step






Step- 7 Adding jar files

The following jar files needs to be added to run the application/Example in Hibernate

You can add jar files as shown below:

Click on project on right click select properties which will open the following Dialog box.

Step- 8 Run your Main Class Files

Right click  project -> Run As -> Java Application

This is the output you will find- And you records get inserted.

Hope this helps you🙂 Your queries are always welcomed…

Speak Your Mind

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: