Implementing bidirectional one-to-one association in JPA

scene

Introduction to JPA and building HelloWorld (with code download):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937

In JPA, one-way many to one association is implemented:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103511623

To realize one-way one to many association in JPA:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103520083

In JPA, two-way one to many association relations are implemented:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103523564

After implementing the above mapping relationship according to the above process, how to implement the bidirectional one-to-one mapping relationship in JPA.

For example, department and manager are two-way one-to-one relationship.

Note:

Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

In order to construct a two-way one-to-one association relationship, two database tables and entity classes, manager and department, are added.

New database table JPA manager table

 

 

JPA? Departments

 

 

Then create a new Department entity class department

package com.badao.jpa.helloworld;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Table(name="JPA_DEPARTMENTS")
@Entity
public class Department {

 private Integer id;
 private String deptName;
 
 private Manager mgr;

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Id
 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @Column(name="DEPT_NAME")
 public String getDeptName() {
  return deptName;
 }

 public void setDeptName(String deptName) {
  this.deptName = deptName;
 }

 //Use @OneToOne To map 1-1 Relationship.
 //If you need to add a primary key to the current data table, you need to use the @JoinColumn To map. Be careful, 1-1 Correlation, So we need to add unique=true
 @JoinColumn(name="MGR_ID", unique=true)
 @OneToOne(fetch=FetchType.LAZY)
 public Manager getMgr() {
  return mgr;
 }

 public void setMgr(Manager mgr) {
  this.mgr = mgr;
 }
}

 

Note:

1. Use @ OneToOne to map the 1-1 Association.

2. If you need to add a primary key to the current data table, you need to use @ JoinColumn to map. Note: 1-1 Association, So you need to add unique=true.

3. This is a two-way one-to-one mapping, so you need to select a party to maintain the main association relationship and add @ joincolumn (name = "Mgr? ID", unique=true)

Then create a new manager entity class

package com.badao.jpa.helloworld;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Table(name="JPA_MANAGERS")
@Entity
public class Manager {

 private Integer id;
 private String mgrName;
 
 private Department dept;

 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Id
 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @Column(name="MGR_NAME")
 public String getMgrName() {
  return mgrName;
 }

 public void setMgrName(String mgrName) {
  this.mgrName = mgrName;
 }

 //For not maintaining association relationship, The party without foreign key, Use @OneToOne To map, Suggested setting mappedBy=Foreign key of the other party
 @OneToOne(mappedBy="mgr")
 public Department getDept() {
  return dept;
 }

 public void setDept(Department dept) {
  this.dept = dept;
 }
}

 

Note:

1. For the party who does not maintain the association relationship and does not have a foreign key, use @ OneToOne to map. It is recommended to set mappedBy = another foreign key.

2. Because this party does not maintain the association relationship, the mappedBy property of @ OneToOne is directly used. The property value corresponds to the foreign key property name of the other party.

Then add the configuration of entity class in the configuration file persistence.xml

<class>com.badao.jpa.helloworld.Manager</class>
<class>com.badao.jpa.helloworld.Department</class>

 

Add location as follows

 

 

Then write the unit test method

  

@Test
  public void testOneToOnePersistence(){
   Manager mgr = new Manager();
   mgr.setMgrName("M-BB");
   
   Department dept = new Department();
   dept.setDeptName("D-BB");
   
   //Set association relationship
   mgr.setDept(dept);
   dept.setMgr(mgr);
   
   //Perform save operation
   entityManager.persist(mgr);
   entityManager.persist(dept);
  }

 

Note:

For the bidirectional 1-1 Association, it is recommended to save the party that does not maintain the association first, that is, the party that does not have a foreign key, so there will be no more updates Sentence.

View database table after running unit test method

Manager table

 

 

Departmental table

Keywords: Java Database Programming xml

Added by Ghostu on Fri, 13 Dec 2019 11:47:20 +0200