You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Rick <ri...@arc-mind.com> on 2006/06/29 01:03:35 UTC

RE: 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.

 


RE: Jeff RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
PS - I have nothing against the ternary operator!  I didn't write that
section of the manual, so I don't make any claims about the code style :)

I figured as much. I just felt a little saucy (devious attitude). :o)

 

 

 

Thanks for the info. The TypeHandlerCallback is easier so that is the one I
will use. 

I'll just change my TypeHandler to be a TypeHandlerCallback.

 

 

package qcom.cas.commons.ibatis.typehandler;

 

import java.sql.SQLException;

 

import com.ibatis.sqlmap.client.extensions.ParameterSetter;

import com.ibatis.sqlmap.client.extensions.ResultGetter;

import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

 

public class StringBooleanTypeHandler implements TypeHandlerCallback {

 

            public Object valueOf(String value) {

                        if (value.equals("Y")) {

                                    return Boolean.TRUE;

                        } else {

                                    return Boolean.FALSE;

                        }

            }

 

            public void setParameter(ParameterSetter param, Object value)
throws SQLException {

                        Boolean bValue = (Boolean) value;

                        param.setString(bValue.booleanValue() ? "Y" : "N");

            }

 

            public Object getResult(ResultGetter rg) throws SQLException {

                        return valueOf(rg.getString());

            }

 

}

 

It works. It is a lot shorter and easier to read. I dig it.

 

 

  _____  

From: Jeff Butler [mailto:jeffgbutler@gmail.com] 
Sent: Thursday, June 29, 2006 10:34 AM
To: user-java@ibatis.apache.org
Subject: Re: Jeff RE: How do you map a Java boolean property to a VARCHAR(1)
that contains 'Y' or 'N'?

 

Hi Rick,

 

It turns out that you can use either one.  TypeHandlerCallback is a bit
simpler to implement than TypeHandler, but either will do.  We call the XML
attributes "typeHandler" for brevity.

 

If you use TypeHandlerCallback, then the class
com.ibatis.sqlmap.engine.type.CustomTypeHandler is used internally to
simulate a full TypeHandler.

 

Off to update the documentation...sigh...

 

Jeff Butler

 

PS - I have nothing against the ternary operator!  I didn't write that
section of the manual, so I don't make any claims about the code style :)

 



 

On 6/29/06, Rick <ricks_mailinglists@arc-mind.com > wrote: 

Jeff,

 

I downloaded the guide out of svn. It does mention TypeHandler (which is
what I used), but it also mentions TypeHandlerCallback. 

 

TypeHandler seems very similar to TypeHandlerCallback, with the exception
that TypeHandlerCallback seems to have the ability to be applied globally. 

 

Which should I use? What is the difference?

 

You were right about the example, it is very similar to what I did.

 

 

P.S.

Mine was more terse.. 

I don't use constants unless they are needed in more than one method or
their value is not clear what they are doing. 

What do you have against the ternary operator?

 

 

--Rick Hightower


 


Re: Jeff RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Jeff Butler <je...@gmail.com>.
Hi Rick,

It turns out that you can use either one.  TypeHandlerCallback is a bit
simpler to implement than TypeHandler, but either will do.  We call the XML
attributes "typeHandler" for brevity.

If you use TypeHandlerCallback, then the class
com.ibatis.sqlmap.engine.type.CustomTypeHandler is used internally to
simulate a full TypeHandler.

Off to update the documentation...sigh...

Jeff Butler

PS - I have nothing against the ternary operator!  I didn't write that
section of the manual, so I don't make any claims about the code style :)




On 6/29/06, Rick <ri...@arc-mind.com> wrote:
>
>   Jeff,
>
>
>
> I downloaded the guide out of svn. It does mention TypeHandler (which is
> what I used), but it also mentions TypeHandlerCallback.
>
>
>
> TypeHandler seems very similar to TypeHandlerCallback, with the exception
> that TypeHandlerCallback seems to have the ability to be applied globally.
>
>
>
> Which should I use? What is the difference?
>
>
>
> You were right about the example, it is very similar to what I did.
>
>
>
>
>
> P.S.
>
> Mine was more terse….
>
> I don't use constants unless they are needed in more than one method or
> their value is not clear what they are doing.
>
> What do you have against the ternary operator?
>
>
>
>
>
> --Rick Hightower
>
>
>

Jeff RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
Jeff,

 

