2011-08-04 108 views
2

我試圖做一個批量刪除刪除查詢與相關實體

@NamedQuery(name=CalcData.DELETE, 
query="delete from CalcData as model where model.dataLocation.locCountries = :locCountries and model.locPeriod= :locPeriod") 

的prbolem是休眠翻譯這

Hibernate: 
delete 
from 
    smart_rise.Calc_Data cross 
join 
    smart_rise.Data_Location datalocati1_ 
where 
    CountryID=? 
    and Period=? 

executin的namedquery

時,這會導致異常
=== 2011-08-04 10:53:30,719 [l0-6] ERROR JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cross join smart_rise.Data_Location datalocati1_ where CountryID=6 and Period=10' at line 1 
=== 2011-08-04 10:53:30,719 [l0-6] ERROR PeriodsDMI - org.hibernate.exception.SQLGrammarException: could not execute update query 
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute update query 
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214) 

任何想法出了什麼問題?

謝謝你, Zdary

回答

3

你不能用一個連接刪除。

如果locCountries是一個索引集合嘗試subquerying:

delete from CalcData as model 
where :locCountries in indicies(model.dataLocation.locCountries) 
and model.locPeriod= :locPeriod 

如果不是:

delete from CalcData as model 
where model.id not in (select id from CalcData as m where model.dataLocation.locCountries = :locCountries and model.locPeriod= :locPeriod 
) 

問候。

+0

嗨,謝謝你的快速回答。我用 '休眠: 從smart_rise.Calc_Data 刪除其中ID(從 選擇calcdata1_.ID smart_rise.Calc_Data calcdata1_交叉 加入 smart_rise.Data_Location datalocati2_ 其中 smart_rise.Calc_Data.LocationID = datalocati2_.ID 和?datalocati2_.CountryID = 和smart_rise.Calc_Data.Period = )' 我結束了例外 '造成的:?值java.sql.SQLException:您不能指定目標表 'Calc_Data' 的更新在FROM子句 \t at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:10 73) ' – zdary

+0

你試過第一個嗎? – ssedano

+0

這在MySQL中不起作用,因爲它不允許你從你在select子句中使用的實體中刪除 – Marc