以與此處發現的問題類似的方式:Using IN clause in a native sql query;我試圖通過Hibernate中的本地SQL查詢來使用IN()
子句。雖然另一個問題的作者能夠使用JPA,但我不是。另外,我堅持使用3.2.2版本。在帶有Hibernate 3.2.2的原生SQL查詢中使用IN子句
它好像Hibernate不支持原生IN()
因爲它正試圖應用查詢參數時,我的ID列表(長原語的數組)轉換成二進制形式:query.setParameter("publisherGroups", [1243536264532,1243536264533,1243536264535]);
從休眠狀態:
SELECT sum(C2CReportedConversion) as c2CConversion, sum(C2CReportedRevenue) as c2CRevenue, sum(I2CReportedConversion) as i2CConversion, sum(I2CReportedRevenue) as i2CRevenue, sum(Clicks) as clicks, sum(Impressions) as impressions, sum(Requests) as requests, sum(Views) as views, coalesce(Name, DisplayName) FROM UiTemplateReportingCache JOIN AdUnit USING (AdUnitId) WHERE PublisherId = ? AND PublisherGroupId IN ( ? ) AND Date >= ? AND Date <= ? GROUP BY coalesce(Name, DisplayName)
從MySQL日誌:
SELECT sum(C2CReportedConversion) as c2CConversion, sum(C2CReportedRevenue) as c2CRevenue, sum(I2CReportedConversion) as i2CConversion, sum(I2CReportedRevenue) as i2CRevenue, sum(Clicks) as clicks, sum(Impressions) as impressions, sum(Requests) as requests, sum(Views) as views, coalesce(Name, DisplayName) FROM UiTemplateReportingCache JOIN AdUnit USING (AdUnitId) WHERE PublisherId = 1239660230591 AND PublisherGroupId IN (_binary'��\0ur\0[Jx ��u�\0\0xp\0\0\0 \0\0!���T\0\0!���U\0\0!���W\0\0!���m\0\0!���n\0\0!���t\0\0!���{\0\0!���|\0\0!���}\0\0!���~\0\0#��\0\0$|��S') AND Date >= '2011-03-17 00:00:00' AND Date <= '2011-03-18 23:59:59' GROUP BY coalesce(Name, DisplayName)
請注意_binary
部分,該值開始IN()
值。做這項工作有什麼竅門?我使用的Hibernate版本甚至會這樣做?如果沒有,我有什麼替代方案?
由於提前,
卡爾
感謝您對我的瞭解:) – Rake36 2011-11-24 00:40:25
+1添加解決方案。 – kriver 2012-09-06 18:46:54
1添加解決方案 – Chad 2015-07-17 06:50:16