Java Reference
In-Depth Information
When mapping in this way to a bean, there is one caveat to remember: if the
column you are selecting exists in the database but does not exist in the bean you
are mapping to, you will get no error, no warning, and no data—the data will sim-
ply be quietly ignored. When mapping to a Map object, the problem is similar:
while you will still get the data, it will not be where you expect it to be.
If you want a firmer approach to data mapping, look at using external result
maps (in section 4.3.1).
In spite of these two potential issues, automatic mapping works well in cases
where you are willing to let the framework do the mapping for you, and when you
do not mind paying the price when the mapping is done the first time.
If the list of fields being selected in the statement can change at runtime,
dynamic result mapping can also be used. Listing 4.2 shows an example of a query
using dynamic result mapping.
Listing 4.2
Dynamic result mapping example
<select id="getAccountRemapExample"
remapResults="true"
resultClass="java.util.HashMap" >
select
accountId,
username,
<dynamic>
<isEqual
property="includePassword"
compareValue="true" >
password,
</isEqual>
</dynamic>
firstName,
lastName
from Account
<dynamic prepend=" where ">
<isNotEmpty property="city">
city like #city#
</isNotEmpty>
<isNotNull
property="accountId"
prepend=" and ">
accountId = #accountId#
</isNotNull>
</dynamic>
</select>
B Remaps results when
mapped statement executes
C Contains simple Dynamic SQL
to demonstrate technique
Search WWH ::




Custom Search