SpringDataJPA : One to one Mapping in Spring Boot Hibernate JPA with Spring Data
Also Read
Let's create two classes Employee and Address, such that they have the following relation between them.
Employee has-a Address
which means a one-to-one
mapping between the two and Employee
is the owner of the relation.
- 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.
This means that Employee can stay without an Address but Address cannot stay without the Employee. In even more simpler words, If employee is deleted his address should also be deleted, but if address is deleted employee should not 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;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", dateCreated=" + dateCreated +
", lastUpdated=" + lastUpdated +
", email='" + email + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
// getters and setters
}
Address.java
package com.ekiras.domain;
import javax.persistence.*;
import java.util.Date;
/**
* @author ekiras
*/
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Temporal(TemporalType.TIMESTAMP)
protected Date dateCreated;
@Temporal(TemporalType.TIMESTAMP)
protected Date lastUpdated;
private String address;
@OneToOne(fetch = FetchType.LAZY)
private Employee employee;
@Override
public String toString() {
return "Address{" +
"id=" + id +
", dateCreated=" + dateCreated +
", lastUpdated=" + lastUpdated +
", address='" + address + '\'' +
", employee=" + employee +
'}';
}
// getters and setters
}
Following Tables will be created from the Entities defined above.
mysql> show tables;
+-----------------------+
| Tables_in_jpa_mapping |
+-----------------------+
| address |
| employee |
+-----------------------+
2 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 | |
+--------------+--------------+------+-----+---------+----------------+
6 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.00 sec)
You can save the object's as shown in the example below
public void test(){
// create employee object
Employee employee = new Employee();
employee.setName("Ekansh");
employee.setEmail("ekansh@ekiras.com");
employee.setPassword("pwd");
// create Address object
Address address = new Address();
address.setAddress("Some Address");
employee.setAddress(address);
// actual save call for the object
employeeRepository.save(employee);
}
Following are the images that show the working of the One-TO-One mapping.
No comments: