2

I have the following query in HQL:

update ProjectFile pf1 
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf 
where pf.validFrom.sequence <= pv.sequence 
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)

Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:

You can't specify target table 'ProjectFile' for update in FROM clause

The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.

4

1 回答 1

0

我遇到了同样的问题并在这里发布了一个问题:MySQL/SQL: Update with related subquery from the updated table itself

要解决您的问题,您需要在 UPDATE 级别加入,请查看我的问题的答案。

于 2009-05-26T13:44:55.173 回答