【问题标题】:I have problem in many to many relationship in jpa repository for saving with reference in join table我在 jpa 存储库中的多对多关系中存在问题,以便在连接表中保存参考
【发布时间】:2020-02-19 20:33:20
【问题描述】:

我有学生课

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "first_name")
    private String firstName;

    @ManyToMany(mappedBy = "students", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
    private List<Course> courses;

    public void addCourse(Course theCourse) {
        if (courses == null) {
            courses = new ArrayList<>();
        }
        courses.add(theCourse);
        theCourse.addStudents(this);
    }

}
@Entity
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;

    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.PERSIST, CascadeType.REFRESH})
    private List<Student> students;

    public void addStudents(Student theStudent) {
        if (students == null) {
            students = new ArrayList<>();
        }
        students.add(theStudent);
    }
}
//definitely I wrote syntax wrong here because i don't copy and paste from code.
Course c =new Course(//fill constrouctor);
CourseRepositoryy.save(c);

student s = new Student(//fill constrouctor);
s.addCourse(c)
StudentRepository.save(s);

or

student s = new Student(//fill constrouctor);
Course c =new Course(//fill constrouctor);
c.addStudents(s);
save(s);
save(c);

现在我想创建学生和课程并将它们保存在连接表中保存每个参考。 我不知道先是哪个,然后再添加另一个。 有人可以帮助我并提供描述该主题的参考资料吗?

【问题讨论】:

    标签: hibernate jpa spring-data-jpa repository spring-repositories


    【解决方案1】:

    如果您想要多对多双向关系,您应该定义拥有方。因此,如果您选择 Course 作为拥有方,则映射将如下所示:

    public class Course {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String title;
    
        @ManyToMany(cascade = CascadeType.PERSIST)
        private List<Student> students = new ArraysList<>();
    ...
    
    @Entity
    public class Student {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Column(name = "first_name")
        private String firstName;
    
        @ManyToMany(mappedBy = "students")
        private List<Course> courses = new ArrayList<>();
    

    来自@ManyToManylink的javadoc

    如果关联是双向的,则 一方可以指定为拥有方。如果关系是 双向,非拥有方必须使用 mappedBy 元素 ManyToMany 注释指定关系字段或 拥有方的财产。

    如果你有如上的映射:Stundent的保存操作会级联到Course

    Course course = new Course();
    List<Student> studentList = new ArrayList<>();
    
    course.getStudents().addAll(studentList);
    
    em.persist(course);
    

    更多详情:official docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2014-02-21
      相关资源
      最近更新 更多