Two-way one-to-many Association in JPA

Keywords: Java Attribute Programming SQL

scene

Introduction to JPA and HelloWorld (with code download):

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

Implement one-way many-to-one associations in JPA:

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

One-way one-to-many associations are implemented in JPA:

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

Once the HelloWorld program for JPA has been set up above, and one-way one-to-many and one-way many-to-one mapping relationships are performed.

What to do if it is a two-way one-to-many mapping.

Note:

Blog Home Page:
https://blog.csdn.net/badao_liumang_qizhi
Focus on Public Number
Domineering program ape
Get programming-related e-books, tutorial pushes, and free downloads.

Realization

Comment out the Customer property of the Order class when you did a one-way one-to-many mapping before, and let it go again.

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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Table(name="JPA_ORDERS")
@Entity
public class Order {
 private Integer id;
 private String orderName;

 private Customer customer;

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

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

 @Column(name="ORDER_NAME")
 public String getOrderName() {
  return orderName;
 }

 public void setOrderName(String orderName) {
  this.orderName = orderName;
 }

 //Mapping One Way n-1 Relevance Relationships
 //Use @ManyToOne To map many-to-one associations
 //Use @JoinColumn To map foreign keys. 
 //Available @ManyToOne Of fetch Property to modify the load policy for the default associated property
 @JoinColumn(name="CUSTOMER_ID")
 @ManyToOne
 public Customer getCustomer() {
  return customer;
 }

 public void setCustomer(Customer customer) {
  this.customer = customer;
 }
}

 

Also map in Customer entity classes

package com.badao.jpa.helloworld;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
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.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="JPA_CUSTOMERS")
public class Customer {
 
 
 private Integer id;
 
 
 private String lastName;

 private String email;
 
 private int age;
 
 private Set<Order> orders = new HashSet<>();
 
 //Mapping One-way 1-n Relevance Relationships
 //Use @OneToMany To map 1-n Relevance Relationships
 //Use @JoinColumn To map the name of a foreign key column
 //have access to @OneToMany Of fetch Property to modify the default load policy
 //By @OneToMany Of cascade Property to modify the default deletion policy. 
 //Be careful: If at one end of the @OneToMany Use mappedBy attribute, be @OneToMany End can no longer be used @JoinColumn Attribute. 
 @OneToMany(fetch=FetchType.LAZY,cascade={CascadeType.REMOVE},mappedBy="customer")
 //@JoinColumn(name="CUSTOMER_ID")
 public Set<Order> getOrders() {
  return orders;
 }
 public void setOrders(Set<Order> orders) {
  this.orders = orders;
 }
 
 
 @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 
 @Column(name="LAST_NAME")
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 @Override
 public String toString() {
  return "Customer [id=" + id + ", lastName=" + lastName + ", email=" + email + ", age=" + age + "]";
 }

}

 

Note:

For a two-way 1-n relationship, if you save one end of n and one end of 1, by default, there will be more n UPDATE statements.

If you save one end of 1 first, there will be n more UPDATE statements.

In the case of two-way 1-n association, it is recommended that one side of n should be used to maintain the association, while the other side of 1 should not. This effectively reduces the number of SQL statements.

If the mappedBy property is used in @OneToMany at one end of the 1, @OneToMany Ends can no longer use the @JoinColumn property

Write unit test methods

  

@Test
  public void testOneToManyPersist(){
   Customer customer = new Customer();
   customer.setAge(18);
   
   customer.setEmail("BB@163.com");
   customer.setLastName("BB");
   
   Order order1 = new Order();
   order1.setOrderName("O-BB-1");
   
   Order order2 = new Order();
   order2.setOrderName("O-BB-2");
   
   //Establish a relationship
   customer.getOrders().add(order1);
   customer.getOrders().add(order2);
   
   
   order1.setCustomer(customer);
   order2.setCustomer(customer);
   
   //Perform save operation
   entityManager.persist(customer);

   entityManager.persist(order1);
   entityManager.persist(order2);
  }

 

Execute unit test results

Order table

 

 

Customer table

Posted by baselineace on Thu, 12 Dec 2019 19:47:12 -0800