MySQL交叉连接查询的Hibernate异常

Jas*_*son 26 mysql hibernate

我正在尝试执行批量删除对象Feature,它与另一个类FeatureMetadata具有鸟类ManyToOne关系.我抛出了一个SQLGrammerException.

我正在使用的hql:

String hql = "delete from Feature F where F.featuresMetadata.stateGeoId = :stateGeoId";
Run Code Online (Sandbox Code Playgroud)

启用show SQL,生成以下内容:

 delete from FEATURE cross join FEATURESMETADATA featuresme1_ where STATEGEOID=?
Run Code Online (Sandbox Code Playgroud)

直接在db客户端中运行SQL会产生以下异常:

 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 FEATURESMETADATA featuresme1_ where stategeoid='01'' at line 1
Run Code Online (Sandbox Code Playgroud)

由于生成的SQL抛出异常,我尝试将方言从MySQL5InnoDBDialect更改为MySQLInnoDBDialect,但没有变化.

有人可以帮忙吗?

JB *_*zet 39

您可能没有加入此类HQL查询.参考文档:

在批量HQL查询中不能指定隐式或显式的连接.子查询可以在where子句中使用,子子查询本身可以包含连接.

所以我觉得这样的事情应该有效:

delete from Feature F where F.id in 
    (select f2.id from Feature f2 where f2.featuresMetadata.stateGeoId = :stateGeoId)
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我很惊讶它因为我得到了你的工作:org.hibernate.exception.GenericJDBCException:你不能在FROM子句中指定目标表'Users'进行更新.这是因为您无法修改在SELECT部分​​中使用的同一个表(在您的情况下 - Feature表).此行为记录在:http://dev.mysql.com/doc/refman/5.6/en/update.html.有什么建议? (9认同)
  • 哇.我认为HQL是关于使查询比SQL更简化和面向对象.我不明白为什么Hibernate无法弄清楚如何在没有显式连接的情况下将对象之间的关系转换为SQL查询. (9认同)