Contents

FirstResult/MaxRows

The JPA Query object contains support for configuring the first and max rows to retrieve when executing a query. When using this method it is important to use an ORDER BY, as multiple querys will be used for each page, and you want to ensure you get back the results in the same order.

By default EclipseLink translated these values into the parameters provided to the JDBC statement. This then leaves it up the JDBC driver to handle the range of rows requested. In many JDBC implementations this provides little performance value as the database does not limit the rows returned but they are instead filtered on the JDBC client. Most drivers support setting the max results, but the first result involves paging through the results on the JDBC client.

Database Specific SQL

EclipseLink's target database platform enables the generation of native SQL that can more optimally retrieve a set of rows.

As of the EclipseLink 2.0 release the following database platforms have pagination support in EclipseLink:

Oracle

MySQL

PostgreSQL

DB2

H2

HSQL

Firebird

Oracle ROWNUM

When the target platform is Oracle then ROWNUM is used in the generated SQL in addition to a hint to retrieve a page of results.

Other Databases

The SQL syntax for pagination is non standard, and only some databases support a custom syntax.
If you require this support on a database platform that EclipseLink does not support it on please post a message on the forum so that the work can be properly prioritized based on demand.

Chunking using Ids with IN

This involves performing a query on the database for all of the Id values that match the criteria and then using these values to retrieve specific sets.
This only works for singleton Ids not composite Ids. For composite Ids it is still possible through using dynamic SQL, or native SQL on some database, but much more complex.

Using a ScrollableCursor

EclipseLink supports returning a ScrollableCursor object from a Query. A ScrollableCursor can be enabled using query hints.
This returns a database cursor on the Query's result set, and allows the client to scroll through the results page by page.
The drawback for this technique is that the ScrollableCursor represents a live cursor and connection with the database,
so will normally not live across web page requests, but useful for scrolling for a server process or batch job.