Search code examples
javaspring-mvchibernate-ogm

Hibernate OGM Mongodb Aggregate Query not working with JPA setFirstResult and setMaxResult while next Execution


I am using Hibernate OGM (5.2.0.Alpha1) with MongoDB (3.4)

While I am Executing

String query ="db.student.find({'collegeName' :'VNSGU'})"

for pagination With JPA setFirstResult() and setMaxResult(), It is working fine, but while executing aggregate Query

String query = "db.student.aggregate([{'$match' : {'collegeName' :'VNSGU'}}])";

List listOfStudent = entityManager.createNativeQuery(query, Student.class).setFirstResult(startPosition).setMaxResults(noOfRecords).getResultList();

Execution :

  • In database table, i have total 10 number of records where college name is 'VNSGU',

  • When startPosition = 0 and noOfRecords = 5, it will give my proper output on first execution and return 5 records,

  • And when try to execute query second time where startPosition = 5 and noOfRecords = 5, it will return empty list, because on second execution it will have total 5 no of records which is an output of first Execution (i checked it by executing query without setFirstResult() and setMaxResult() and returning total 5 records).As Per, I understood the output of earlier execution will become an input of next execution and it will return result from earlier result.

But actually what is happening and What is the solution for this..??


Solution

  • So indeed, it is a bug.

    We opened the following issue to track the bug you reported: https://hibernate.atlassian.net/browse/OGM-1411 .

    A fix is under review, it will be part of the upcoming 5.3.1.Final release.