We have launched a free Basic Kubernetes Course. Click here to enrol now! Kubernetes is a container orchestration platform used by enterprises worldwide and Kubernetes developers are in high demand!
In this tutorial we model a one to many bidirectional relationship between two entities.
This tutorial is divided primarily into the following sections:
- Concept Overview
- Sample program
- 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)
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.
It represents the
of the entity under a relationship e.g. If one Department has many Employees then the cardinality this relationship is one-to-many. The cardinality of a relationship could be one-to-one, one-to-many or many-to-many.
It represents the allowable way to navigate from one entity to another e.g. If there is a unidirectional relationship from Department to Employee, then if one has an instance of Department object then one can traverse from Department object to Employee object but not vice-versa.
However, In a bidirectional relationship between Department and Employee, it is possible to navigate from Department object to Employee 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-many relationship between a Department entity and Employee entity with bidirectional navigation between Department to Employee entities.
In this sample program, we shall create a one to many bidirectional relationship between Department and Employee entities. This is a one-to-many relationship i.e. one Department has many Employees.
We shall create these entities in the database and then access them. First we shall Department entity and then traverse it to get the Employee entity. Then we shall fetch the Employee entity and then traverse it to get the Department entity. This way we shall verify the bidirectionality of the one-to-many relationship.
Source Package Structure
Create the Department class (see below) that represents an entity involved in the one-to-many relationship.
Create the members departmentId, name and Set
In particular, note that the Set
Create the Employee class (see below) which represents the other entity involved in one-to-many relationship.
Create members employeeId, age, salary and department(see lines 6-10 below) along with their accessor methods (see lines 25-60 below).
In particular, note that there is reference to department entity (see line 10 below) that represents the relationshp between Employee and Department. Thus if we get a reference to the Employee object then we can get a reference to the Department object.
Similarly, we have seen in Department earlier that if we get a reference to the Person object then we can get a reference to the Employee object.
This demonstates how to model a one-to-many bidirectional relationship between Employee and Department entities.
Create Department.hbm.xml as shown below.
Map the Department class to DEPARTMENT table (see line 6 below).
Map the identifier departmentId to the corresponding primary key DEPARTMENT_ID (see line 7 below).
Map the name to its corresponding column NAME (see line 10 below).
Map the one to many relationship between Department and Employee using the set tag (see line 12 below).
- inverse=”true” (see line 12 below) attribute indicates that the Department entity is responsible for taking care of the one-to-many relationship
- the key tag (see line 13 below) indicates that the DEPARTMENT_ID in EMPLOYEE table is the foreign key corresponding to the DEPARTMENT_ID in DEPARTMENT table
- the one-to-many tag (see line 14 below) is indicates that one Department and many Employees
Create the Employee.hbm.xml mapping file as shown below.
Map the Employee class to EMPLOYEE table (see line 6 below).
Map the identifier employeeId to EMPLOYEE_ID (see line 7 below).
Map the other members like name, age and salary to their corresponding columns (see lines 10-12 below)
Use the many-to-one tag to indicate that many Employees belong to one Department (see lines 15-17 below below).
- Database connection settings (see lines 8-11 below)
- Hibernate dialect for HyperSQL database (see line 17 below)
- Reference to Department and Employee mapping resources (see lines 32-33 below)
Create the client program (as shown below) that adds a Department along with its Employees and then fetches both Department and Employee entities.
For more details on performing basic database operations refer to here
- Create the insertDepartmentAndEmployees() method (see lines 25-44). Create a Department along its Employess and save it to the database using Session.save()
- Create the fetchAllDepartments() method (see lines 46-61 below) using Criteria API. From the Department object access the Employees (see line 54 below)
- Create the fetchAllEmployees() method (see lines 63-74 below) using Criteria API. From the Employee object access the Department (see line 69 below)
- Create the execute() method (see lines 15-23) and make calls to insertDepartmentAndEmployees(), fetchAllDepartments() and fetchAllEmployees() methods
- Create the main() method (see lines 76-78 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.
- Save hibernateonetomanybidirectionalxml-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 hibernateonetomanybidirectionalxml-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 Deparment and its Employees to database
- Fetching the Employee entity and then accessing the Department entity
Fetching the Department entity and then accessing the Employee entity
This demonstrates the successful exection of mapping a one to many bidirectional relationship using XML based mapping using Hibernate.
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.