如何在EJB3中执行批量删除关系

发布于 2024-07-26 08:57:59 字数 893 浏览 1 评论 0原文

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

场景:

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

我正在尝试在下载实体上使用 @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

有什么建议么?

I am trying to figure out how to execute a bulk delete on a @ManyToOne relationship, so far without success.

Scenario:

Parent has many Downloads, I want to execute a delete of all Downloads where the Parent date is > :some_date. I do not want to delete any of the Parent records, just the Download records. Download has a parent field that is mapped using a @ManyToOne annotation.

I am trying to use a @NamedQuery on the Download entity to accomplish this.

//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

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够运 2024-08-02 08:58:00

我会坚持第一种方法。 有显示错误信息吗?

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

I would stick to the first approach. Is there any error message shown?

When it comes to Parent records being deleted together with Downloads... Are you sure you don't have a cascade on the relation between them?

伴梦长久 2024-08-02 08:58:00

我可以通过取消 @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)

I was able to get the bulk delete to work by taking off the @ManyToOne annotation and replacing it with just the Long parentId. Then I changed the sub select to use the Parent name instead of the field.

Now the OQL looks like this:

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

I didn't try it (because I don't need query capabilities for what I am doing) but if may work to leave the @ManyToOne on there and use this OQL:

DELETE FROM Download dwn WHERE EXISTS 
    (SELECT p FROM Parent p WHERE p.id = dwn.parent.id and p.date > :date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文