Hibernate XML Mapping Example

Hibernate is an Object-Relational Mapping(ORM) framework developed by Gavin King in 2001. It maps Java classes to database tables and do the persistent operations like save,update and delete.
Here we are taking example to persist a Employee object through XML configuration with MySQL.

Required jars

  • antlr-2.7.6
  • cglib-2.2
  • commons-collections-3.1
  • dom4j
  • ehcache-1.2.3
  • hibernate3
  • javassist-3.9.0.GA
  • jta-1.1
  • log4j-1.2.14
  • slf4j-api-1.5.8
  • slf4j-log4j12-1.5.8

Dowload Jars

Persistent class

Here we are creating a POJO class to map the class with the database table.We define the all the attributes for the table columns.


package mypack;
public class Employee {
	private int id;
	private String name,job;
	private int salary;
	public int getId() {
		return id;
	public Employee(){
	public void setId(int id) {
		this.id = id;
	public Employee(String name, String job, int salary) {
		this.name = name;
		this.job = job;
		this.salary = salary;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getJob() {
		return job;
	public void setJob(String job) {
		this.job = job;
	public int getSalary() {
		return salary;
	public void setSalary(int salary) {
		this.salary = salary;


Create Configuration file

The configuration file keeps the information regarding database connection url, driver class name , database user name, password etc.Conventionally the configuration file name should be hibernate.cfg.xml.This file also contains the information about mapping files for persistent classes.


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
   <property name="hibernate.dialect">
   <property name="hibernate.connection.driver_class">

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
   <property name="hibernate.connection.username">
   <property name="hibernate.connection.password">

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


Create Mapping files for Persistent class

Here we configure the mapping about Persistent class with the database table.We are describing about columns.


<?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"> 
   <class name="mypack.Employee" table="employee">
      <meta attribute="class-description">
         This class contains the employee detail. 
      <id name="id" type="int" column="id">
         <generator class="native"/>
      <property name="name" column="name" type="string"/>
      <property name="job" column="job" type="string"/>
      <property name="salary" column="salary" type="int"/>

Create Main Class

Now we are creating a class with main method to persist objects.


package mypack;

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

public class Main {

	 * @param args
	public static void main(String[] args) {
	 //creating configuration object  
          Configuration cfg=new Configuration();  
          cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
       //creating seession factory object  
       SessionFactory factory=cfg.buildSessionFactory();  
       //creating session object  
       Session session=factory.openSession();  
      //creating transaction object  
       Transaction t=session.beginTransaction();  
       Employee e1 = new Employee("Raju","Java Developer",40000);
       Employee e2 = new Employee("Amit","Java Developer",50000); 
	 System.out.println("Saved successfully");

Leave a Reply