Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Hibernate Example

This tutorial illustrate an example of hibernate


Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example

Hibernate Example

This tutorial illustrate an example of hibernate.

Hibernate provide a way of mapping of applications class to the database table.
It maps each class properties to the corresponding columns of table. After mapping to the table, all operations are performed by using the object of class in which table is mapped.
Hibernate is an ORM based programming technology provides mapping library.
Here is a simple example of mysql database connectivity using hibernate. Here we are describing this example step by step.

Step 1. Configuring Hibernate-

hibernate-cfg.xml is configuration file saved in the same folder where the source code of class file is saved. It creates the connection pool and set-up required environment.

<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->

<property name="cache.provider_class">org.hibernate.cache.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="">none</property>
<mapping class="net.roseindia.table.Employee" />


In <hibernate-configuration> you configure your database connectivity, set sql dialect ,create database schema etc.
Here hibernate is your database name which contain tables. ?net.roseindia.table.Employee" is your class in which you are mapping your table.

Step 2. Persistent class:

Now create Persistent class ?Hibernate uses the Plain Old Java Object (POJO) classes to map to the database table.

Here is the code of

package net.roseindia.table;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "employee") //database table name employee
public class Employee implements Serializable {

private int empId;

@Column(name = "emp_name", nullable = false)
private String empName;

@Column(name = "emp_salary", nullable = false)
private int salary;

@Column(name = "designation", nullable = false)
private String designation;

@Column(name = "address", nullable = false)
private String address;

public int getEmpId() {
return empId;

public void setEmpId(int empId) {
this.empId = empId;

public String getEmpName() {
return empName;

public void setEmpName(String empName) {
this.empName = empName;

public int getSalary() {
return salary;

public void setSalary(int salary) {
this.salary = salary;

public String getDesignation() {
return designation;

public void setDesignation(String designation) {
this.designation = designation;

public String getAddress() {
return address;

public void setAddress(String address) {
this.address = address;


Here we are using annotation for mapping our table employee to our class

Step3.  Utility class:  Now create an util class as

package net.roseindia.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;

	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure()

		} catch (HibernateException exception) {

	public static SessionFactory getSessionFactory() {
		return sessionFactory;

Step 4. Main Class:  Now we are going to create our main class.Here we are inserting data in to employee table. saves the employee object into the database table.

package net.roseindia.application;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class MainClaz {
public static void main(String[] args) {

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee = new Employee();



Hibernate: select employee0_.empId as empId0_0_, employee0_.address as address0_0_, employee0_.designation as designat3_0_0_, employee0_.emp_name as emp4_0_0_, employee0_.emp_salary as emp5_0_0_ from employee employee0_ where employee0_.empId=?
Hibernate: insert into employee (address, designation, emp_name, emp_salary) values (?, ?, ?, ?)

Download source code


Share on Google+Share on Google+

Hibernate Example

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



Discuss: Hibernate Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image