2013-05-12 68 views
7

我正在開發Grails項目。我有以下查詢,我正在嘗試執行HQL在executeUpdate上生成不完整的「交叉連接」

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之間的關係是單向多對一的。所以,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 

看來生成的查詢是錯誤的。應該有一個加入貿易表,但缺少。我無法確定我在這裏所犯的錯誤。你能幫助我嗎?

創建腳本兩個表(我已經剝離了一些無趣列)

CREATE TABLE `location_trade` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `version` bigint(20) NOT NULL, 
    `auto_status` varchar(255) DEFAULT NULL, 
    `exclusion_reason_description` varchar(255) DEFAULT NULL, 
    `exclusion_reason_id` bigint(20) DEFAULT NULL, 
    `exclusion_reason_title` varchar(255) DEFAULT NULL, 
    `location_indices_id` bigint(20) DEFAULT NULL, 
    `manual_status` varchar(255) DEFAULT NULL, 
    `trade_id` bigint(20) DEFAULT NULL, 
    `absolute_price` decimal(19,6) DEFAULT NULL, 
    `flag` varchar(255) DEFAULT NULL, 
    `auto_exclusion_reason` varchar(255) DEFAULT NULL, 
    `date_created` datetime DEFAULT NULL, 
    `exclusion_reason_text` varchar(255) DEFAULT NULL, 
    `last_updated` datetime DEFAULT NULL, 
    `has_very_high_volume` bit(1) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `FK858985A90CAA966` (`location_indices_id`), 
    KEY `FK858985AB5FA6A69` (`trade_id`), 
    CONSTRAINT `FK858985A90CAA966` FOREIGN KEY (`location_indices_id`) REFERENCES `location_indices` (`id`), 
    CONSTRAINT `FK858985AB5FA6A69` FOREIGN KEY (`trade_id`) REFERENCES `trade` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=25405 DEFAULT CHARSET=latin1; 




CREATE TABLE `trade` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `version` bigint(20) NOT NULL, 
    `comments` varchar(1020) DEFAULT NULL, 
    `end_date` datetime DEFAULT NULL, 
    `price` decimal(19,6) DEFAULT NULL, 
    `price_type` varchar(255) DEFAULT NULL, 
    `source_id` bigint(20) DEFAULT NULL, 
    `start_date` datetime DEFAULT NULL, 
    `trade_date` datetime DEFAULT NULL, 
    `trade_name` varchar(255) DEFAULT NULL, 
    `volume` decimal(19,6) DEFAULT NULL, 
    `volume_units` varchar(255) DEFAULT NULL, 
    `date_created` datetime DEFAULT NULL, 
    `last_updated` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `FK697F1642D085935` (`source_id`), 
    CONSTRAINT `FK697F1642D085935` FOREIGN KEY (`source_id`) REFERENCES `job_source` (`id`), 
) ENGINE=InnoDB AUTO_INCREMENT=26567 DEFAULT CHARSET=latin1; 

感謝

+0

你可以爲兩個表添加「CREATE」語句嗎? – 2013-05-12 11:44:42

+0

@Declan_K添加了創建腳本 – Amit 2013-05-12 12:00:50

+0

我終於與原生SQL一起去了。我對Native SQL的擔心是,我不想開始一個新的交易,在這裏得到解決http://stackoverflow.com/q/16507369/593644 – Amit 2013-05-14 06:13:49

回答

10

Hibernate documentation說:

沒有加入,無論是隱性或顯性的,可以在批量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查詢。

+0

我會試試這個。但是,我記得MySQL在選擇後不允許「UPDATE」表。 – Amit 2013-05-12 11:56:55

+1

未與MySQL配合使用 'SQLException:您無法在FROM子句中指定目標表'location_trade'進行更新 – Amit 2013-05-12 12:07:07

+1

那麼您將不得不使用SQL,我很害怕。 – 2013-05-12 12:09:31