| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>Composite Keys or Multiple Complex Parameters Properties</h1>
|
|
|
4 |
<p>You might have noticed that in the above examples there is only a single key
|
|
|
5 |
being used as specified in the <tt>resultMap</tt> by the <tt>column</tt> attribute.
|
|
|
6 |
This would suggest that only a single column can be associated to a related
|
|
|
7 |
mapped statement. However, there is an alternate syntax that allows multiple
|
|
|
8 |
columns to be passed to the related mapped statement. This comes in handy for
|
|
|
9 |
situations where a composite key relationship exists, or even if you simply
|
|
|
10 |
want to use a parameter of some name other than <tt>#value#</tt>. The alternate
|
|
|
11 |
syntax for the column attribute is simply <tt>param1=column1, param2=column2, ... ,
|
|
|
12 |
paramN=columnN</tt>. Consider the example below where the PAYMENT table
|
|
|
13 |
is keyed by both Customer ID and Order ID:</p>
|
|
|
14 |
|
|
|
15 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
16 |
<resultMap id="select-order-result" class="order">
|
|
|
17 |
<result property="id" column="ORD_ID"/>
|
|
|
18 |
<result property="customerId" column="ORD_CST_ID"/>
|
|
|
19 |
...
|
|
|
20 |
<result property="payments" column="{itemId=ORD_ID, custId=ORD_CST_ID}"
|
|
|
21 |
select="selectOrderPayments"/>
|
|
|
22 |
</resultMap>
|
|
|
23 |
|
|
|
24 |
<statement id="selectOrderPayments" resultMap="select-payment-result">
|
|
|
25 |
select * from PAYMENT
|
|
|
26 |
where PAY_ORD_ID = #itemId#
|
|
|
27 |
and PAY_CST_ID = #custId#
|
|
|
28 |
</statement>
|
|
|
29 |
</com:TTextHighlighter>
|
|
|
30 |
|
|
|
31 |
<p>Optionally you can just specify the column names as long as they're in the
|
|
|
32 |
same order as the parameters. For example:</p>
|
|
|
33 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
34 |
{ORD_ID, ORD_CST_ID}
|
|
|
35 |
</com:TTextHighlighter>
|
|
|
36 |
|
|
|
37 |
<div class="note"><b>Important!</b>
|
|
|
38 |
<p>Currently the SQLMap DataMapper framework does not automatically resolve
|
|
|
39 |
circular relationships. Be aware of this when implementing parent/child
|
|
|
40 |
relationships (trees). An easy work around is to simply define a second result
|
|
|
41 |
map for one of the cases that does not load the parent object (or vice versa),
|
|
|
42 |
or use a join as described in the "N+1 avoidance" solutions.</p>
|
|
|
43 |
</div>
|
|
|
44 |
|
|
|
45 |
<div class="info"><b class="tip">Info:</b>
|
|
|
46 |
Result Map names are always local to the Data Map definition file that they
|
|
|
47 |
are defined in. You can refer to a Result Map in another Data Map definition
|
|
|
48 |
file by prefixing the name of the Result Map with the namespace of the SqlMap
|
|
|
49 |
set in the <tt><sqlMap></tt> root element.
|
|
|
50 |
</div>
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
</com:TContent>
|