gpt4 book ai didi

hibernate - 我可以使用 join 语句执行批量删除吗?

转载 作者:行者123 更新时间:2023-12-04 04:48:08 26 4
gpt4 key购买 nike

我正在尝试使用 jpql 执行以下批量删除:

            " DELETE " +
" FROM AutAnt aa " +
" JOIN aa.person p " +
" JOIN p.employees e" +
" WHERE e = :employess " +
" AND (" +
" aa.dateFrom BETWEEN :dateStart AND :dateEnd" +
" OR" +
" aa.dateTor BETWEEN :dataStart AND :dateEnd" +
" )");

但我收到此错误:
unexpected token: JOIN

JPA docs声明“按查询删除使用与普通查询相同的 JPQL 语法,但有一个异常(exception):以 delete 关键字而不是 select 关键字开始查询字符串”

我有以下选择查询,它完美地工作:
            " SELECT aa " +
" FROM AutAnt aa " +
" JOIN aa.person p " +
" JOIN p.employees e" +
" WHERE e = :employess " +
" AND (" +
" aa.dateFrom BETWEEN :dateStart AND :dateEnd" +
" OR" +
" aa.dateTor BETWEEN :dataStart AND :dateEnd" +
" )");

将删除查询的第一条语句更改为 “删除aa”引发不同的异常。

这是一个 hibernate 错误,还是我在这里遗漏了什么。

干杯!

最佳答案

Hibernate 批量 DML 操作不支持连接。来自 documentation :

No Section 16.4, “Forms of join syntax”, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.



正如文档中所建议的,您可以在子查询的帮助下在语义上编写相同的语句:
delete AutAnt where id in
(select aa.id from AutAnt aa join aa.person p ...)

关于hibernate - 我可以使用 join 语句执行批量删除吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17861259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com