我試圖刪除父學生或家長當然,我得到這個錯誤:錯誤:更新或刪除表「表名」違反外鍵約束
引起:org.postgresql.util.PSQLException :錯誤:表「學生」上的更新或刪除違反了表「註冊」上的外鍵約束「fkeyvuofq5vwdylcf78jar3mxol」
RegistrationId類是註冊類中使用的組合鍵。我使用Spring數據jpa和spring引導。
我在做什麼錯?我知道把cascadetype.all也應該刪除父母被刪除時的孩子,但它給我一個錯誤,而不是。
@Embeddable
public class RegistrationId implements Serializable {
@JsonIgnoreProperties("notifications")
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "student_pcn", referencedColumnName="pcn")
private Student student;
@JsonIgnoreProperties({"teachers", "states", "reviews"})
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "course_code", referencedColumnName="code")
private Course course;
註冊類
@Entity(name = "Registration")
@Table(name = "registration")
public class Registration {
@EmbeddedId
private RegistrationId id;
感謝您的回答。基本上我想要的是在添加新註冊(學生和課程必須存在)時確保數據完整性,這就是爲什麼我使用這些外鍵。但是,通過放置cascadetype.all,當我刪除父母時,它應該把所有的孩子帶走。這就是我想要的。 – Merv