我如何级联删除作为jpa实体一部分的集合? [英] How can I cascade delete a collection which is part of a jpa entity?

查看:1396
本文介绍了我如何级联删除作为jpa实体一部分的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Entity
public class Report extends Model {

    public Date date;
    public double availability;

    @ElementCollection
    @Cascade(value={CascadeType.ALL})
    public Map<FaultCategory, Integer> categories;      
}



在我的一个作业中,我有以下代码:

In one of my jobs I have the following code:

int n = MonthlyReport.delete("date = ?", date);

这总是无法删除具有以下错误的实体:

This always fails to delete the entity with the following error:


DELETE语句与REFERENCE约束FK966F0D9A66DB1E54冲突。冲突发生在数据库TFADB,表dbo.MonthlyReport_categories,列'MonthlyReport_id'。

The DELETE statement conflicted with the REFERENCE constraint "FK966F0D9A66DB1E54". The conflict occurred in database "TFADB", table "dbo.MonthlyReport_categories", column 'MonthlyReport_id'.

如何指定映射

推荐答案

级联删除(和一般级联操作)是仅当通过EntityManager完成操作时才有效。当通过JP QL / HQL查询作为批量删除进行删除时不是。当通过查询删除时,您不能指定将删除链接到ElementCollection中的元素的映射。

Cascading delete (and cascading operations in general) is effective only when operation is done via EntityManager. Not when delete is done as bulk delete via JP QL /HQL query. You cannot specify mapping that would chain removal to the elements in ElementCollection when removal is done via query.

ElementCollection注释没有级联属性,因为操作总是级联的。当你通过EntityManager.remove()删除你的实体,操作被级联到ElementCollection。

ElementCollection annotation does not have cascade attribute, because operations are always cascaded. When you remove your entity via EntityManager.remove(), operation is cascaded to the ElementCollection.

你必须获取所有要删除的MonthlyReport实体,并调用EntityManager.remove为他们每个。看起来像而不是在Play框架中,你也可以调用实体中的删除方法。

You have to fetch all MonthlyReport entities you want to delete and call EntityManager.remove for each of them. Looks like instead of this in Play framework you can also call delete-method in entity.

这篇关于我如何级联删除作为jpa实体一部分的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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