Java Reference
In-Depth Information
6.2 Relating objects with mapped statements
The i BATIS framework also offers various means by which you can relate complex
objects, such as an order and its order lines (and their products, customers, etc.).
Each method has its own advantages and disadvantages, and as with most things,
no single solution is the right one. Depending on your needs, one of them may do
what you need.
For brevity's sake, in the rest of the examples in this chapter, we are leav-
ing out attributes of the data that are not required to show what we are
doing. For example, when we get a customer, we are not going to get all
of the fields of the customer, but only the primary and foreign keys.
NOTE
6.2.1
Complex collections
In chapter 4, you learned how to get data out of your database using SELECT state-
ments. In those examples, we only worked with a single object type in the results,
even when joining multiple tables. If you have more complex objects, you can also
use i BATIS to load them.
This capability is useful if you like to have your application's model look like
your data model. It is possible to use i BATIS to define your data model in terms of
related objects, and have i BATIS load them all at once. For example, if you have a
database in which Account records have related Order records that have related
OrderItem records, those relationships can be set up so that when you request an
Account , you also get all of the Order objects and all of the OrderItem objects as
well. Listing 6.1 shows how you would define your SQL map to make this work.
Listing 6.1
Mapping a complex collection
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Ch7">
B
<resultMap id="ResultAccountInfoMap"
class="org.apache.mapper2.examples.bean.AccountInfo">
<result property="account.accountId"
column="accountId" />
<result property="orderList"
select="Ch6.getOrderInfoList"
column="accountId" />
</resultMap>
C
<resultMap id="ResultOrderInfoMap"
Search WWH ::




Custom Search