Java HQL正在生成不完整的';交叉连接';关于执行更新
我正在从事一个grails项目。我正在尝试执行以下查询Java HQL正在生成不完整的';交叉连接';关于执行更新,java,mysql,spring,hibernate,grails,Java,Mysql,Spring,Hibernate,Grails,我正在从事一个grails项目。我正在尝试执行以下查询 String CHECK_FOR_HIGH_TRADE_VOLUME_QUERY = "Update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=? AND lt.trade.volume>20000"; ... LocationTrade.executeUpdate(CHECK_FOR_HIGH_TRADE_VOLUME_QUER
String CHECK_FOR_HIGH_TRADE_VOLUME_QUERY = "Update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=? AND lt.trade.volume>20000";
...
LocationTrade.executeUpdate(CHECK_FOR_HIGH_TRADE_VOLUME_QUERY, [indices]);
区位贸易与贸易之间的关系是单向的多对一关系。因此,LocationTrade引用了Trade,但Trade类没有引用LocationTrade列表
在执行时,我得到以下异常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute update query; SQL [update location_trade cross join set has_very_high_volume=1 where location_indices_id=? and volume>20000]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute update query
and
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 'set has_very_high_volume=1 where location_indices_id=997 and volume>20000' at line 1
似乎生成的查询是错误的。本来应该有一个与贸易表的联接,但是缺少了。我无法识别我在这里犯的错误。你们中的一些人能帮我吗
两个表的创建脚本(我已经去掉了一些不感兴趣的列)
谢谢经理说:
不能在批量HQL查询中指定隐式或显式联接。子查询可以在where子句中使用,其中子查询本身可能包含联接
lt.trade.volume
是LocationTrade和trade之间的隐式内部联接,因此查询无效。您必须将其重写为以下内容:
update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=?
and lt.id in (
select lt2.id from LocationTrade lt2 where lt2.trade.volume > 20000)
或者您必须使用SQL查询。您可以为这两个表添加
CREATE
语句吗?@Declan_K添加了CREATE脚本,我最终使用了原生SQL。我对原生SQL的担心是,我不想启动一个新的事务,这个问题在这里得到了解决,我将尝试一下。但是,我记得MySQL不允许在选择后对表进行“更新”。MySQL不适用于SQLException:您不能在FROM子句中为更新指定目标表“location\u trade”,那么恐怕您必须使用SQL。这仅适用于DML样式的操作
update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=?
and lt.id in (
select lt2.id from LocationTrade lt2 where lt2.trade.volume > 20000)