Search

Hibernate One-To-One Mapping Example

     In this example you will learn how to map one-to-one relationship using Hibernate. Consider the following relationship between Student and Address entity.
According to the relationship each student should have a unique address.To create this relationship you need to have a STUDENT and ADDRESS table. The relational model is shown below.
To create the STUDENT and ADDRESS table you need to create the following hibernate mapping files.
Student.hbm.xml is used to create the STUDENT table.

01.<?xml version="1.0"?>
02.<!DOCTYPE hibernate-mapping PUBLIC
03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"
05.<hibernate-mapping>
06.<class name="com.vaannila.student.Student" table="STUDENT">
07.<meta attribute="class-description">This class contains student details.</meta>
08.<id name="studentId" type="long" column="STUDENT_ID">
09.<generator class="native" />
10.</id>
11.<property name="studentName" type="string" not-null="true"length="100" column="STUDENT_NAME" />
12.<many-to-one name="studentAddress"class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" />
13.</class>
14.</hibernate-mapping>
We use the many-to-one element to create the one-to-one relationship between the Student andAddress entities. We do this my making the STUDENT_ADDRESS column unique in the STUDENTtable.
Address.hbm.xml is used to create the ADDRESS table.

01.<?xml version="1.0"?>
02.<!DOCTYPE hibernate-mapping PUBLIC
03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"
05.<hibernate-mapping>
06.<class name="com.vaannila.student.Address" table="ADDRESS">
07.<meta attribute="class-description">This class contains the student's address
08.details.</meta>
09.<id name="addressId" type="long" column="ADDRESS_ID">
10.<generator class="native" />
11.</id>
12.<property name="street" column="ADDRESS_STREET" type="string"length="250" />
13.<property name="city" column="ADDRESS_CITY" type="string" length="50"/>
14.<property name="state" column="ADDRESS_STATE" type="string"length="50" />
15.<property name="zipcode" column="ADDRESS_ZIPCODE" type="string"length="10" />
16.</class>
17.</hibernate-mapping>
Now create the hibernate configuration file and add all the mapping files.

01.<?xml version="1.0" encoding="UTF-8"?>
02.<!DOCTYPE hibernate-configuration PUBLIC
03."-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.<hibernate-configuration>
06.<session-factory>
07.<property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver </property>
08.<property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhost<;/property>
09.<property name="hibernate.connection.username">sa</property>
10.<property name="connection.password"></property>
11.<property name="connection.pool_size">1</property>
12.<property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect </property>
13.<property name="show_sql">true</property>
14.<property name="hbm2ddl.auto">create-drop</property>
15.<mapping resource="com/vaannila/student/Student.hbm.xml"/>
16.<mapping resource="com/vaannila/student/Address.hbm.xml"/>
17.</session-factory>
18.</hibernate-configuration>
After creating the configuration file, generate java class files using Hibernate Tools.(To generate code using Hibernate Tools refer this example )
The following classes will be generated.
Create the Main class to run the example.

01.package com.vaannila.student;
02. 
03.import org.hibernate.HibernateException;
04.import org.hibernate.Session;
05.import org.hibernate.Transaction;
06. 
07.import com.vaannila.util.HibernateUtil;
08. 
09.public class Main {
10. 
11.public static void main(String[] args) {
12.Session session = HibernateUtil.getSessionFactory().openSession();
13.Transaction transaction = null;
14.try {
15.transaction = session.beginTransaction();
16.Address address1 = new Address("OMR Road""Chennai""TN","600097");
17.Address address2 = new Address("Ring Road""Banglore","Karnataka""560000");
18.Student student1 = new Student("Eswar", address1);
19.Student student2 = new Student("Joe", address2);
20.session.save(student1);
21.session.save(student2);
22.transaction.commit();
23.catch (HibernateException e) {
24.transaction.rollback();
25.e.printStackTrace();
26.finally {
27.session.close();
28.}
29. 
30.}
31. 

No comments:

Post a Comment