带有JoinTable的JPA OneToMany删除外键失败

时间:2014-11-22 16:36:11

标签: jpa

我的Patientbooking class使用了加入表。以下是代码。 Appointment没有Patientbooking

public class Patientbooking implements Serializable {
    ...
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST})
    @JoinTable
    (
      name="patientbooking_appointments",
      joinColumns={ @JoinColumn(name="patientbooking_id", referencedColumnName="id") },
      inverseJoinColumns={ @JoinColumn(name="appointments_id", referencedColumnName="id", unique=true) }
  )
    private List<Appointment> appointments;

    ...
}

当我删除Appointment时,我收到此错误。

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`soebala/patientbooking_appointments`, CONSTRAINT `patientbooking_appointments_ibfk_1` FOREIGN KEY (`appointments_id`) REFERENCES `appointments` (`id`))

1 个答案:

答案 0 :(得分:0)

由于错误表示您正在执行约束违规。加入你的代码

cascade = {CascadeType.PERSIST, CascadeType.REMOVE})