JPA:删除 WHERE 不会删除子项并引发异常
我正在尝试从JPQL查询中删除大量行。MOTHER
该类定义如下:Mother
@Entity
@Table(name = "MOTHER")
public class Mother implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "mother",
orphanRemoval = true)
private List<Child> children;
}
@Entity
@Table(name = "CHILD")
public class Child implements Serializable {
@ManyToOne
@JoinColumn(name = "MOTHER_ID")
private Mother mother;
}
如您所见,该类具有“子级”,并且在执行以下查询时:Mother
String deleteQuery = "DELETE FROM MOTHER WHERE some_condition";
entityManager.createQuery(deleteQuery).executeUpdate();
抛出异常:
ERROR - ORA-02292: integrity constraint <constraint name> violated -
child record found
当然,我可以先选择要删除的所有对象,然后将它们检索到列表中,然后再循环访问以删除所有检索到的对象,但是这种解决方案的性能将非常糟糕!
那么,有没有办法利用前面的映射来有效地删除所有对象以及与它们关联的所有对象,而无需首先为所有子级编写查询?Mother
Child