我正在尝试通过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
类有“children”,当执行以下查询时:
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 对象,而无需首先编写所有子查询的查询呢?