JPA:DELETE WHERE不会删除子项并引发异常 [英] JPA: DELETE WHERE does not delete children and throws an exception

查看:728
本文介绍了JPA:DELETE WHERE不会删除子项并引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 MOTHER 中删除​​大量行,这要感谢JPQL查询。



Mother class定义如下:

  @Entity 
@Table(name =MOTHER)
public class Mother实现Serializable {

@OneToMany(cascade = CascadeType.ALL,mappedBy =mother,
orphanRemoval = true)
私人列表< Child>儿童;

$ b $实体
@Table(name =CHILD)
public class Child实现Serializable {

@ManyToOne
@JoinColumn(name =MOTHER_ID)
私人母亲母亲;

$ / code>

正如您所见, Mother class有children,并执行以下查询:

  String deleteQuery =DELETE FROM MOTHER WHERE some_condition ; 
entityManager.createQuery(deleteQuery).executeUpdate();

抛出异常:

 错误 -  ORA-02292:完整性约束<约束名称>违反 -  
找到的子记录

当然,我可以先选择所有我想要的对象删除并将它们检索到列表中,然后迭代它以删除所有检索到的对象,但是这样的解决方案的性能会非常糟糕!

所以有一个利用先前映射的方式可以有效地删除所有 Mother 对象以及与它们关联的所有 Child 对象而不是先写 all 子项的查询?

解决方案

DELETE(和INSERT)通过JPQL查询中的关系。这在规范中明确说明:


删除操作仅适用于指定类的实体,
的子类。它不会级联到相关的实体。


幸运的是,通过实体管理器执行持久性和删除操作(当定义了级联属性时)。



你可以做什么:


  • 提取所有母亲实体实例每个人都会调用EntityManager.remove()。



代码是一些东西像这样:

 字符串selectQuery =选择m从母亲m WHERE some_condition; 
列表< Mother> mothersToRemove = entityManager.createQuery(selectQuery).getResultList();
(母亲m:母亲去除){
em.remove(m);
}


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?

解决方案

DELETE (and INSERT) do not cascade via relationships in JPQL query. This is clearly spelled in specification:

A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.

Luckily persist and removal via entity manager do (when there is cascade attribute defined).

What you can do:

  • fetch all Mother entity instances that should be removed.
  • for each of them call EntityManager.remove().

Code is something like this:

String selectQuery = "SELECT m FROM Mother m WHERE some_condition";  
List<Mother> mothersToRemove = entityManager.createQuery(selectQuery).getResultList();  
for (Mother m: mothersToRemove) {  
    em.remove(m);
}

这篇关于JPA:DELETE WHERE不会删除子项并引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