SpringDataJPA : One to Many Mapping in Spring Boot Hibernate JPA with Spring Data

Also Read

  1. One To One Mapping

Let's create two entities/domains Employee and Department such that they have the following relation between them.

Department has-many Employees which means an Employee can belong to only one Department and a Department can have many Employee and Department is the owner of the relation between the two.

Owner of the relation means that Owner can exist without the dependent entity but dependent entity cannot stay without the owner entity.

Dependent Entity of Relationship will containes the 'foreign key' ID of the Owner entity. In this case, Address will contian the Employee Id in >its table as shown in the table structure below.

So a Department can exist without Employee but an Employee cannot be there without a Department. If you

  1. on deleting an Employee, the Employee-Department mapping will be removed
  2. on deleting a Department, all the employees in the department should be deleted.

Employee.java

package com.ekiras.domain;

import javax.persistence.*;
import java.util.Date;

/**
 * @author ekiras
 */
@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected long id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date lastUpdated;

    private String email;
    private String name;
    private String password;
    
    @OneToOne(mappedBy = "employee",optional = false,cascade = CascadeType.ALL)
    private Address address;

    @ManyToOne(fetch = FetchType.EAGER)
    private Department department;

    // getters and setters

Department.java

package com.ekiras.domain;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author ekiras
 */
@Entity
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date dateCreated;

    @Temporal(TemporalType.TIMESTAMP)
    protected Date lastUpdated;

    private String name;

    @OneToMany(cascade = CascadeType.ALL,mappedBy = "department", fetch = FetchType.LAZY)
    private List<Employee> employees;

    // getters and setters
}

Following Tables will be created from the Entities defined above.

mysql> show tables;
+-----------------------+
| Tables_in_jpa_mapping |
+-----------------------+
| address               |
| department            |
| employee              |
+-----------------------+
3 rows in set (0.00 sec)

mysql> desc employee;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| date_created  | datetime     | YES  |     | NULL    |                |
| email         | varchar(255) | YES  |     | NULL    |                |
| last_updated  | datetime     | YES  |     | NULL    |                |
| name          | varchar(255) | YES  |     | NULL    |                |
| password      | varchar(255) | YES  |     | NULL    |                |
| department_id | bigint(20)   | YES  | MUL | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> desc department;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| date_created | datetime     | YES  |     | NULL    |                |
| last_updated | datetime     | YES  |     | NULL    |                |
| name         | varchar(255) | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc address;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| address      | varchar(255) | YES  |     | NULL    |                |
| date_created | datetime     | YES  |     | NULL    |                |
| last_updated | datetime     | YES  |     | NULL    |                |
| employee_id  | bigint(20)   | YES  | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

Following are the images that show the working of the One-To-Many mapping.

List of Departments
img

Adding Employee
img

Employee Details
img

List of all Employees
img

List of Employees in a Department
img

No comments :

Post a Comment