By calling the getAccountInfoListN mapped statement , we get the same data
back that we did with the previous two examples (a list of accounts, each with a list
of orders that have their associated order items as a list property on them), but
because we are running only one SQL statement it is much faster. The getAc-
countInfoListN mapped statement is run, and the results are mapped using
the resultAccountInfoNMap result map , which uses the groupBy attribute. That
attribute tells i BATIS that it only needs to create a new AccountInfo instance when
the account.accountId property changes. Further, because the orderList prop-
erty is mapped to the ResultOrderInfoNMap result map , that list is populated as
rows from the query are processed. Since the ResultOrderInfoNMap result map
also uses the groupBy attribute, the process is repeated for the orderItemList
using the ResultOrderItemNMap result map for the orderItemList property.
Using our totally nonscientific measurement from earlier, we receive a per-
formance improvement of about 7 to 1 with a small set of data. We suspect that
with the example we started with (25 million records), both cases would still be
It is important to remember that in spite of the performance increase, the
memory consumption is still the same as the non-lazy version. All rows are in
memory at one time, so even though it gets the list faster, memory utilization may
still be a problem.
The bottom line here is that, depending on your needs, one of these tech-
niques may help you out. How do you decide? Table 6.1 provides a guide.
Differences between lazy loading and the N+1 select solution
N+1 Select solution
Good for cases when you are getting a larger
data set that may not all be used.
Good for a smaller data set or a data set where
you know you will use all of the data.
Performance up front is a priority and you are
willing to pay for it later.
Overall performance is the priority.
So, that is about all there is to mapping complex results. Next, let's see some other
uses of i BATIS .
6.3 I nheritance
Inheritance is a fundamental concept in object-oriented programming. Inherit-
ance allows us to extend classes, effectively inheriting fields and methods from a
base class—or even a hierarchy of many base classes. The new class can override