Java Reference
In-Depth Information
however, we can do it with a single database query, but we get all 25,000,000 rows
in one big chunk.
To decide whether to use complex properties, you need to understand your
database and how your application will use it. If you use the techniques described
in this section, you can save yourself a good deal of programming effort, but if you
misuse it, you can create a big mess. In the next two sections, we look at how to
decide which strategy to use, depending on your goals.
Let's start by asking this question: is the example of relating accounts to orders
to order items a good example of when to relate your data this way? Actually, no—
the order-to-order item relationship is solid, but the account-to-order relationship
is not a requirement.
Our reasoning is that the order items are not complete objects without the
order that owns them, while the account is. Think about it in terms of how you
would use them. Generally, you would not be able to do much with an order with-
out its order items, and conversely, the order items without the order are some-
what meaningless. An account, on the other hand, is something that could be
thought of as a complete object.
For the purposes of our example, however, the relationship shows the tech-
nique well using concepts that are familiar and recognizable ( Account s have
Order s, Order s have Order Item s), and that is what we are trying to accomplish
here, so we will stick with it for a while longer.
6.2.2
Lazy loading
The first of the options we will look at is lazy loading . Lazy loading is useful if all of
the related data will not be needed immediately. For example, if our application
called for a web page to show all accounts, then a sales representative (our user)
could click on an account to view all of the orders for that account, and then click
on an order to view all of the details for that order. All we need in this case is a sin-
gle list at any time. This is a reasonable use of lazy loading.
To use lazy loading, we need to edit the SqlMapConfig.xml file to enable it by
changing the lazyLoadingEnabled attribute to true in the <setting> element. If
you want to use the cglib enhanced version of lazy loading, you will want to down-
load it, add it to your application's classpath, and change the enhancementEnabled
attribute of the <setting> element to true as well. One thing to note is that this is
a global setting, so all of the mapped statements in the SQL map will use lazy load-
ing if these properties are enabled.
Once we have enabled lazy loading, we can get to more reasonable numbers
for object creation and database I/O . For one user to get down to the order
Search WWH ::




Custom Search