在多对多关系中删除子行时,如何防止父行被删除(休眠)

时间:2018-07-17 13:54:37

标签: hibernate spring-boot jpa

我有两个班级的课程和一个学生,并且创建了三个表(学生,课程,学生课程),我想做的是删除与(学生课程)中与该学生相关的课程表格而不删除学生。

public class Course {

    @Id
    @GeneratedValue
    private int id;

    @ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER,cascade = 
    {CascadeType.ALL})
    private Set<Student> students;
}

k

@Entity
public class Student {

@Id
@GeneratedValue
private int id;


@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "stud_course", joinColumns ={ @JoinColumn(name = "student_id",
        referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "course_id",
        referencedColumnName = "id")})
private Set<Course> courses;

0 个答案:

没有答案