1

我试图弄清楚如何对@ManyToOne 关系执行批量删除,到目前为止没有成功。

设想:

父母有很多下载,我想删除父母日期为的所有下载> :some_date。我不想删除任何父记录,只是下载记录。下载有一个使用 @ManyToOne 注释映射的父字段。

我正在尝试在 Download 实体上使用 @NamedQuery 来完成此操作。

//this OQL tries to delete from both the child and parent tables
//I only want to delete from the Download table
DELETE FROM Download dwn 
    WHERE dwn.acctId = :acctId AND dwn.parent.date > :date

//this OQL is invalid and will keep the bean from deploying
//The example I found used this sub query but with a @OneToMany relationship
//instead of a @ManyToOne relationship
//this is what i get for an error on deployment:
//"Errors in named queries: deleteByAccountIdAndDownloadedDate"
DELETE FROM Download dwn WHERE EXISTS 
    (SELECT p from dwn.parent WHERE p.date > :date) 
    AND dwn.acctId = :acctId

有什么建议么?

4

2 回答 2

1

我会坚持第一种方法。是否显示任何错误消息?

当涉及到与下载一起删除父记录时……您确定它们之间的关系没有级联吗?

于 2009-07-09T22:11:30.083 回答
0

通过取消 @ManyToOne 注释并将其替换为 Long parentId,我能够使批量删除工作。然后我将子选择更改为使用父名称而不是字段。

现在 OQL 如下所示:

DELETE FROM Download dwn WHERE EXISTS 
    (SELECT p FROM Parent p WHERE p.id = dwn.parentId and p.date > :date)

我没有尝试过(因为我不需要查询功能来完成我正在做的事情)但如果可以将@ManyToOne 留在那里并使用此OQL:

DELETE FROM Download dwn WHERE EXISTS 
    (SELECT p FROM Parent p WHERE p.id = dwn.parent.id and p.date > :date)
于 2009-07-10T19:29:19.190 回答