One way many to one association in JPA

Keywords: Java Database Programming xml

scene

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

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

After the HelloWorld of JPA is built in the above blog, only the database mapping is completed for the customer entity class.

How to realize the one-way many to one mapping.

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

Create a new JPA? Order table in the database

 

 

 

One user can have multiple orders, but an order can only belong to one user, so a one-way many to one relationship is formed.

The customer? ID in the order table is the foreign key associated with the user table.

Create a new entity class Order under the package

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 Relationship of
 //Use @ManyToOne To map many to one relationships
 //Use @JoinColumn To map foreign keys. 
 //May use @ManyToOne Of fetch Property to modify the default load policy for associated properties
 @JoinColumn(name="CUSTOMER_ID")
 @ManyToOne
 public Customer getCustomer() {
  return customer;
 }

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

 

It mainly uses @ ManyToOne to map the many to one association relationship and @ JoinColumn to map the primary key.

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

<class>com.badao.jpa.helloworld.Order</class>

Add location as follows

 

 

 

Continue with the unit test class for the blog above. Continue writing test methods

/**
   * To save many to one, it is recommended to save one end of 1 first, and then the other end of n, so that no additional UPDATE statement will be generated
   */
  
  @Test
  public void testManyToOnePersist(){
   Customer customer = new Customer();
   customer.setAge(18);
   customer.setEmail("gg@163.com");
   customer.setLastName("GG");
   
   Order order1 = new Order();
   order1.setOrderName("G-GG-1");
   
   Order order2 = new Order();
   order2.setOrderName("G-GG-2");
   
   //Set association relationship
   order1.setCustomer(customer);
   order2.setCustomer(customer);
   
   //Perform save operation
   entityManager.persist(customer);
   entityManager.persist(order1);
   entityManager.persist(order2);
   
   
  }

 

View database after successful execution

User table

Order form

Posted by Richter on Thu, 12 Dec 2019 06:06:55 -0800