我正在使用 JPA2 (EclipseLink),我的实体对象如下所示:
@Entity
public class Dashboard
{
@Id
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy="dashboard", orphanRemoval = true)
private List<LogResult> logResults;
//getter-setters
}
@Entity
public class LogResult
{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;
private Dashboard dashboard;
//getter-setters
}
我有这样的代码来删除仪表板:
EntityManager em = PersistenceInitializer.newEntityManager();
em.getTransaction().begin();
em.remove(dashboard);
em.getTransaction().commit();
但是,当我执行上面的代码时,我收到关于 logresult 表上的外键约束的错误消息。
为什么会这样?使用 CascadeType.ALL 不意味着应该为我处理所有这些吗?
此处更新是控制台上的实际错误输出
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`crs`.`logresult`, CONSTRAINT `FK_LOGRESULT_DASHBOARD_NAME` FOREIGN KEY (`DASHBOARD_NAME`) REFERENCES `dashboard` (`NAME`))
Error Code: 1451