Java Reference
In-Depth Information
B
The
doInJdo()
method creates the criteria query and calls a helper method to
add the search criteria, specify the ordering and range, and to create the
PagedQueryResult
.
The
addCriteria()
method adds criteria to the query based on what search crite-
ria are specified in the
OrderSearchCriteria
object. One notable feature of this
method is that if the restaurant name is not one of the search criteria, then
addCriteria()
adds a fetch join for
Restaurant
.
The
addSortBy()
method specifies how to sort the results.
The
addRange()
method specifies the range of rows to retrieve.
The
makePagedQueryResult()
method constructs the
PagedQueryResult
.
The Hibernate version of the repository contains some conditional logic, but it's a
lot simpler than the
JDO
version because using criteria queries eliminates the
need to concatenate query fragments.
C
D
E
F
11.6 Using JDO and Hibernate native SQL queries
In an ideal world, we would use object queries for all database queries and rely on
the persistence framework to generate optimal
SQL
statements. But as you might
expect, in the real world this isn't always possible and sometimes we must use
SQL
queries that use vendor-specific features such as optimizer hints to achieve the
necessary performance. Both
JDO
and Hibernate support native
SQL
queries.
They give you complete control over the
SQL
while taking care of the potentially
tedious task of constructing Java objects. The only drawback is that neither Hiber-
nate nor
JDO
provides any support for dynamically generating
SQL
queries. Con-
sequently, you might want to consider using i
BATIS
if you are only going to display
the results of the query and do not need to manipulate the objects. Let's look at
how to use
JDO
and Hibernate
SQL
queries.
11.6.1
Using JDO native SQL queries
A
JDO
native
SQL
query is executed using the
JDO
Query
interface but is written in
SQL
instead of
JDOQL
. It can return either persistent objects or
DTO
s.
SQL
que-
ries that return persistent objects are useful if the application needs to manipulate
the persistent objects. However, they are somewhat restrictive because the col-
umns of the result set must map to the fields of a single persistent class. A
SQL
query cannot, for example, eagerly load related objects such as an order and its
restaurant, which makes it impossible to implement the View Orders query.
Search WWH ::
Custom Search