Queries in Ebean

PagingList

PagingList is used to make it easy to page through a query result. Paging through the results means that instead of all the results are not fetched in a single query Ebean will use SQL to limit the results (limit/offset, rownum, row_number() etc).

Instead of using PagingList you could just use setFirstRow() setMaxRows() on the query yourself. If you are building a stateless application (not holding the PagingList over multiple requests) then this approach is a good option.

For Stateful applications PagingList provides some benefits:

  • Fetch ahead (background fetching of the next page via a FutureList query)
  • Automatic propagation of the persistence context
  • Automatically getting the total row count (via a FutureRowCount query)

So with PagingList when you use Page 2 it will automatically fetch Page 3 data in the background (using a FutureList query). The persistence context is automatically propagated meaning that all the paging queries use the same persistence context.

example

int pageSize = 10;

PagingList<TOne> pagingList = 
	Ebean.find(TOne.class)
		.where().gt("name", "b")
		.findPagingList(pageSize);

		
// get the row count in the background...
// ... otherwise it is fetched on demand
// ... when getTotalRowCount() or getTotalPageCount() 
// ... is called
pagingList.getFutureRowCount();

// get the first page
Page<TOne> page = pagingList.getPage(0);

// get the beans from the page as a list
List<TOne> list = page.getList();


int totalRows = page.getTotalRowCount();

if (page.hasNext()) {
	Page<TOne> nextPage = page.next();
	...
}
woResponse