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


  • 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 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <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">
   root
   </property>
   <property name="hibernate.connection.password">
   root
   </property>
   
   <!-- 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"/>

   </session-factory>
</hibernate-configuration>
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"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 
<hibernate-mapping>
 <class name="com.tkhts.Employee"
 	table ="EMPLOYEES_TABLE">
 	<id name="id" column="ID" type="int">
 		<generator class="native"></generator>
 	</id>
 	<property name="firstName"
 		column="first_name"
 			type="string"></property>
  	<property name="lastName" column="last_name"
  		type="string"></property>
   	<property name="salary" column="salary"
   		type="int"></property>
 </class>
 
</hibernate-mapping>
		
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 
		Configuration().configure().buildSessionFactory();
		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);
		session.save(emp);
		tx.commit();
	}
	
	
	/* Method to UPDATE salary for an employee */
	   public void deleteEmployee
	   	(Integer EmployeeID, int salary ){
	      Session session = sesssionFactory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Employee employee = (Employee)session.get
	         	(Employee.class, EmployeeID); 
		// This method set the salary of employee
		// employee.setSalary( salary );
		session.delete(employee); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }
	      catch(Exception e){
	    	  if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
	      }
	      finally {
	         session.close(); 
	      }
	   }
}
Output On Console
SLF4J: Failed to load class
	"org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation 
	(NOP) logger implementation
SLF4J: See 
	http://www.slf4j.org/codes.php#StaticLoggerBinder 
		for further details.
Hibernate: insert into EMPLOYEES_TABLE 
(first_name, last_name, salary) values (?, ?, ?)
		
Table in Mysql
Hibernate Output