Hi Tracey,
I used a custom type handler on a project last year (for a boolean field on an AS/400 no
less :-) ) and I remember struggling to get it to work.
I just went and looked at my map I think you need to create a parameterMap for the query
that uses the type handler as well as a result map. You specify the type handler for the
property that uses it on both ends. So, here's what mine looks like:
<resultMap id="ScheduledMonthlyPaymentResult" class="ScheduledMonthlyPayment">
<result property="applicationNumber" column="appnum"/>
<result property="sequenceNumber" column="seqnum"/>
<result property="accountName" column="acctnm"/>
<result property="totalAmountOwed" column="totamt"/>
<result property="monthlyPayment" column="mnthpmt"/>
<result typeHandler="com.foo.bar.dao.BooleanToCharTypeHandler"
property="active" column="active"/>
</resultMap>
<parameterMap id="ScheduledMonthlyPaymentParameters"
class="ScheduledMonthlyPayment">
<parameter property="applicationNumber"/>
<parameter property="sequenceNumber" />
<parameter property="accountName"/>
<parameter property="totalAmountOwed"/>
<parameter property="monthlyPayment" />
<parameter property="active"
typeHandler="com.foo.bar.dao.BooleanToCharTypeHandler" />
</parameterMap>
Hope that helps!
b
Tracey Annison wrote:
Hiya!
*** I think I sent this wrong earlier - apologies if it duplicates! ***
We are using iBATIS with Java against a quirky AS/400 database. We have
used custom TypeHandlers successfully to globally map enums against
columns holding String codes, but are now trying to map a variety of
codes to Booleans without success.
As an example, I have three string fields, let's say Fred, Hal, Bill and
Joe.
- Fred and Hal can have values "A", "N", and "Y"
- Bill has values "Y" and "N" (which mean positive & negative)
- Joe has values "1" and "0" (which also mean positive & negative)
I have made a Java enum to hold the A/N/Y values called FlagANY, and a
TypeHandler called TypeHandlerStringANYEnum to convert the string "A",
"N" and "Y" values from the db into the enum FlagANY. I've made Fred and
Hal into FlagANY enums in the Java class ThingIbatis.
Both Bill and Joe are Booleans in the Java class ThingIbatis, and I have
created TypeHandlers TypeHandlerStringYNBoolean and
TypeHandlerString01Boolean to convert their varying string values into
the appropriate Boolean values.
In the sql-map-config.xml, I can map the FlagANY fields globally, which
works fine for Fred and Hal - as they're FlagANY in the Java class, and
as their value on the Db are consistent, the TypeHandler gets invoked
from the sql-map-config.xml setting, and all is well.
Now, Bill and Joe are both Booleans in the Java class, but their values
on the Db differ, so I have to specify their TypeHandlers on a
column-by-column basis in the table's XML file. (I tried omitting the "
javaType="java.lang.Boolean", but that didn't help)
This ought to work, as far as I can see, but the TypeHandler doesn't
ever get invoked. Is there some special syntax I should be using?
Generic TypeHandler in the sql-map-config.xml-
<typeHandler javaType="uk.co.package.user_type.FlagANY"
callback="uk.co.package.utils.TypeHandlerStringANYEnum"/>
And in the thing.xml I can define a ResultMap like this -
<resultMap id="thingResult" class="uk.co.package.persistence.ThingIbatis">
<result column="OCFRED" property="Fred"/>
<result column="OCHAL" property="Hal"/>
<result column="OCBILL" property="Bill"
javaType="java.lang.Boolean"
typeHandler="uk.co.package.utils.TypeHandlerStringYNBoolean"/>
<result column="OCJOE" property="Joe"
javaType="java.lang.Boolean"
typeHandler="uk.co.package.utils.TypeHandlerString01Boolean"/>
</resultMap>
Cheers
Tracey Annison
----------------------------------------------------------------------
The information in this email is confidential and may be legally
privileged.
It is intended solely for the addressee. Access to this email by
anyone else is unauthorised. If you are not the intended recipient,
any disclosure, copying, distribution, or any action taken or omitted
to be taken in reliance on it, is prohibited and may be unlawful.
TriSystems Ltd. cannot accept liability for statements made which are
clearly
the sender's own.