Buggy code:
wh += getStringPredicate ( "sessionId" , query . getSessionId ( ) , prms ) ; wh += getStringPredicate ( "instanceKeyword1" , query . getInstanceKeyword1 ( ) , prms ) ; wh += getStringPredicate ( "instanceKeyword2" , query . getInstanceKeyword2 ( ) , prms ) ; wh += getStringPredicate ( "instanceKeyword3" , query . getInstanceKeyword3 ( ) , prms ) ; wh += getStringPredicate ( "instanceModule" , query . getInstanceModule ( ) , prms ) ; wh += getStringPredicate ( "instanceApplication" , query . getInstanceApplication ( ) , prms ) ; wh += getStringPredicate ( "jd.keyword1" , query . getJobDefKeyword1 ( ) , prms ) ; wh += getStringPredicate ( "jd.keyword2" , query . getJobDefKeyword2 ( ) , prms ) ; wh += getStringPredicate ( "jd.keyword3" , query . getJobDefKeyword3 ( ) , prms ) ; wh += getStringPredicate ( "jd.module" , query . getJobDefModule ( ) , prms ) ; wh += getStringPredicate ( "jd.application" , query . getJobDefApplication ( ) , prms ) ; wh += getStringPredicate ( "queue.name" , query . getQueueName ( ) , prms ) ; wh += getIntPredicate ( "parentId" , query . getParentId ( ) , prms ) ; wh += getIntPredicate ( "id" , query . getJobInstanceId ( ) , prms ) ; wh += getIntPredicate ( "queue.id" , ( ( query . getQueueId ( ) ) == null ? null : query . getQueueId ( ) ) , prms ) ; List < com . enioka . jqm . api . JobInstance > res2 = new ArrayList < com . enioka . jqm . api . JobInstance > ( ) ; if ( query . isQueryLiveInstances ( ) ) { String sort = "" ; for ( SortSpec s : query . getSorts ( ) ) { sort += ( ( s . col . getJiField ( ) ) == null ) ? "" : ( ( ",h." + ( s . col . getJiField ( ) ) ) + "<seq2seq4repair_space>" ) + ( ( s . order ) == ( SortOrder . ASCENDING ) ? "ASC" : "DESC" ) ; } if ( sort . isEmpty ( ) ) { sort = "<seq2seq4repair_space>ORDER<seq2seq4repair_space>BY<seq2seq4repair_space>h.id" ; } else { sort = "<seq2seq4repair_space>ORDER<seq2seq4repair_space>BY<seq2seq4repair_space>" + ( sort . substring ( 1 ) ) ; } String wh2 = "" + wh ; Map < String , Object > prms2 = new HashMap < String , Object > ( ) ; prms2 . putAll ( prms ) ; wh2 += getCalendarPredicate ( "creationDate" , query . getEnqueuedAfter ( ) , ">=" , prms2 ) ; wh2 += getCalendarPredicate ( "creationDate" , query . getEnqueuedBefore ( ) , "<=" , prms2 ) ; wh2 += getCalendarPredicate ( "executionDate" , query . getBeganRunningAfter ( ) , ">=" , prms2 ) ; wh2 += getCalendarPredicate ( "executionDate" , query . getBeganRunningBefore ( ) , "<=" , prms2 ) ; wh2 += getStatusPredicate ( "state" , query . getStatus ( ) , prms2 ) ; if ( ( wh2 . length ( ) ) >= 3 ) { wh2 = "<seq2seq4repair_space>WHERE<seq2seq4repair_space>" + ( wh2 . substring ( 3 ) ) ; } TypedQuery < JobInstance > q2 = em . createQuery ( ( ( "SELECT<seq2seq4repair_space>h<seq2seq4repair_space>FROM<seq2seq4repair_space>JobInstance<seq2seq4repair_space>h<seq2seq4repair_space>" + wh2 ) + sort ) , JobInstance . class ) ; for ( Map . Entry < String , Object > entry : prms2 . entrySet ( ) ) { q2 . setParameter ( entry . getKey ( ) , entry . getValue ( ) ) ; } if ( ( query . getFirstRow ( ) ) != null ) { q2 . setFirstResult ( query . getFirstRow ( ) ) ; } if ( ( query . getPageSize ( ) ) != null ) { q2 . setMaxResults ( query . getPageSize ( ) ) ; } if ( ( ( query . getFirstRow ( ) ) != null ) || ( ( query . getPageSize ( ) ) != null ) ) { <START_BUG> TypedQuery < Long > qCount = em . createQuery ( ( "SELECT<seq2seq4repair_space>COUNT(h)<seq2seq4repair_space>FROM<seq2seq4repair_space>JobInstance<seq2seq4repair_space>h<seq2seq4repair_space>" + wh ) , Long . class ) ; <END_BUG> for ( Map . Entry < String , Object > entry : prms . entrySet ( ) ) { qCount . setParameter ( entry . getKey ( ) , entry . getValue ( ) ) ; } query . setResultSize ( new BigDecimal ( qCount . getSingleResult ( ) ) . intValueExact ( ) ) ; } for ( JobInstance ji : q2 . getResultList ( ) ) { res2 . add ( getJob ( ji , em ) ) ; } } if ( query . isQueryHistoryInstances ( ) ) { wh += getCalendarPredicate ( "enqueueDate" , query . getEnqueuedAfter ( ) , ">=" , prms ) ; wh += getCalendarPredicate ( "enqueueDate" , query . getEnqueuedBefore ( ) , "<=" , prms ) ; wh += getCalendarPredicate ( "executionDate" , query . getBeganRunningAfter ( ) , ">=" , prms ) ; wh += getCalendarPredicate ( "executionDate" , query . getBeganRunningBefore ( ) , "<=" , prms ) ; wh += getCalendarPredicate ( "endDate" , query . getEndedAfter ( ) , ">=" , prms ) ; wh += getCalendarPredicate ( "endDate" , query . getEndedBefore ( ) , "<=" , prms ) ; wh += getStatusPredicate ( "status" , query . getStatus ( ) , prms ) ; if ( ( wh . length ( ) ) >= 3 ) { wh = "<seq2seq4repair_space>WHERE<seq2seq4repair_space>" + ( wh . substring ( 3 ) ) ; } String sort = "" ; for ( SortSpec s : query . getSorts ( ) ) { sort += ( ( ",h." + ( s . col . getHistoryField ( ) ) ) + "<seq2seq4repair_space>" ) + ( ( s . order ) == ( SortOrder . ASCENDING ) ? "ASC" : "DESC" ) ; } if ( sort . isEmpty ( ) ) { sort = "<seq2seq4repair_space>ORDER<seq2seq4repair_space>BY<seq2seq4repair_space>h.id" ;

Fix:
TypedQuery < Long > qCount = em . createQuery ( ( "SELECT<seq2seq4repair_space>COUNT(h)<seq2seq4repair_space>FROM<seq2seq4repair_space>JobInstance<seq2seq4repair_space>h<seq2seq4repair_space>" + wh2 ) , Long . class ) ;