LuckyLuke
LuckyLuke

Reputation: 49067

Paginate all JPA queries

I am creating a web service and I wondered if there was a solution to paginate all queries. Currently I am creating overloaded methods such as findAll(int offset, int limit) etc. But I wondered if there was another solution to this which doesn't create that much duplication of logic in the finders. I want to this at database level. Or is the solution that I came up with an OK approach?

Upvotes: 0

Views: 125

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34367

I think you may want to use method overloading to avoid duplication of logic, if duplication is the primary concern.

  1. Change all your existing findAll methods to support pagination as you have mentioned:

    findAll(int offset, int limit)
    
  2. Then create overloaded findAll() method and internally call the findAll methods created in step 1 with default values e.g. below:

    findAll(){
      //set appropriate defaults, Integer.MAX_VALUE is just an example 
      findAll(0, Integer.MAX_VALUE);
    }
    

Upvotes: 4

Related Questions