代码参考git。git地址:https://github.com/lidreamwind/Java-Jpa-Data

one to many是一张表的一条记录对应另一张表的多条记录。

Many to one 是一张表的多条记录对应另一张表的一条记录。

两张表之间以外键关系关联在一起。

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

1、OneToMany配置如下  

 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

相关文章:

  • 2022-12-23
  • 2021-04-19
  • 2021-09-06
  • 2021-08-13
  • 2022-12-23
  • 2021-07-12
  • 2021-06-24
猜你喜欢
  • 2021-12-08
  • 2021-07-10
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
相关资源
相似解决方案