The test scenario is: Make call to method to load paged results for entity. The query in said method is generated based on a propertiesMap sent and then executed. The first time load the start index is 0 so the firstResult is not set, but maxResults are set to 20. Below is the code in the dao method: StringBuffer sb; Query query; try { sb = new StringBuffer("select model from Document model where 1=1"); int positionalParamIndex = 1; for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) { sb.append(" and model.").append(entry.getKey()).append(" = ?").append(positionalParamIndex++); } if (orderedProperty.equalsIgnoreCase("date")) { sb.append(" ORDER BY model.").append("docYear").append(" ").append( order); sb.append(" , model.").append("docMonth").append(" ").append( order); sb.append(" , model.").append("docDay").append(" ").append( order); } else { sb.append(" ORDER BY model.").append(orderedProperty); sb.append(" ").append(order); } query = getEntityManager().createQuery(sb.toString()); positionalParamIndex = 1; for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) { query.setParameter(positionalParamIndex++, entry.getValue()); } //The below chunk of code is what's causing the problem. Whenever any code in my application tries to set first or max results to limit a query then I get the index out of bounds error message if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) { int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]); if (rowStartIdx > 0) { query.setFirstResult(rowStartIdx); } if (rowStartIdxAndCount.length > 1) { int rowCount = Math.max(0, rowStartIdxAndCount[1]); if (rowCount > 0) { query.setMaxResults(rowCount); } } } return query.getResultList(); } catch (RuntimeException re) { throw re; } |