ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niels Beekman" <n.beek...@wis.nl>
Subject RE: boolean type handler
Date Wed, 03 May 2006 17:37:26 GMT
I think you should remove the jdbcType-attribute from the
typeHandler-tag, see:

 

http://www.mail-archive.com/user-java@ibatis.apache.org/msg03985.html

 

Niels

 

________________________________

From: Paul Allen [mailto:pea1@cornell.edu] 
Sent: woensdag 3 mei 2006 19:05
To: user-java@ibatis.apache.org
Subject: RE: boolean type handler 

 


------------------------------------------------------------------------
---

Here's my SQL Map Config.

------------------------------------------------------------------------
---

<sqlMapConfig >
<typeHandler jdbcType= "VARCHAR" javaType= "java.lang.Boolean" callback=
"clo.is.sample.ibatis.CharBooleanHandler" />

<sqlMap resource= "clo/is/sample/ibatis/UserAccount.xml" />
<sqlMap resource= "clo/is/sample/ibatis/ProjUser.xml" />

</sqlMapConfig >

------------------------------------------------------------------------
---

Here's part of ProjUser.XML

------------------------------------------------------------------------
---

<typeAlias alias= "projUser" type= "org.birdsource.core.db.ProjUser" />

<insert id= "insertProjUserComplete" parameterClass ="projUser" >

INSERT INTO PROJ_USER (
   PROJ_ID, USER_ID, PROJ_PERIOD_ID, GROUP_ID, 
   GIFT_USER_ID, PAID, FEE_ID, BEGIN_DT, 
   END_DT, AUTH_GROUP, AUTH_VAL1, AUTH_VAL2, 
   AUTH_VAL3, AUTH_VAL4, CREATE_USER_ID, CREATE_DT, 
   EDIT_USER_ID, EDIT_DT, 
   REGISTRY_DT, 
   ENABLED, 
   NOTES, 
   MULTI_USE, 
   REQUEST_ID, TRANSACTION_ID
) VALUES (
   #projID:VARCHAR#, #userID:VARCHAR#, #projPeriodID:VARCHAR#,
#groupID:VARCHAR#, 
   #giftUserID:VARCHAR#, 
   #paid,jdbcType=VARCHAR,javaType=java.lang.Boolean#, 
   #feeID:VARCHAR#, #beginDate:DATE#, 
   #endDate:DATE#, #authGroup:VARCHAR#, #authVal1:VARCHAR#,
#authVal2:VARCHAR#, 
   #authVal3:VARCHAR#, #authVal4:VARCHAR#, #createUserID:VARCHAR#,
#createDate:DATE:SYSDATE#, 
   #editUserID:VARCHAR#, #editDate:DATE:SYSDATE#, 
   #registryDate,jdbcType=DATE,javaType=java.util.Date#, 
   #enabled,jdbcType=VARCHAR,javaType=java.lang.Boolean#, 
   #notes:VARCHAR#, 
   #multiUse,jdbcType=VARCHAR,javaType=java.lang.Boolean#, 
   #requestID:VARCHAR#, #transactionID:VARCHAR#
)
</ insert >

------------------------------------------------------------------------
---

Here's my CustomTypeHandler. I never see the "setParameter: object is
null." message even when 
the paid property is null.

------------------------------------------------------------------------
---

package clo.is.sample.ibatis;

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;

/**
 * @author pea1
 *  
 */
public class CharBooleanHandler implements TypeHandlerCallback {

        /**
         *  
         */
        public CharBooleanHandler() {
                 super();
        }

        public void setParameter(ParameterSetter setter, Object
parameter) throws SQLException {
                 
                 if (parameter == null) {
                           System.err.println("setParameter: object is
null. Setting as empty string");
                           setter.setString("");
                 }
                 else {
                          boolean b = ((Boolean)
parameter).booleanValue();
                          if (b) {
                                    System.err.println("setParameter:
object is true. Setting as 1");
                                    setter.setString("1");
                          } else {
                                    System.err.println("setParameter:
object is false. Setting as 0");
                                    setter.setString("0");
                          }
                 }
        }

        /*
         * (non-Javadoc)
         * 
         * @see
com.ibatis.sqlmap.client.extensions.TypeHandlerCallback#getResult(com.ib
atis.sqlmap.client.extensions.ResultGetter)
         */
        public Object getResult(ResultGetter getter) throws SQLException
{
                 String s = getter.getString();
                 if (s == null) {
                           System.err.println("getResult: object is
null. returning null");
                          return null;
                 } else if ("0".equalsIgnoreCase(s)) {
                           System.err.println("getResult: object is 0.
returning false");
                          return new Boolean(false);
                 } else {
                           System.err.println("getResult: object is not
0. returning true");
                          return new Boolean(true);
                 }
        }

        public Object valueOf(String s) {
                 if (s == null) {
                           System.err.println("valueOf: string is null.
returning null");
                          return null;
                 }
                 else if ("0".equalsIgnoreCase(s)) { 
                           System.err.println("valueOf: string is 0.
returning false");
                          return new Boolean(false);
                 }
                 else {
                           System.err.println("valueOf: string is not 0.
returning true");
                          return new Boolean(false);
                 }
        }

}


At 12:35 PM 5/3/2006, Niels Beekman wrote:



Hi,

Could you post the actual typehandler code and the piece of SQLMap where
you use it?

-----Original Message-----
From: Paul Allen [ mailto:pea1@cornell.edu <mailto:pea1@cornell.edu> ] 
Sent: woensdag 3 mei 2006 18:32
To: user-java@ibatis.apache.org
Subject: boolean type handler 

I've seen some threads that describe how to make a custom type handler
to 
map from a Boolean bean property to a CHAR SQL type and I've implemented

one version of them. What I am trying to get is this mapping:

object property value --> DB value
Boolean.TRUE --> "1"
Boolean.FALSE --> "0"
null --> null

However I keep coming up with the same problem, by the time the 
setParameter(ParameterSetter setter, Object parameter) method of my
handler 
is called, the parameter is always a Boolean object, even when I think
it 
should be null because the Boolean the property from my bean is null.

Is there anyway around this?


Mime
View raw message