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:
- Hibernate service
- 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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatemappings</property>
<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>
<property
name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
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.
employee.hbm.xml
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.Employee"
table="employee">
<id name="empId" type="int"
column="Emp_id" unsaved-value="0">
<generator
class="increment"/>
</id>
<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" />
</class>
</hibernate-mapping> |
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

|