Jpa One To One Mapping Example

By | | Updated : 2021-03-14 | Viewed : 118 times

Jpa One To One Mapping Example

The current one to one relationship example in JPA shows one to one relationships in between two entities. Here we will learn the one to one relationship with a foreign key, shared primary key, and join table.

JPA One To One Mapping With Foreign Key

Here we will have to have two tables to show one to one relationship with the foreign key in JPA. Here are those tables 1. employee 2. employee_details

Jpa One To One Mapping Example

Now we will see the code snippets for one to one mapping relationship with foreign key.

Employee.java for JPA One To One Mapping With Foreign Key
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

    @Id
    @Column(name = "EMP_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long empId;

    @OneToOne(mappedBy = "employee")
    private EmployeeDetails empDet;

}
EmployeeDetails.java for JPA One To One Mapping With Foreign Key
@Entity
@Table(name = "EMPLOYEE_DETAILS")
public class EmployeeDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "EMP_DET_ID")
    private Long empDetId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "EMP_ID", referencedColumnName = "EMP_ID")
    private Employee employee;
	
}

We used here @OneToOne annotation in both Employee and EmployeeDetails classes. In EmployeeDetails class we used @JoinColumn. It is indicating the foreign key in EmployeeDetails which is referred to by Employee.

Please find the Git repo for JPA-OnetoOne-ForeignKey-Annotation-Example-App

JPA One To One Mapping With Shared Primary Key

To illustrate the one to one to relationship with the shared primary key we need two tables. Those tables are 1. product 2. product_details

Jpa One To One Mapping Example

Now we will see the code snippets for JPA One To One Mapping With Shared Primary Key.

Product for JPA One To One Mapping With Shared Primary Key
@Entity
@Table(name = "PRODUCT")
public class Product {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "PROD_ID")
	private long prodId;
}
ProductDetails for JPA One To One Mapping With Shared Primary Key
@Entity
@Table(name = "PRODUCT_DETAILS")
public class ProductDetails {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "PROD_DET_ID")
	private long prodDetId;

	@OneToOne(cascade = CascadeType.ALL)
	@PrimaryKeyJoinColumn
	private Product product;
}

Notice ProductDetails contains the @PrimaryKeyJoinColumn used with annotation @OneToOne. Here @PrimaryKeyJoinColumn is used for the shared primary key. Here PROD_DET_ID is the primary key of the table PRODUCT_DETAILS where it is also a foreign key referred primary key PROD_ID of table PRODUCT.

JPA One To One Mapping With Join Table

In this example, we need a third table for storing the primary keys for the first two tables. Here first two tables are 1. employee 2. organization and the third table is 3. employee_organization.

Jpa One To One Mapping Example

You can see the relationship between the three tables. So now we will see the code snippet for the same relationship.

Employee for JPA One To One Mapping With Join Table
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

	@Id
	@Column(name = "EMP_ID")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long empId;
	
	@OneToOne(cascade = CascadeType.ALL)
	@JoinTable(name="EMPLOYEE_ORGANIZATION", joinColumns = @JoinColumn(name="EMP_ID", referencedColumnName = "EMP_ID"),
	inverseJoinColumns = @JoinColumn(name="ORG_ID", referencedColumnName = "ORG_ID"))
	private Organization organization;
	
}
Organization for for JPA One To One Mapping With Join Table
@Entity
@Table(name = "ORGANIZATION")
public class Organization implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ORG_ID")
	private Long orgId;
}

Notice we used @OneToOne annotation with @JoinTable. @JoinTable is used for the join tables where the Join table contains the relationship of the other two tables. This means the other two table\'s primary keys exist in the join table. So here if you persist data of employee and organization records then one more entry will be added to employee_organization.

Please find the Git repo for JPA-OneToOne-JoinTable-Annotation-Example-App

Leave A Reply