Hibernate Configuration in Eclipse

  • Step-1 Download hibernate-distribution-3.6.4.Final from http://www.hibernate.org/downloads
  • Step-2 Add below given jar files into classpath by rightclick on project >> properties >> java buildpath >> add library >> choose user library from the list >> click to user libraries >> click to new tab >> enter the user library name and click ok >> choose your library and click to add jars tab.
    • hibernate3.jar
    • antlr-2.7.6.jar
    • common-collections-3.1.jar
    • dom4j-1.6.1.jar
    • javassist-3.12.0.GA.jar
    • jta-1.1.jar
    • sl4j-api-1.6.1.jar
    • hibernate-jpa-2.0-api-1.0.0.Final.jar
    • mysql-connector-java-5.0.8-bin.jar download it from

  • http://dev.mysql.com/downloads/connector/j/5.0.php

    Download Required jar for Project Setupclick here

  • Step-3 Create XML file by right-click on project src folder >> New >> other >> XML >> XML file >> enter file name hibernate.cfg.xml >> finish.

  • Hibernate XML file

  • Step-4 Create another XML file by right-click on project src folder >> New >> other >> XML >> XML file >> enter file name Employee.hbm.xml >> finish.

  • Hibernate XML file next

  • Step-5 Create java class by right-click on project >> New >> class >> class name >> finish.

  • Adding Hibernate class

  • Step-6 Create another java class by right-click on project >> New >> class >> class name >> finish.

  • Adding Hibernate class next

Simple Example Of Hibernate

hibernate.cfg.xml file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 

   <property name="hibernate.dialect"> 
   org.hibernate.dialect.MySQLDialect </property>
   <property name="hibernate.connection.driver_class"> 
   com.mysql.jdbc.Driver </property>

   <!-- Assume hibernatedb as the database name -->
   <property name="hibernate.connection.url"> 
   jdbc:mysql://localhost/hibernatedb </property>
   <property name="hibernate.connection.username">
   <property name="hibernate.connection.password">
   <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
   <!-- Drop and re-create the database 
   		schema on startup -->
   <property name="hbm2ddl.auto">create</property>	

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

Employee.java file
package com.tkhts;

public class Employee {
	private int id;
	private String firstName;
	private String lastName;
	private int salary;

	public Employee(String firstName, 
			String lastName, int salary){
		this.firstName = firstName;
		this.lastName = lastName;
		this.salary = salary;
	public int getId() {
		return id;
	public void setId(int id) {
		this.id = 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 getSalary() {
		return salary;
	public void setSalary(int salary) {
		this.salary = salary;
Employee.hbm.xml file

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 <class name="com.tkhts.Employee"
 	<id name="id" column="ID" type="int">
 		<generator class="native"></generator>
 	<property name="firstName"
  	<property name="lastName" column="last_name"
   	<property name="salary" column="salary"
TestEmployee.java file
package com.tkhts;

import org.apache.log4j.chainsaw.Main;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TestEmployee {
	private static SessionFactory sesssionFactory;
	public static void main(String args[]){
		sesssionFactory = new 
		TestEmployee employ = new TestEmployee();
		employ.addEmployee("abc", "xyz", 100000);
		// pass EmployeeID and salary as
		// parameter to deleteEmployee 
		method to delete the employee
		// employ.deleteEmployee(1, 1200000);
	public void addEmployee
		(String fName,String lName, int salary){
		Session session = sesssionFactory.openSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		Employee emp = new Employee(fName, lName, salary);
	/* Method to UPDATE salary for an employee */
	   public void deleteEmployee
	   	(Integer EmployeeID, int salary ){
	      Session session = sesssionFactory.openSession();
	      Transaction tx = null;
	         tx = session.beginTransaction();
	         Employee employee = (Employee)session.get
	         	(Employee.class, EmployeeID); 
		// This method set the salary of employee
		// employee.setSalary( salary );
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	      catch(Exception e){
	    	  if (tx!=null) tx.rollback();
	      finally {
Output On Console
SLF4J: Failed to load class
SLF4J: Defaulting to no-operation 
	(NOP) logger implementation
SLF4J: See 
		for further details.
Hibernate: insert into EMPLOYEES_TABLE 
(first_name, last_name, salary) values (?, ?, ?)
Table in Mysql
Hibernate Output