我有兩個物體,在五個物體的模型中,當我運行我的代碼時沒有錯誤。但是,每當我從 insomnia 發出洗掉所有請求時,都會收到錯誤訊息: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;
}
}
uj5u.com熱心網友回復:
你可以嘗試將教師類的級聯型別更改為
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH})
uj5u.com熱心網友回復:
我的朋友找到了一個解決方案,如何在 JPA 中洗掉具有 ManyToMany 關系的物體(以及相應的連接表行)?.
最后,我只是把
@PreRemove
private void removeCoursesFromTeacher() {
for (Teacher t : teacherList) {
t.getCourseList().remove(this);
}
}
在關系的非擁有方。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/358233.html
