我正在做一个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_QUERY, [indices]);
Run Code Online (Sandbox Code Playgroud)
LocationTrade和Trade之间的关系是单向多对一的.因此,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 …Run Code Online (Sandbox Code Playgroud) 我试图使用类似的东西执行原始SQL查询
def dataSource;
Sql sql = new Sql(dataSource);
Run Code Online (Sandbox Code Playgroud)
但是,它似乎在一个单独的交易中运行.因此,它错过了在服务方法中完成的所有(未提交的)更改.
在当前事务中运行原始sql查询的最佳方法是什么?