我试图删除父学生或家长当然,我得到这个错误:错误:更新或删除表“表名”违反外键约束
引起: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