I downloaded the guide out of svn. It does mention TypeHandler (which is
what I used), but it also mentions TypeHandlerCallback.

 

TypeHandler seems very similar to TypeHandlerCallback, with the exception
that TypeHandlerCallback seems to have the ability to be applied globally.

 

Which should I use? What is the difference?

 

You were right about the example, it is very similar to what I did.

 

 

P.S.

Mine was more terse.. 

I don't use constants unless they are needed in more than one method or
their value is not clear what they are doing.

What do you have against the ternary operator?

 

 

--Rick Hightower

  _____  

From: Jeff Butler [mailto:jeffgbutler@gmail.com] 
Sent: Thursday, June 29, 2006 5:42 AM
To: user-java@ibatis.apache.org
Subject: Re: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

 

Sorry - the latest developer's guide in SVN has it.  We update the
developer's guide fairly often but, for some reason, don't always update the
download page.  So there's a link on the download page called "Latest
OpenOffice Documents in Subversion" - then download iBATIS-SqlMaps-2.pdf.

 

I don't know how we expect newcomers to figure that out :(

 

Jeff Butler



 

On 6/29/06, Rick <ri...@arc-mind.com> wrote: 

Comments below.

 

  _____  

From: Jeff Butler [mailto: jeffgbutler@gmail.com] 
Sent: Wednesday, June 28, 2006 8:01 PM 


To: user-java@ibatis.apache.org 

Subject: Re: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'? 

 

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 :) 

(Great minds think alike.)

 

**What developers guide?

I just searched the iBatis developer guide (the PDF file) for the string
"TypeHandler" and it is not in there. 

Is this a future version of the developers guide? The one I downloaded on
6/16/2006 does not seem to have this (or did I miss it). 

 

You could register the type handler globally so it would always handle any
VARCHAR to boolean transalation.  

 

**Nice idea. This app handles all Booleans as VARCHAR(1) Y or N.

 

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). 

 

** Perhaps in the FAQ as well. I am more than happy to update the FAQ.

 

 

Jeff Butler

 



 

On 6/28/06, Rick < ricks_mailinglists@arc-mind.com
<ma...@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. 

 

 

 


Re: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Jeff Butler <je...@gmail.com>.
Sorry - the latest developer's guide in SVN has it.  We update the
developer's guide fairly often but, for some reason, don't always update the
download page.  So there's a link on the download page called "Latest
OpenOffice Documents in Subversion" - then download iBATIS-SqlMaps-2.pdf.

I don't know how we expect newcomers to figure that out :(

Jeff Butler



On 6/29/06, Rick <ri...@arc-mind.com> wrote:
>
>   Comments below…
>
>
>  ------------------------------
>
> *From:* Jeff Butler [mailto:jeffgbutler@gmail.com]
> *Sent:* Wednesday, June 28, 2006 8:01 PM
>
> *To:* user-java@ibatis.apache.org
> *Subject:* Re: How do you map a Java boolean property to a VARCHAR(1) that
> contains 'Y' or 'N'?
>
>
>
> 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 :)
>
> (Great minds think alike…)
>
>
>
> **What developers guide?
>
> I just searched the iBatis developer guide (the PDF file) for the string
> "TypeHandler" and it is not in there.
>
> Is this a future version of the developers guide? The one I downloaded on
> 6/16/2006 does not seem to have this (or did I miss it).
>
>
>
> You could register the type handler globally so it would always handle any
> VARCHAR to boolean transalation.
>
>
>
> **Nice idea. This app handles all Booleans as VARCHAR(1) Y or N.
>
>
>
> 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).
>
>
>
> ** Perhaps in the FAQ as well. I am more than happy to update the FAQ.
>
>
>
>
>
> Jeff Butler
>
>
>
>
>
>
>
> On 6/28/06, *Rick* <ri...@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.
>
>
>
>
>

RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
Thanks. I'll add these to my bag 'o tricks.

 

  _____  

From: Firas A. [mailto:ofbiz@idigna.com] 
Sent: Wednesday, June 28, 2006 11:48 PM
To: user-java@ibatis.apache.org
Subject: RE: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

 

Hello,

 

I had a similar problem and these were my alternatives:

 

1) Use CASE SQL expression and return true/false, ex:

SELECT CASE WHEN some_column = 'Y' THEN true
       ELSE false
       END As "ColumnName"
