Java Reference
In-Depth Information
implementations such as Kodo
JDO
let you specify whether it should navigate the
result set or use a
SELECT
statement with a
ROWNUM
-like feature. However, there is
no guarantee that all
JDO
implementations provide this capability. As a result, you
might need to use i
BATIS
or
JDBC
instead.
In this section, we provide an overview of how to implement efficient paged
queries with Hibernate and
JDO
object queries. You will learn about what support,
if any, Hibernate and
JDO
provide for dynamically generating object queries. We
describe how to persuade the persistence framework to execute an object query
using a
SELECT
statement similar to those that a
JDBC
or i
BATIS
application would
use. You'll learn how
JDO
and Hibernate applications can implement pagination.
In sections 11.4 and 11.5 you will see how to apply the techniques described here
in more detail. In those sections we also describe how to use
JDO
and Hibernate
native
SQL
queries.
11.3.1
Generating Hibernate and JDO queries dynamically
One important aspect of implementing a search screen is generating queries
dynamically from the search criteria entered by the user. Ideally, you want to avoid
writing messy repository code that generates object queries by concatenating
query fragments. Unfortunately,
JDO
lacks support for dynamically generating
queries. Only Hibernate has
API
s for dynamically generating object queries. Let's
see how
JDO
and Hibernate repositories dynamically generate queries.
Dynamically generating JDO queries
A
JDO
application must generate an object query by concatenating fragments of
the
JDO
query language (
JDOQL
), which makes the code messy and error-prone.
Here is an example of a code fragment that constructs the
WHERE
clause of a query
from an
OrderSearchCriteria
:
…
public class JDOOrderRepositoryImpl … {
public PagedQueryResult findOrders(int startIndex,
int pageSize, OrderSearchCriteria criteria) {
…
StringBuffer where = new StringBuffer();
Map parameters = new HashMap();
if (criteria.isDeliveryCitySpecified()) {
if (where.length() != 0)
where.append(" && ");
where.append("deliveryAddress.city == :pDeliveryCity");
}
if (criteria.isRestaurantSpecified()) {
if (where.length() != 0)
Search WWH ::
Custom Search