Hibernate Mapping Files

In this section I will describe you the necessary Hibernate Mapping Files that is required to develop a hibernate application.



Hibernate Mapping Files


Hibernate Mapping Files:

In this section I will describe you the necessary Hibernate Mapping Files that is required to develop a hibernate application. Hibernate is very flexible O/R mapping tool. It works with many databases and it's very easy to move the application from one database to another database.

Hibernate is configured at two levels:

  1. Hibernate service
  2. Persistence class configuration

Hibernate Service configuration

To configure the Hibernate service usually hibernate.cfg.xml file is used. The default name of configuration file is hibernate.cfg.xml, but it is also possible to give different name to the configuration file.

While configuring the Hiberbate Service we provide database connection url and passwords. We also provide different caching level for the application performance.

Here is the code of hibernate.cfg.xml file used in our example application

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD//EN"




<property name="hibernate.connection.driver_class">

<property name="hibernate.connection.url">

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.connection.pool_size">10</property>

<property name="show_sql">true</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping files -->

<mapping resource="employee.hbm.xml"/>



Configuration parameter description:

hibernate.connection.driver_class is used to specify the JDBC Driver class

hibernate.connection.url is database connection url

hibernate.connection.username is username to access database

hibernate.connection.password is the password to connect to the database

dialect property is used to specify the database dialect. Here in this case it is org.hibernate.dialect.MySQLDialect. You can read more about hibernate dialect here.

Configuring the Persistence class

Usually <persistence-object>.hbm.xml file is used to configure the Persistence object. Here is the example employee.hbm.xml configuration used in our application.


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"



<class name="roseindia.Employee" table="employee">

<id name="empId" type="int" column="Emp_id" unsaved-value="0">

<generator class="increment"/>


<property name="empName" type="java.lang.String"
   column="Emp_name" not-null="true" length="50" />

<property name="empSal" type="java.lang.Double"
  column="Emp_sal" not-null="true" />



In nutshell we need one configuration file and one hibernate mapping files to develop the application. These are:

  • hibernate.cfg.xml and
  • <persistence-object>.hbm.xml
Share on Google+Share on Google+

Hibernate Mapping Files

Posted on: September 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Mapping Files  

Post your Comment

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