T
- the entity bean typepublic interface PagedList<T>
The benefit of using PagedList over just using the normal Query with
Query.setFirstRow(int)
and Query.setMaxRows(int)
is that it additionally wraps
functionality that can call Query.findFutureRowCount()
to determine total row count,
total page count etc.
Internally this works using Query.setFirstRow(int)
and Query.setMaxRows(int)
on
the query. This translates into SQL that uses limit offset, rownum or row_number function to
limit the result set.
// We want to find the first 50 new orders
// ... so we don't really need setFirstRow(0)
PagedList<Order> pagedList
= ebeanServer.find(Order.class)
.where().eq("status", Order.Status.NEW)
.order().asc("id")
.setFirstRow(0)
.setMaxRows(50)
.findPagedList();
// Optional: initiate the loading of the total
// row count in a background thread
pagedList.loadRowCount();
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
// get the total row count (from the future)
int totalRowCount = pagedList.getTotalRowCount();
// We want to find the first 100 new orders
// ... 0 means first page
// ... page size is 100
PagedList<Order> pagedList
= ebeanServer.find(Order.class)
.where().eq("status", Order.Status.NEW)
.order().asc("id")
.findPagedList(0, 100);
// Optional: initiate the loading of the total
// row count in a background thread
pagedList.loadRowCount();
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
// get the total row count (from the future)
int totalRowCount = pagedList.getTotalRowCount();
// If you are not getting the 'first page' often
// you do not bother getting the total row count again
// so instead just get the page list of data
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
Query.findPagedList()
Modifier and Type | Method and Description |
---|---|
String |
getDisplayXtoYofZ(String to,
String of)
Helper method to return a "X to Y of Z" string for this page where X is the first row, Y the
last row and Z the total row count.
|
Future<Integer> |
getFutureCount()
Return the Future row count.
|
Future<Integer> |
getFutureRowCount()
Deprecated.
|
List<T> |
getList()
Return the list of entities for this page.
|
int |
getPageSize()
Return the page size used for this query.
|
int |
getTotalCount()
Return the total row count for all pages.
|
int |
getTotalPageCount()
Return the total number of pages based on the page size and total row count.
|
int |
getTotalRowCount()
Deprecated.
|
boolean |
hasNext()
Return true if there is a next page.
|
boolean |
hasPrev()
Return true if there is a previous page.
|
void |
loadCount()
Initiate the loading of the total row count in the background.
|
void |
loadRowCount()
Deprecated.
|
void loadCount()
// initiate the loading of the total row count
// in a background thread
pagedList.loadRowCount();
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
// get the total row count (from the future)
int totalRowCount = pagedList.getTotalRowCount();
Also note that using loadRowCount() and getTotalRowCount() rather than getFutureRowCount() means that exceptions ExecutionException, InterruptedException, TimeoutException are instead wrapped in the unchecked PersistenceException (which might be preferrable).
void loadRowCount()
Future<Integer> getFutureCount()
The loadRowCount() & getTotalRowCount() methods internally make use of this getFutureRowCount() method. Generally I expect people to prefer loadRowCount() & getTotalRowCount() over getFutureRowCount().
// initiate the row count query in the background thread
Future<Integer> rowCount = pagedList.getFutureRowCount();
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
// now get the total count with a timeout
Integer totalRowCount = rowCount.get(30, TimeUnit.SECONDS);
// or ge the total count without a timeout
Integer totalRowCountViaFuture = rowCount.get();
// which is actually the same as ...
int totalRowCount = pagedList.getTotalRowCount();
Future<Integer> getFutureRowCount()
int getTotalCount()
If loadRowCount() has already been called then the row count query is already executing in a background thread and this gets the associated Future and gets the value waiting for the future to finish.
If loadRowCount() has not been called then this executes the find row count query and returns the result and this will just occur in the current thread and not use a background thread.
// Optional: initiate the loading of the total
// row count in a background thread
pagedList.loadRowCount();
// fetch and return the list in the foreground thread
List<Order> orders = pagedList.getList();
// get the total row count (which was being executed
// in a background thread if loadRowCount() was used)
int totalRowCount = pagedList.getTotalRowCount();
int getTotalRowCount()
int getTotalPageCount()
This method requires that the total row count has been fetched and will invoke the total row count query if it has not already been invoked.
int getPageSize()
boolean hasNext()
This method requires that the total row count has been fetched and will invoke the total row count query if it has not already been invoked.
boolean hasPrev()
String getDisplayXtoYofZ(String to, String of)
This method requires that the total row count has been fetched and will invoke the total row count query if it has not already been invoked.
to
- String to put between the first and last rowof
- String to put between the last row and the total row countCopyright © 2016. All rights reserved.