I am trying to implement a pagination functionality with JPA but I am having trouble.
I can get and set the first and last numbers on the link e.g. localhost:8000/?first=11&last=20.
As you can see, I am trying to get the items starting at row 11 and ending at row 20, however, I am getting all the results starting from first but with a total amount of last e.g. starting at 10 but with 20 results as opposed to the desired 10.
I am using:
query.setFirstResult(firstRowNumber - 1).setMaxResults(lastRowNumber);
How do I therefore limit my results?
You want this:
Notice that the method is named
setMaxResults()(total number), notsetLastResult(). Have a look at awesome spring-data-jpa which handles paging very nicely.