ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Butler" <jeffgbut...@gmail.com>
Subject Re: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?
Date Thu, 29 Jun 2006 03:01:00 GMT
A custom type handler is the right way to do it.  Your code looks amazingly
similar to the example for this exact problem in the developer's guide :)

You could register the type handler globally so it would always handle any
VARCHAR to boolean transalation.  This is what's shown in the developer's
guide.  Specifying the type handler for a specific field in a result is
fairly new - we do need to get that into the documentation.  I'll get to
that tomorrow (hopefully).

Jeff Butler




On 6/28/06, Rick <ricks_mailinglists@arc-mind.com> wrote:
>
>   I found the answer (just by trial and error no documentation per se).
>
>
>
> Can you tell me if there is a easier/better way?
>
>
>
> I created a custom type handler and used it as follows:
>
>
>
>       <resultMap id="contacts" class="qcom.cas.mysourcej.poc.model.Contact
> ">
>
>             <result property="name" column="contact_name"/>
>
>             <result property="primaryContact" column="primary_contact"
>
>                     typeHandler="
> qcom.cas.commons.ibatis.typehandler.StringBooleanTypeHandler" />
>
>       </resultMap>
>
>
>
>
>
>
>
> package qcom.cas.commons.ibatis.typehandler;
>
>
>
> import java.sql.CallableStatement;
>
> import java.sql.PreparedStatement;
>
> import java.sql.ResultSet;
>
> import java.sql.SQLException;
>
>
>
> import com.ibatis.sqlmap.engine.type.TypeHandler;
>
>
>
> public class StringBooleanTypeHandler implements TypeHandler {
>
>
>
>             public void setParameter(PreparedStatement ps, int position,
> Object value,
>
>                                     String jdbcType) throws SQLException {
>
>
>
>                         Boolean bValue = (Boolean) value;
>
>                         ps.setString(position, bValue.booleanValue() ? "Y"
> : "N");
>
>
>
>             }
>
>
>
>             public Object getResult(ResultSet rs, String name) throws
> SQLException {
>
>                         return valueOf(rs.getString(name));
>
>             }
>
>
>
>             public Object getResult(ResultSet rs, int position) throws
> SQLException {
>
>                         return valueOf(rs.getString(position));
>
>             }
>
>
>
>             public Object getResult(CallableStatement cs, int position)
>
>                                     throws SQLException {
>
>                         return valueOf(cs.getString(position));
>
>             }
>
>
>
>             public Object valueOf(String value) {
>
>                         if (value.equals("Y")) {
>
>                                     return Boolean.TRUE;
>
>                         } else {
>
>                                     return Boolean.FALSE;
>
>                         }
>
>             }
>
>
>
>             public boolean equals(Object value1, String value2) {
>
>                         return valueOf(value2).equals(value1);
>
>             }
>
>
>
> }
>
>
>
> Can you tell me if there is a easier/better way?
>
>
>  ------------------------------
>
> *From:* Rick [mailto:ricks_mailinglists@arc-mind.com]
> *Sent:* Wednesday, June 28, 2006 4:07 PM
> *To:* user-java@ibatis.apache.org
> *Subject:* How do you map a Java boolean property to a VARCHAR(1) that
> contains 'Y' or 'N'?
>
>
>
>
>
> I have a SQL map as follows:
>
>
>
>       <resultMap id="contacts" class="qcom.cas.mysourcej.poc.model.Contact
> ">
>
>             <result property="name" column="contact_name"/>
>
>             <result property="primaryContact" column="primary_contact" />
>
>       </resultMap>
>
>
>
> The class Contact has a primaryContact field is boolean
>
> The result set is from a column that is equal to 'Y' or 'N' in the
> database.
>
>
>
> Is there a way to map a boolean property (primitive) to a VARCHAR(1) NOT
> NULL column (that has either a 'Y' or a 'N')?
>
>
>
> This is a legacy database and there is no way I can change the table. I
> could change the query.
>
>
>
>
>
> BTW I am new to iBatis, and I got it talking to a legacy db. I also setup
> relationships fairly easily.
>
>
>
>
>
> I did notice the typeHandler attribute of resultMap but this is not
> documented in the dtd or user documents.
>
>
>

Mime
View raw message