diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java index b0222df20..4fe7807ea 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/BaseFhirDao.java @@ -175,12 +175,20 @@ public List getSearchResults(@Nonnull SearchParameterMap theParams) { TypedQuery executableQuery = (TypedQuery) criteriaContext.getEntityManager() .createQuery(criteriaContext.getCriteriaQuery()); + executableQuery.setFirstResult(theParams.getFromIndex()); if (theParams.getToIndex() != Integer.MAX_VALUE) { int maxResults = theParams.getToIndex() - theParams.getFromIndex(); - executableQuery.setMaxResults(maxResults); + if (maxResults >= 0) { + executableQuery.setMaxResults(maxResults); + } else { + // TODO: this is really just a workaround, we can find a better way of handling the negative results + int negative = theParams.getFromIndex() - theParams.getToIndex(); + executableQuery.setMaxResults(negative); + } } - + + if (hasDistinctResults()) { results = (List) criteriaContext.getEntityManager().createQuery(criteriaContext.getCriteriaQuery()) .getResultList();