Home Hibernate Hibernate4 Hibernate XML Mapping

Related Tutorials


 
 

Share on Google+Share on Google+

Hibernate XML Mapping

Advertisement
Hibernate XML Mapping

Hibernate XML Mapping

In this section, you will learn how to do XML mapping in Hibernate.

In Hibernate, XML file is used for two things : First, for configuring the database(hibernate.cfg.xml), Second, for mapping database table to POJO class. 

The sample XML file used for configuring the database is given below :

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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.10.13:3306/anky</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">validate</property>

<mapping class="net.roseindia.WorkerDetail"/>
<mapping class="net.roseindia.Worker"/>

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

XML Mapping

The sample XML mapping file used for mapping Java class to database table is given below :

person.hbm.xml

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="roseindia">
<class name="Person" table="person">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
</id>

<property name="name">
<column name="Name" />
</property>

</class>
</hibernate-mapping>

XML one to one mapping

The XML mapping file used in one-to-one mapping is given below :

Worker.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">

<class name="Worker" table="worker">
<id name="workerId" column="worker_id">
<generator class="native" />
</id>
<one-to-one name="workerDetail" class="net.roseindia.WorkerDetail"
cascade="save-update"></one-to-one>

<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />

</class>
</hibernate-mapping>

WorkerDetail.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">

<class name="WorkerDetail" table="workerdetail">

<id name="workerId" type="java.lang.Long">
<column name="worker_id" />
<generator class="foreign">
<param name="property">worker</param>
</generator>
</id>
<one-to-one name="worker" class="net.roseindia.Worker"
constrained="true"></one-to-one>

<property name="street" column="street" />
<property name="city" column="city" />
<property name="state" column="state" />
<property name="country" column="country" />
</class>

</hibernate-mapping>

XML One to Many Mapping

The one to many relation between two tables is shown below :

The XML one-to-many mapping is given below:

Address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Address" table="address">
<id name="addressId" column="address_id">
<generator class="native" />
</id>
<property name="street" column="street"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="country" column="country"/>
<set name="employees" table="employee"
inverse="true" lazy="true" fetch="select">
<key>
<column name="address_id" not-null="true" />
</key>
<one-to-many class="net.roseindia.Employee" />
</set>
</class>
</hibernate-mapping>

Employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">

<class name="Employee" table="employee">
<id name="employeeId" column="employee_id">
<generator class="native" />
</id>

<property name="firstname" column="firstname"/>
<property name="lastname" column="lastname" />
<property name="cellphone" column="cell_phone" />

<many-to-one name="address" class="net.roseindia.Address" fetch="select">
<column name="address_id" not-null="true" />
</many-to-one>

</class>
</hibernate-mapping>

XML Many to Many Mapping

The XML Many to Many relation shown below using figure:

The XML many-to-many mapping is given below :

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Student" table="student">
<id name="studentId" column="student_id">
<generator class="native" />
</id>

<property name="studentName" column="student_name"/>

<set name="courses" table="student_course"
inverse="false" lazy="true" fetch="select" cascade="all">
<key column="student_id" />
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>

Course.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Course" table="course">
<id name="courseId" column="course_id">
<generator class="native" />
</id>

<property name="courseName" column="course_name"/>

<set name="students" table="student_course"
inverse="false" lazy="true" fetch="select" cascade="all">
<key column="student_id" />
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 12, 2012

Related Tutorials

Discuss: Hibernate XML Mapping  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com