在Hibernate 3.2.2的本机sql查询中使用IN子句

时间:2011-04-04 19:59:16

标签: sql hibernate in-operator sql-in

以类似于此处发现的问题的方式:Using IN clause in a native sql query;我试图通过Hibernate中的本机SQL查询来使用IN()子句。虽然另一个问题的作者能够使用JPA,但我不是。另外,我坚持使用版本3.2.2。

似乎Hibernate本身不支持IN(),因为它在应用查询参数时试图将我的ID列表(长基元数组)转换为二进制形式:query.setParameter("publisherGroups", [1243536264532,1243536264533,1243536264535]);

来自Hibernate:

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版本是否会这样做?如果没有,我有什么替代方案?

提前致谢,

卡尔

1 个答案:

答案 0 :(得分:33)

回答了我自己的问题,并且在发布之前应该有RTFM。 “技巧”是使用query.setParameterList()而不是query.setParameter()