Hibernate: Performing One-To-One Bidirectional Mapping Using XML Mapping

Contents

Pre-Requisite

Concept Overview

Required Libraries

Diagrammatic Representation

Sample Program


In this tutorial we model a one to one bidirectional relationship between two entities.


This tutorial is divided primarily into the following sections:

  1. Concept Overview
  2. Sample program

Pre-requisite

  • Basic familiarity with JDBC (Java DataBase Connectivity) API.
  • Relationship between entities in database using primary key and foreign key constraints.
  • Performaing basic database operations on an entity (see this tuturial for further details)

Also see the tutorial on
one to one unidirectional relationship

Concept Overview

In real life, Entities can have inter-relationships among themselves. e.g. A School has many Students, a Social Event can have many Participants and one Participant can attend many Social Events, etc.

The key elements of relationship between two entities are Cardinality and Directionality.

Cardinality:
It represents the
number
of the entity under a relationship e.g. If one Person has one Address then the cardinality of Person and Address is one. The cardinality of a relationship could be one-to-one, one-to-many or many-to-many.

Directionality:
It represents the allowable way to navigate from one entity to another e.g. If there is a unidirectional relationship from Person to Address, then if one has an instance of Person object then one can traverse from Person object to Address object but not vice-versa.

However, In a bidirectional relationship between Person and Address, it is possible to navigate from Person object to Address object and vice versa. The directionality could typically be unidirectional (from one entity to another) or bi-directional.

In this sample program, we shall consider a one-to-one relationship between a Person entity and Address entity with bidirectional navigation between Person to Address entities.

Required Libraries

  • antlr-2.7.7.jar
  • dom4j-1.6.1.jar
  • hibernate-commons-annotations-4.0.4.Final.jar
  • hibernate-core-4.3.5.Final.jar
  • hibernate-jpa-2.1-api-1.0.0.Final.jar
  • hsqldb.jar
  • jandex-1.1.0.Final.jar
  • javassist-3.18.1-GA.jar
  • jboss-logging-3.1.3.GA.jar
  • jboss-logging-annotations-1.2.0.Beta1.jar
  • jboss-transaction-api_1.2_spec-1.0.0.Final.jar
  • log4j.jar

Diagrammatic Representation




Sample Program


In this sample program, we shall create a one to one bidirectional relationship between Person and Address entities. We shall create these entities in the database and then access them. First we shall Person entity and then traverse it to get the Address entity. Then we shall fetch the Address entity and then traverse it to get the Person entity. This way we shall verify the bidirectionality of the relationship.

Source Package Structure

Source Code


Create the Person class (see below) that represents an entity involved in the one-to-one relationship.

Create the members personId, firstName, lastName and address (see lines 6-12 below) and the corresponding accessor methods (see lines 26-49 below).

In particular, note that the Address member represents the one-to-one relationship between Person and Address class (see line 12 below).


Create the Address class (see below) which represents the other entity involved in one-to-one relationship.

Create members addressId, street, city, state, zipCode and person(see lines 6-14 below).

In particular, note that there is reference to person entity (see line 14 below) that represents the relationshp between Address and Person. Thus if we get a reference to the Address object then we can get a reference to the Person object.

Similarly, we have seen in
Person
earlier that if we get a reference to the Person object then we can get a reference to the Address object.

This demonstates how to model a one-to-one bidirectional relationship between Person and Address entities.


Create Person.hbm.xml as shown below.

Map the Person class to PERSON table (see line 6 below).

Map the identifier personId to the corresponding primary key PERSON_ID (see line 7 below).

Map the other members firstName and lastName to their corresponding columns (see lines 10-11 below).

Map the one to one relationship between Person and Address using the
one-to-one
tag (see line 14 below). Note that Hibernate provides a very direct and evocative way to map a one to one relationship by using the
one-to-one
tag.


Create the Address.hbm.xml mapping file as shown below.

Map the Address class to ADDRESS table (see line 6 below).

Map the other members like street, city, state and zipCode to their corresponding columns (see lines 10-13 below)

Map the one to one relationship between Address and Person using the
one-to-one
tag (see line 14 below). Note that Hibernate provides a very direct and evocative way to map a one to one relationship by using the
one-to-one
tag.


Create the configuration file for Log4J which is used to log the output of the sample program.


Create the HibernateUtil class which is utility class to create Hibernate SessionFactory.

The important sections in this class are:

  • Load Hibernate configuration file (see lines 14-15 below)
  • Build thie session factory using this configuration (see line 21 below)


Create the client program (as shown below) that performs add a Person along with his address and then fetches both Person and Address entities.

For more details on performing basic database operations refer to
here

  • Create the addPerson() method (see lines 16-29). Create a Person along his Address and save it to the database using Session.save() (see lines 21-25 below)
  • Create the fetchAllPersons() method (see lines 31-44 below) using Criteria API. From the Person object access the Address (see line 39 below)
  • Create the fetchAddAddresses() method (see lines 46-59 below) using Criteria API. From the Address object access the Person (see line 54 below)
  • Create the execute() method (see lines 63-80) and make calls to addPerson(), fetchAllPersons() and fetchAllAddresses() method
  • Create the main() method (see lines 82-84 below) and call execute() method

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) 1.5 or above on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save hibernateonetoonebidirectionalxml-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment
    (Alternatively you can go the folder containing the ?-installer.jar and execute the jar using java -jar ?-installer.jar command)
  • You will see a wizard as shown below:
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below:
    • Adding a Person and his Address to database
    • Fetching the Person entity and then accessing the Address entity

    • Fetching the Address entity and then accessing the Person entity

    This demonstrates the successful exection of mapping a one to one bidirectional relationship using Hibernate.

Browsing the Program

This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called hibernatebasiccrudoperations. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.



Leave a Comment