我有下面的Hibernate NamedQuery,当其中一个“in”表达式有1000个或更多项时会遇到问题。当我在(:devices)中的ma.deviceId中有1000个或更多项时,我得到 java.sql.SQLException:ORA-01795:列表中的最大表达式数为1000
我需要处理的唯一“in”表达式是“和(:devices)中的ma.deviceId”。任何人有任何关于如何重写这个NamedQuery的想法?我正在使用JPA和Hibernate。
@NamedQuery(name = "Messages.findMessages", query = " SELECT ma from Messages ma JOIN FETCH ma.messagePayLoadXml mx LEFT OUTER JOIN FETCH ma.messageProperties mp " +
" WHERE ma.customerId = :customerId and ma.time >= :startTime and ma.time <= :endTime " +
" and ma.deviceId in (:devices) and mx.messageType = 'XML' and mx.alerts in " +
" ( select mtfm.messageType from MessageTypeFeatureMap mtfm where mtfm.feature in (:featureType) ) " +
" and ma.messageKey = mx.messageKey and ( mp.deleted = 0 or mp.deleted is null ) " +
" order by ma.time desc " )
答案 0 :(得分:2)
有两种方式。
1)将您的列表存储在中间表中并执行
... IN (SELECT ... FROM intermediaryTable)
2)将您的列表分成多达1000个元素的子列表,并将您的查询写为
(... IN (subList1) OR ... IN (subList2) ...)
答案 1 :(得分:1)
对于我们的应用程序,我们已针对这种情况做了一个简单的修复...
If number of values are greater than 999
QUERY = SELECT + FROM + WHERE + COND1 +
( FIELD IN ( 999 values ) OR FIELD IN ( 999 values )...) + ORDER
Else
QUERY = SELECT + FROM + WHERE + COND1 + IN (...) + ORDER