【问题标题】:ERROR: update or delete on table "courses" violates foreign key constraint "fk998yb1badftsiklfh13bcw3ol" on table "teacher_courses"错误:更新或删除表“课程”违反了表“教师课程”上的外键约束“fk998yb1badftsiklfh13bcw3ol”
【发布时间】:2021-11-16 17:45:02
【问题描述】:

我有两个实体,在一个包含五个实体的模型中,当我运行我的代码时没有错误。但是,每当我从失眠中删除所有请求时,我都会收到错误消息:org.postgresql.util.PSQLException: ERROR: update or delete on table "courses" 违反了表 "teacher_courses" 上的外键约束 "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;
    }
}

【问题讨论】:

    标签: java spring spring-boot hibernate jpa


    【解决方案1】:

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

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH})
    

    【讨论】:

    • 不幸的是,这只是给我抛出了一个错误,无法执行传递给持久化的 CommandLineRunner 分离实体。
    • 尝试将所有映射的 CascadeType.DETACH 更改为 CascadeType.PERSIST
    • 我在上面放了一个解决方案链接,以防你有兴趣!
    • ps,我也在尝试你建议的解决方案!
    【解决方案2】:

    我的朋友找到了一个解决方案,How to remove entity with ManyToMany relationship in JPA (and corresponding join table rows)?

    最后,我只是放了

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

    在关系的非拥有方。

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2017-11-18
      • 2019-11-05
      • 2018-05-17
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      相关资源
      最近更新 更多