Spring boot入門之-Jpa- One to Many Many to one

代碼參考git。git地址:https://github.com/lidreamwind/Java-Jpa-Datajava

one to many是一張表的一條記錄對應另外一張表的多條記錄。git

Many to one 是一張表的多條記錄對應另外一張表的一條記錄。github

兩張表之間之外鍵關係關聯在一塊兒。spring

文檔參考:https://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam=1574061512_630dd51b88a4d03476be822a6903bad5oracle

一、OneToMany配置以下  app

 1 package com.lph.spring.dev.ManyToOne.pojo;
 2 
 3 import javax.persistence.*;
 4 import java.util.List;
 5 
 6 @Entity
 7 @Table(name = "student")
 8 public class StudentEntity {
 9     @Id
10     @GeneratedValue
11     @Column(name =  "student_id")
12     private Integer studentId;
13     @Column(name = "name")
14     private String name;
15     @Column(name = "age")
16     private Integer age;
17 
18     //studentId是實體類中的屬性,mappedBy不能和@JoinTable一塊兒
19     @OneToMany(targetEntity = CourseStudent.class,mappedBy = "studentId",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
20     private List<CourseStudent> courseStudent ;
21 
22     public Integer getStudentId() {
23         return studentId;
24     }
25 
26     public void setStudentId(Integer studentId) {
27         this.studentId = studentId;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public Integer getAge() {
39         return age;
40     }
41 
42     public void setAge(Integer age) {
43         this.age = age;
44     }
45 
46     public List<CourseStudent> getCourseStudent() {
47         return courseStudent;
48     }
49 
50     public void setCourseStudent(List<CourseStudent> courseStudent) {
51         this.courseStudent = courseStudent;
52     }
53 }
View Code

二、Many To One的配置以下  ide

 1 package com.lph.spring.dev.ManyToOne.pojo;
 2 
 3 import com.fasterxml.jackson.annotation.JsonIgnore;
 4 
 5 import javax.persistence.*;
 6 
 7 @Entity
 8 @Table(name = "couurse_student")
 9 public class CourseStudent {
10     @Id
11     @GeneratedValue
12     @Column(name = "id")
13     private Integer id;
14     @Column(name = "course_id")
15     private Integer courseId;
16     @Column(name = "student_id")
17     private Integer studentId;
18 
19     @ManyToOne()
20     @JoinColumn(name = "student_id",insertable = false,updatable = false,referencedColumnName = "student_id")
21     @JsonIgnore
22     private StudentEntity studentEntity;
23 
24     public Integer getId() {
25         return id;
26     }
27 
28     public void setId(Integer id) {
29         this.id = id;
30     }
31 
32     public Integer getCourseId() {
33         return courseId;
34     }
35 
36     public void setCourseId(Integer courseId) {
37         this.courseId = courseId;
38     }
39 
40     public Integer getStudentId() {
41         return studentId;
42     }
43 
44     public void setStudentId(Integer studentId) {
45         this.studentId = studentId;
46     }
47 
48     public StudentEntity getStudentEntity() {
49         return studentEntity;
50     }
51 
52     public void setStudentEntity(StudentEntity studentEntity) {
53         this.studentEntity = studentEntity;
54     }
55 }
View Code

三、One To Many的配置解析。  fetch

@OneToMany(targetEntity = CourseStudent.class,mappedBy = "studentId",fetch = FetchType.EAGER,cascade = CascadeType.ALL) 
private List<CourseStudent> courseStudent ;

OneToMany的有兩行,一行註解和一個List變量。List變量用來存放一對多的結果。
targetEntity的是用來註釋須要和哪一個實體類進行聯合查詢(實體類對應表),mappedBy指定的是實體類中的連接字段,對應表中的字段。
fetch有兩個類型,LAZY和EAGER  
  參考:https://stackoverflow.com/questions/26601032/default-fetch-type-for-one-to-one-many-to-one-and-one-to-many-in-hibernate
cascade有ALL,DETACH,MERGE,PERSIST,REFRESH,REMOVE幾種類型。  
  參考:https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/
官網文檔 ,mappedBy不能用於ManyToOnne註解。
The many side of one-to-many / many-to-one bidirectional relationships must be the owning
side, hence the mappedBy element cannot be specified on the ManyToOne annotation.

四、Many To One的配置解析。  ui

@ManyToOne() 
@JoinColumn(name = "student_id",insertable = false,updatable = false,referencedColumnName = "student_id")
@JsonIgnore
private StudentEntity studentEntity;
@ManyToOne是標註爲多對一
@JoinColumn是標註匹配的外鍵列,insertable = false,不容許插入,updatable=false是不容許更新。referenceColumnName標註的是外鍵列。
@JsonIgnore,生成Json時不生成StudentEntity 的屬性
五、結果展現
 
相關文章
相關標籤/搜索