使用相关实体删除JPA2中的查询

时间:2011-08-04 09:02:40

标签: hibernate jpa-2.0

我正在尝试批量删除

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

prbolem是hibernate将其翻译为

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

这会在执行na​​medquery

时导致异常
=== 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

1 个答案:

答案 0 :(得分:3)

您无法使用加入进行删除。

如果locCountries是索引集合,请尝试子查询:

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
)

问候。