I have two entities, in a model of five entities and when I run my code there are no errors. However, whenever I make delete all request from insomnia, I get the error: org.postgresql.util.PSQLException: ERROR: update or delete on table "courses" violates foreign key constraint "fk998yb1badftsiklfh13bcw3ol" on table "teacher_courses" Detail: Key (id)=(1) is still referenced from table "teacher_courses".
I understand that there is something wrong with the cascading between the two items, but I do not know how to resolve the issues.
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; } }