Java Reference
In-Depth Information
doesn't concatenate query fragments. In section 11.5 you will see an in-depth exam-
ple that uses criteria queries.
11.3.2
Loading the data with a single SELECT statement
Another important aspect of implementing a search screen is querying the data-
base efficiently. As we saw in section 11.1.3, there are a few things that you might
need to do to get good performance, but when using a persistence framework the
first step is to persuade to retrieve the data using a single SELECT statement. Even
though this sounds elementary, a persistence framework might not do this auto-
matically if the search screens display the attributes of multiple related objects. It
can, for example, load objects lazily, which requires multiple SELECT statements.
For example, as you can see in figure 11.4, the View Orders screen displays
the orderNumber and deliveryTime attributes of an order, the phoneNumber and
email attributes of the order's payment information, and the name of the order's
restaurant. The presentation tier code that implements the View Orders screen
iterates through the list of orders and navigates to each order's payment informa-
tion and restaurant.
The persistence framework must retrieve this data using a single SELECT state-
ment. It must not, for example, lazily load the restaurants by executing multiple
SELECT statements that each loads one restaurant from the RESTAURANT table
because that can reduce performance significantly. As we have seen in chapters 4,
5, and 6, you can optimize object loading by eliminating SQL statements by using
either process-level cache or eager loading.
We could, for example, improve the performance of the View Orders query by
caching the restaurants in the process-level cache, as we saw in chapter 4. Execut-
ing a query that retrieves just orders will work just fine. The application will
retrieve the restaurants from the process-level cache when it navigates to them.
Alternatively, we can improve the performance of the View Orders query by
eagerly loading the restaurants. The persistence framework will then execute a
SELECT statement that does a join between the PLACED_ORDER and RESTAURANT
tables. The details of how you configure and use eager loading depend on which
persistence framework you are using. As we saw in chapter 5, a JDO application
uses fetch groups to configure eager loading, and in chapter 6 we saw that a
Hibernate application uses queries with fetch joins. Let's see how fetch groups
can be used here in the query that retrieves orders.
 
 
Search WWH ::




Custom Search