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.

Employee.java

package mypack;
public class Employee {
	private int id;
	private String name,job;
	private int salary;
	public int getId() {
		return id;
	}
	public Employee(){
		super();
	}
	public void setId(int id) {
		this.id = id;
	}
	public Employee(String name, String job, int salary) {
		super();
		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.

hibernate.cfg.xml

<?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">
<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 test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/databaseName
   </property>
   <property name="hibernate.connection.username">
      your_user_name_here
   </property>
   <property name="hibernate.connection.password">
      your_password_here
   </property>

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

</session-factory>
</hibernate-configuration>

Create Mapping files for Persistent class

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

Employee.hbm.xml

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

Create Main Class

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

Main.java

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); 
	 s.save(e1);
         s.save(e2);
	 t.commit();
	 s.close();
	 System.out.println("Saved successfully");
	}
}

Leave a Reply