我在我的应用程序中使用 Hibernate。目前我正在尝试执行以下查询:
DELETE FROM ActiveTimes a WHERE
a.begin>=:from AND a.begin<=:to
AND a.end>=:from AND a.end<=:to
AND a in(
SELECT al FROM ActiveTimes al
JOIN al.source.stage st
JOIN st.level.dataSource ds
WHERE ds=:dataSource)
但我得到一个错误:Column 'id' in field list is ambiguous
。这感觉很正常,因为创建的 SQL 查询如下所示:
delete
from
active_times
where
begin>=?
and begin<=?
and end>=?
and end<=?
and (
id in (
select
id
from
active_times activeti1_
inner join
sources sourc2_
on activeti1_.source=sourc2_.id
inner join
stage stage3_
on sourc2_.id=stage3_.source
inner join
levels levels4_
on stage3_.level=levels4_.id
inner join
datasources datasource5_
on levels4_.data_source=datasource5_.id
where
id=?
)
)
如果我将查询更改为:
DELETE FROM ActiveTimes a WHERE
a.begin>=:from AND a.begin<=:to
AND a.end>=:from AND a.end<=:to
AND a.id in(
SELECT al.id FROM ActiveTimes al
JOIN al.source.stage st
JOIN st.level.dataSource ds
WHERE ds.id=:dataSource)
我得到另一个错误:You can't specify target table 'active_times' for update in FROM clause
。
我没有对 JPQL(或 HQL)进行过试验,所以我不明白为什么查询看起来像第一个示例中的那样。
发生新错误是因为显然我无法对 MySQL 中的删除表进行子查询。
您对如何重写上述查询之一以使其正常工作有什么建议吗?