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 Reumann <ri...@gmail.com> on 2006/05/14 18:38:22 UTC

My Boolen handler seems to be only working in one direction...

Based off some examples in the archives, I have this Y-N boolean handle 
that is supposed to handle converting true/false to Y/N in the db and 
vice versa.

The problem seems to be that it's working fine for converting data that 
needs to go into the DB as Y/N, but going the other direction, all my 
boolean object properties are remaining false. I have debugging 
statements in the "getResult" method below and I never the method being 
called (it's not a cache issue either since I have debugging on for 
iBATIS and can see the result set returning).

In my sqlMap-config I've defined:

<typeHandler javaType="java.lang.Boolean" 
callback="com.company.persistence.ibatis.BooleanYNTypeHandler"/>

Does it matter that the bean properties are primitive booleans and not 
Boolean? (I wouldn't think so since it seems to be fine going in the one 
direction -- bean > db).


BooleanYNTypeHandler:

public void setParameter(ParameterSetter setter, Object parameter) 
throws SQLException
{
     if (parameter == null) {
          setter.setString(null);
     } else {
         Boolean bool = (Boolean)parameter;
         String boolVal = "N";
         if (bool.booleanValue()) {
             boolVal = "Y";
         }
         setter.setString(boolVal);
     }
}

public Object getResult(ResultGetter getter) throws SQLException
{
     if (getter.wasNull()){
          return null;
     }

     String dbVal = getter.getString();
     if (dbVal.equalsIgnoreCase("Y")) {
         return Boolean.TRUE;
     } else {
         return Boolean.FALSE;
     }
}

public Object valueOf(String s)
{
     return s;
}