2011-02-28 121 views
1

我有以下HQL查詢:休眠,MySQL的

UPDATE TaskAssessment taskAssessment 
SET taskAssessment.activeFlag = false 
WHERE taskAssessment IN 
(
SELECT taskAssessment2 
FROM TaskAssessment taskAssessment2 
Where taskAssessment2.activeFlag = true 
AND taskAssessment2.patient.id 
AND taskAssessment2.needsLevel.careNeed = :careNeed 
) 

但它給我的錯誤:

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

任何人都可以幫我糾正MySQL的查詢和休眠。提前致謝。

+1

見http://stackoverflow.com/questions/45494/sql- delete-cant-specify-target-table-for-update-in-from-clause – axtavt 2011-02-28 17:25:51

+0

我看到了,但是當我試着說出乎意料的時候(在查詢中! – Ikthiander 2011-02-28 17:27:25

+0

@ user582862:你能發表你已經試過的東西嗎? – 2011-02-28 17:35:38

回答

0

要解決You can't specify target table 'TASK_ASSESSMENT' for update in FROM clause,重寫查詢使用JOIN代替IN(在MySQL中,你需要寫這樣的事情):

UPDATE TaskAssessment a 
INNER JOIN TaskAssessment a2 ON (a2.id = a.id) 
SET a.activeFlag = 0 
WHERE a2.active_flag = 1 AND 
a2.patient_id = :patient_id AND a2.needsLevel_careNeed = :careNeed 
+0

嘗試過:UPDATE TaskAssessment taskAssessment INNER JOIN TaskAssessment taskAssessment2 ON(taskAssessment.id = taskAssessment2.id)SET taskAssessment.activeFlag = false其中taskAssessment2.activeFlag = true AND taskAssessment2.patient.id =:patientId AND taskAssessment2.needsLevel.careNeed =: careneed – Ikthiander 2011-02-28 17:48:34

+0

它沒有工作。給出錯誤:期待「設置」,找到'INNER' – Ikthiander 2011-02-28 17:48:58

+0

這是MySQL語法,而不是hql。爲了使它工作,你需要使用'CreateSQLQuery' +'executeUpdate'。我不確定你是否可以在hql中使用join + update。 – a1ex07 2011-02-28 18:20:01