1

我有两个实体,在五个实体的模型中,当我运行我的代码时没有错误。但是,每当我从失眠中删除所有请求时,我都会收到错误消息:org.postgresql.util.PSQLException:错误:更新或删除表“课程”违反了表“教师课程”上的外键约束“fk998yb1badftsiklfh13bcw3ol”详细信息:键( id)=(1) 仍然从表“teacher_courses”中引用。

我知道这两个项目之间的级联有问题,但我不知道如何解决这些问题。

package com.example.springapiwithsecuritydevelopment.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.List;

@Entity
@Table(name = "courses")
@Getter @Setter
public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Integer id;

    @Column(unique = true)
    private String courseName;

    @Column
    private LocalDate courseStartDate;

    @Column
    private LocalDate courseEndDate;

    // One course to many lessons
    @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnoreProperties("course")
    private List<Lesson> lessonList;

    // Many courses to many Students
    @ManyToMany(mappedBy = "courseList", fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE})
    @JsonIgnoreProperties("courseList")
    private List<Student> studentList;

    // Subject to course
    @ManyToOne()
    @JoinColumn(name = "subject_id")
    @JsonIgnoreProperties("courseList")
    private Subject subject;

    // ManyCourses to Many Teachers
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "courseList", cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
    @JsonIgnoreProperties("courseList")
    private List<Teacher> teacherList;

    // Adding constructors
    public Course() {
    }

    public Course(String courseName, LocalDate courseStartDate, LocalDate courseEndDate) {
        this.courseName = courseName;
        this.courseStartDate = courseStartDate;
        this.courseEndDate = courseEndDate;
    }


}

package com.example.springapiwithsecuritydevelopment.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

@Entity
@Getter @Setter
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    private String firstName;

    @Column
    private String secondName;

    @Column(nullable = false) @NotNull
    private LocalDate birthday;

    @Column
    private Integer age;

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinTable(
            name = "teacher_courses",
            joinColumns = @JoinColumn(name = "teacher_id"),
            inverseJoinColumns = @JoinColumn(name = "course_id")
    )
    @JsonIgnoreProperties("teacherList")
    private List<Course> courseList;


    // Adding constructors
    public Teacher() {
    }

    public Teacher(String firstName, String secondName, LocalDate birthday, Integer age) {
        this.firstName = firstName;
        this.secondName = secondName;
        this.birthday = birthday;
        this.age = age;
    }
}

4

2 回答 2

0

您可以尝试将 Teacher 类的级联类型更改为

@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH})
于 2021-11-16T18:06:02.897 回答
0

我的朋友找到了一个解决方案,如何在 JPA 中删除具有多对多关系的实体(以及相应的联接表行)?.

最后,我只是把

  @PreRemove
    private void removeCoursesFromTeacher() {
        for (Teacher t : teacherList) {
            t.getCourseList().remove(this);
        }
    }

在关系的非拥有方。

于 2021-11-16T18:10:25.627 回答