本文介绍了JPA CascadeType.ALL无法通过查询运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有OneToMany个双向关系实体类(WorkOrderTask). WorkOrder具有一个或多个Task.当我通过 query 删除WorkOrder实体时,我得到了外键约束异常. EntityManager无法自动删除相关的Task.

I have OneToMany bidirectional relationship enity classes (WorkOrder and Task). WorkOrder have one or more Task.when I delete a WorkOrder enity by query, I get foreign key constraint exception. EntityManager can't delete related Task automatically.

我的问题:em.removed(..)方法是否使用CascadeType.REMOVE? query不使用吗?(删除查询).

My question : Does Is CascadeType.REMOVE used by em.removed(..) method? Is it not used by query?(delete query).

WorkOrder.java

WorkOrder.java

.....
public class WorkOrder {
    ....
    @Temporal(TemporalType.TIMESTAMP)
    private Date expiryDate;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="workOrder", orphanRemoval=true)
    private List<Task> taskList;
    ......
}

Task.java

Task.java

......
public class Task {
    .....
    @ManyToOne
    @JoinColumn(name = "WORK_ORDER_ID", referencedColumnName = "ID")
    private WorkOrder workOrder;
    .....
}

我需要根据有效期限删除WorkOrder.首先,我需要删除相关的Task,之后我必须删除WorkOrder.这是正确的吗?我认为,Query可以使用CascadeType更好.

I need to delete WorkOrder based on expiry date. I need to delete related Task firstly, after that I have to delete WorkOrder. Is it correct?I think, it will be better CascadeType could used by Query.

推荐答案

按照规范,通过查询进行的DELETE(查询以"DELETE FROM ..."开头的查询)不会触发此类回调(因为这是一种删除数据的方式)从数据存储中快速获取);回调仅由正常的持久性操作(持久,合并,刷新,删除)使用,因此,如果您希望遵循它们的行为,请使用 remove()

As per the spec, DELETE by query (query starting "DELETE FROM ...") does not trigger such callbacks (since it is a way of removing data from the datastore quickly); callbacks are only used by the normal persistence operations (persist, merge, refresh, remove) so if you want their behaviour to be followed then use remove()

这篇关于JPA CascadeType.ALL无法通过查询运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:44