FROM SomeTable

 

2) Modify the setter/getter methods of the boolean property like this:

public void setBoolColumn(String yesOrNo) {... }

public boolean getBoolColumn() {

    return "Y".equals(this.yesOrNo);

}

 

In my case, the second alternative was better choise.

 

Hope this help.

 

</Firas>

 

 

  _____  

From: Rick [mailto:ricks_mailinglists@arc-mind.com] 
Sent: den 29 juni 2006 08:01
To: user-java@ibatis.apache.org
Subject: RE: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

Comments below.

 

  _____  

From: Jeff Butler [mailto:jeffgbutler@gmail.com] 
Sent: Wednesday, June 28, 2006 8:01 PM
To: user-java@ibatis.apache.org
Subject: Re: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

 

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 :)

(Great minds think alike.)

 

**What developers guide?

I just searched the iBatis developer guide (the PDF file) for the string
"TypeHandler" and it is not in there.

Is this a future version of the developers guide? The one I downloaded on
6/16/2006 does not seem to have this (or did I miss it).

 

You could register the type handler globally so it would always handle any
VARCHAR to boolean transalation.  

 

**Nice idea. This app handles all Booleans as VARCHAR(1) Y or N.

 

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). 

 

** Perhaps in the FAQ as well. I am more than happy to update the FAQ.

 

 

Jeff Butler

 



 

On 6/28/06, Rick <ri...@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. 

 

 


RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by "Firas A." <of...@idigna.com>.
Hello,
 
I had a similar problem and these were my alternatives:
 
1) Use CASE SQL expression and return true/false, ex:

SELECT CASE WHEN some_column = 'Y' THEN true
       ELSE false
       END As "ColumnName"
FROM SomeTable

 
2) Modify the setter/getter methods of the boolean property like this:

public void setBoolColumn(String yesOrNo) {... }
public boolean getBoolColumn() {
    return "Y".equals(this.yesOrNo);
}

 
In my case, the second alternative was better choise.
 
Hope this help.
 
</Firas>
 

  _____  

From: Rick [mailto:ricks_mailinglists@arc-mind.com] 
Sent: den 29 juni 2006 08:01
To: user-java@ibatis.apache.org
Subject: RE: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?



Comments below.

 

  _____  

From: Jeff Butler [mailto:jeffgbutler@gmail.com] 
Sent: Wednesday, June 28, 2006 8:01 PM
To: user-java@ibatis.apache.org
Subject: Re: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

 

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 :)

(Great minds think alike.)

 

**What developers guide?

I just searched the iBatis developer guide (the PDF file) for the string
"TypeHandler" and it is not in there.

Is this a future version of the developers guide? The one I downloaded on
6/16/2006 does not seem to have this (or did I miss it).

 

You could register the type handler globally so it would always handle any
VARCHAR to boolean transalation.  

 

**Nice idea. This app handles all Booleans as VARCHAR(1) Y or N.

 

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). 

 

** Perhaps in the FAQ as well. I am more than happy to update the FAQ.

 

 

Jeff Butler

 



 

On 6/28/06, Rick <ri...@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. 

 

 


RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
Comments below.

 

  _____  

From: Jeff Butler [mailto:jeffgbutler@gmail.com] 
Sent: Wednesday, June 28, 2006 8:01 PM
To: user-java@ibatis.apache.org
Subject: Re: How do you map a Java boolean property to a VARCHAR(1) that
contains 'Y' or 'N'?

 

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 :)

(Great minds think alike.)

 

**What developers guide?

I just searched the iBatis developer guide (the PDF file) for the string
"TypeHandler" and it is not in there.

Is this a future version of the developers guide? The one I downloaded on
6/16/2006 does not seem to have this (or did I miss it).

 

You could register the type handler globally so it would always handle any
VARCHAR to boolean transalation.  

 

**Nice idea. This app handles all Booleans as VARCHAR(1) Y or N.

 

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). 

 

** Perhaps in the FAQ as well. I am more than happy to update the FAQ.

 

 

Jeff Butler

 



 

On 6/28/06, Rick <ri...@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. 

 

 


Re: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Jeff Butler <je...@gmail.com>.
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 <ri...@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.
>
>
>

RE: How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
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.

 


How do you map a Java boolean property to a VARCHAR(1) that contains 'Y' or 'N'?

Posted by Rick <ri...@arc-mind.com>.
 

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.