< Prev
Next >

Hibernate - The first program without IDE

Today we are going to understand how to put Hibernate into an execution without using any Integrated Development Environment(IDE) application like Eclipse, NetBeans etc.

As you know that Hibernate is a middleware used for object-relational mapping(ORM) and for performing efficient object persistence. So, in the next section, we are going to create a Java class which will be mapped to a table in database(Oracle Express Edition 10g) and its objects will be persisted, using the Hibernate.

POJO/Entity class

This is a simple Java class whose objects needs to be persisted/saved, these objects are also known as Plain Old Java Objects(POJO) or an Entity class. Some may even refer to such class whose objects needs to be persisted as the Model class.
package decodejava;

public class UserData 

//public no-arg constructor
public UserData()

private int id;
private String name;
public int getId() 
	return id;
public void setId(int id) 
{ = id;
public String getName() 
	return name;
public void setName(String name) 
{ = name;


Class that makes hibernate work

This class will perform some create, retrieve, update, delete(CRUD) operations on the UserData object using the Hibernate API, which will lead object persistence and an object-relational mapping(ORM) in the database. The Configuration object is used to configure the Hibernate. This is the first object we use when using Hibernate. This object is used to specify the location of a configuration file and mapping document used by Hibernate. Using Configuration object we can create a SessionFactory object, which is eventually used to create a Session object to perform the object persistence operations.
package decodejava;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Hiber 

public static void main(String[] args) 

//Creating the Configuration object.
Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addResource("userdata.hbm.xml");

//Using the Configuration object to creagte a SessionFactory object.
SessionFactory sf = cfg.buildSessionFactory();  

//Using the SessionFactory object to create a Session object.
Session session = sf.openSession();
//Creating the first object
UserData ob1 = new UserData();
//Creating the second object
UserData ob2 = new UserData();
//Creating the third object
UserData ob3 = new UserData();

session.beginTransaction();; //Saving the first object.; //Saving the second object; //Saving the third object

ob1 = null;
//Creating a new Session to retrieve and modify the object.
session= sf.openSession();
ob1 =(UserData)session.get(UserData.class, 3);
//Modifying the username of Id=3
//Saving the modified object in database

//Committing the trasaction

//Closing the session
ob1 = null;
ob2 = null;
ob3 = null;

//Creating a new Session to retrieve the objects.
session= sf.openSession();
ob1 = (UserData)session.get(UserData.class,1);
ob2 = (UserData)session.get(UserData.class,2);
ob3 = (UserData)session.get(UserData.class,3);
System.out.println("Retrieving the saved objects");
//Retrieving details of the first user.
System.out.println("First User");
System.out.println("Id : " + ob1.getId() + "  Name : " + ob1.getName());
//Retrieving details of the second user.
System.out.println("Second User");
System.out.println("Id : " + ob2.getId() + "  Name : " + ob2.getName());
//Retrieving details of the third user.
System.out.println("Third User");
System.out.println("Id : " + ob3.getId() + "  Name : " + ob3.getName());

//closing the session
//closing the heavyweight SessionFactory object


Mapping Document

This mapping document is essentially required by Hibernate to map the class and its instance variables/properties to a database table and its columns.

This mapping document tells Hibernate -
This mapping document ends with an extension .hbm.xml, hence, we have named it userdata.hbm.xml.

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


   <class name = "decodejava.UserData" table = "userdata">
      <id name = "id"  column = "id" type = "int">
         <generator class="native"/>
      <property name = "name" column = "Name" type = "string"/>


This mapping document has <hibernate-mapping> as the root element and its child element <class>, containing all the class elements.

Hibernate Configuration File

This configuration file is an xml file and it allows us to specify the following features -
This configuration document ends with an extension .cfg.xml, and it is named as hibernate.cfg.xml.

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



<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">promila21</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

<!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="">create</property>

<!-- Names the mapping entity class -->
<mapping class ="decodejava.UserData"/>

<!--Naming the mapping resource file -->
<mapping resource ="userdata.hbm.xml"/>



Directory Structure of Hibernate Project

The picture above depicts how and where to arrange POJO/Entity(.java) file and the other the class(that calls the hibernate into action) in a specific directory structure.

  • Project Folder - HibernateProgram is the name of our Project and it is a top-level directory.

  • Compilation

    Please Subscribe

    Please subscribe to our social media channels for daily updates.

    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



    Please check our latest addition