2

I have a query that retrieves all results for a particular query,

public List<MyObjs> getMyObjsForCustomer(final Long customerId) throws IOException {
}

This worked fine for a while but now I need to limit the result set for performance reasons, something like..

query.setMaxResults(1000);

However, i would like an intelligent way of finding out if there are more results that the 1000 to handle UI paging etc, handle the next call and so on. Is there a way to find out if there are more records matching my query than the 1000 returned? the equivalent of a

query.hasMore();

for example. I could get the ID of the last returned record and use that as the starting point for my next query but i was wondering if there was a better/another way?

user1843591
  • 1,074
  • 3
  • 17
  • 37

1 Answers1

5

The issue here is that Hibernate will not know if there are any more results since the limiting is done in the query and the database does not tell you if your query has more results beyond the requested limit. You could do a count query to get the total count, but that could obviously be very expensive.

A better trick is the set your maxResults to 1 more than your actual page size. So for example, if you are showing a page with 50 results, you actually ask the database for maxResults = 51. You only return records 1-50 to your client, but then if you actually got 51 records back, you can tell them there are more results (at least one, right?).

neildo
  • 2,206
  • 15
  • 12