I am trying to delete a large number of rows from MOTHER
thanks to a JPQL query.
The Mother
class is defined as follows:
@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;
}
As you can see, the Mother
class has "children" and when executing the following query:
String deleteQuery = "DELETE FROM MOTHER WHERE some_condition";
entityManager.createQuery(deleteQuery).executeUpdate();
an exception is thrown:
ERROR - ORA-02292: integrity constraint <constraint name> violated -
child record found
Of course, I could first select all the objects I want to delete and retrieve them into a list before iterating through it to delete all the retrieved object, but the performance of such a solution would just be terrible!
So is there a way to take advantage of the previous mapping to delete all the Mother
objects AND all the Child
objects associated with them efficiently and without writing first the queries for all the children?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…