I have the below Hibernate NamedQuery that runs into issues when one of the “in” expressions has 1000 or more items. When I have 1000 or more items in the ma.deviceId in (:devices), I get java.sql.SQLException: ORA-01795: maximum number of expressions in a list is 1000
The only “in” expression that I need to take care of is the “and ma.deviceId in (:devices)”. Anyone have any ideas on how to rewrite this NamedQuery? I’m using JPA and 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 " )
There are 2 ways.
1) Store your list in intermediary table and do
... IN (SELECT ... FROM intermediaryTable)2) Break your list into sublists each upto 1000 elements and write your query as
(... IN (subList1) OR ... IN (subList2) ...)