问题描述
@Modifying
@Query("delete from Thing t where t.manyToOne1.manyToOne2 = :farThing and t.id in :ids")
void delete(ManyToOne2 manyToOne2,List<String> ids);
导致异常:
Caused by: com.MysqL.jdbc.exceptions.jdbc4.MysqLSyntaxErrorException: 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 many_to_one1 manytoone1_ where manytoone2_id=1039 and (id in (' at line 1
解决方法
将请求更改为
@Modifying
@Query("delete from Thing t where t.manyToOne1 = :manyToOne1 and t.id in :ids")
void delete(ManyToOne1 manyToOne1,List<String> ids);
解决了该问题。
在某些其他请求上使用2深度多对一关系链似乎没有任何错误,但是与IN
运算符或与修改查询结合使用在练习。
问题是,这种查询使用隐式联接,该隐式联接将在DML语句中生成SQL联接。并非所有DBMS都允许这样做,并且Hibernate还没有为此提供支持。如您所知,可以避免隐式联接。对于delete语句,您还可以使用一个现存的子查询对联接进行建模,如下所示:
delete from Thing t
where t.id in :ids
and exists (
select 1
from t.manyToOne1 a
where a.manyToOne2 = :farThing
)