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

时间:2020-12-11 07:41:13

In a fashion similar to the question found here: Using IN clause in a native sql query; I am attempting to make use of an IN() clause by way of a native SQL query in Hibernate. While the author in the other question was able to use JPA, I am not. In addition, I am stuck with version 3.2.2.

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

It seems as though Hibernate doesn't support IN() natively because it is attempting to convert my list (array of long primitives) of IDs into binary form when applying the query parameters: query.setParameter("publisherGroups", [1243536264532,1243536264533,1243536264535]);

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

From Hibernate:

来自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)

SELECT sum(C2CReportedConversion)为c2CConversion,sum(C2CReportedRevenue)为c2CRevenue,sum(I2CReportedConversion)为i2CConversion,sum(I2CReportedRevenue)为i2CRevenue,sum(Clicks)为click,sum(Impressions)为impressions,sum(Requests)为request, sum(Views)as views,coalesce(Name,DisplayName)FROM UiTemplateReportingCache JOIN AdUnit USING(AdUnitId)WHERE PublisherId =? AND PublisherGroupId IN(?)AND Date> =?和日期<=? GROUP BY coalesce(Name,DisplayName)

From the mysql logs:

从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)

SELECT sum(C2CReportedConversion)为c2CConversion,sum(C2CReportedRevenue)为c2CRevenue,sum(I2CReportedConversion)为i2CConversion,sum(I2CReportedRevenue)为i2CRevenue,sum(Clicks)为click,sum(Impressions)为impressions,sum(Requests)为request, 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吨\ 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 )

Notice the _binary part that starts the IN() value. What's the trick to making this work? Will the version of Hibernate I'm using even do this? If not, what alternatives do I have?

注意启动IN()值的_binary部分。这项工作的诀窍是什么?我正在使用的Hibernate版本是否会这样做?如果没有,我有什么替代品?

Thanks in advance,

提前致谢,

Carl

卡尔

1 个解决方案

#1


31  

Answered my own question and should have RTFM'd before posting. The "trick" is to use query.setParameterList() as opposed to query.setParameter().

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

#1


31  

Answered my own question and should have RTFM'd before posting. The "trick" is to use query.setParameterList() as opposed to query.setParameter().

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