如何在EJB3中执行批量删除关系
我试图弄清楚如何对 @ManyToOne 关系执行批量删除,但到目前为止没有成功。
场景:
父级有很多下载,我想删除父级日期为 > 的所有下载。 :some_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会坚持第一种方法。 有显示错误信息吗?
当涉及到父记录与下载一起删除时......您确定它们之间的关系没有级联吗?
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?
我可以通过取消 @ManyToOne 注释并将其替换为 Long ParentId 来实现批量删除。 然后我更改了子选择以使用父名称而不是字段。
现在 OQL 看起来像这样:
我没有尝试它(因为我不需要查询功能来完成我正在做的事情),但如果可以将 @ManyToOne 保留在那里并使用此 OQL:
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:
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: