Java Reference
In-Depth Information
Sometimes, depending on the query, one of these approaches is significantly
more efficient than the other. Let's look at how you can select a page of results
using
JDO
and Hibernate.
Implementing paging in JDO
There are two ways a
JDO
application can select a page from a result set. It can
either specify the required range of objects when it executes the query, or it can
pick the required objects out of the collection returned by
Query.execute()
. The
application specifies the range of rows by calling
Query.setRange()
with the start-
ing index and page size, or by using the
range <from>,<to>
clause in a
JDOQL
query string. Here is an example of such a
JDOQL
query:
select from net.chrisrichardson.foodToGo.domain.Order
range 0 to 10
The
Query.execute()
method will return a collection containing only the first ten
objects.
Alternatively, the application can pick the required objects out of the collec-
tion returned by
Query.execute()
by either using an iterator or calling
List.get()
.
A potential problem with both of these approaches is that the
JDO
specifica-
tion does not describe how the
JDO
implementation implements queries. There is
no guarantee that the
JDO
implementation implements
JDOQL
ranges by execut-
ing a
SQL
SELECT
statement that has a
ROWNUM
-like construct or will lazily navigate
the
JDBC
ResultSet
as the application iterates through the collection. The
JDO 2.0
specification does not give you a way to control how the
JDO
implementation exe-
cutes the query and processes the result set.
Fortunately, some
JDO
implementations such as Kodo
JDO
let you choose
between navigating the result set and using a
ROWNUM
query. For example, Kodo
JDO
always uses
ROWNUM
if you specify the range when querying an Oracle database.
In addition, Kodo
JDO
can be configured to process the result set lazily, which
enables it to efficiently handle large result sets. As the application accesses the ele-
ments of the collection returned by the
JDO
query, Kodo
JDO
iterates through the
underlying
JDBC
ResultSet
, loading objects. Kodo
JDO
gives you the flexibility
you need to efficiently implement paging, but if you are using another
JDO
imple-
mentation you should consult its documentation to determine how it implements
ranges and processes result sets.
Search WWH ::
Custom Search