Uploaded image for project: 'iBatis for Java [READ ONLY]'
  1. iBatis for Java [READ ONLY]
  2. IBATIS-794

New typehandler implementation is not called during select statment.



    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 3.0 Beta 10
    • None
    • Core



      I have created a new type handler called GregorianCalendarTypeHandler. This type handler is used to convert java type: java.util.GregorianCalendar to jdbc type: DATE and vice verse. The code is the following:

      import java.sql.CallableStatement;
      import java.sql.Date;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Time;
      import java.sql.Timestamp;
      import java.util.Calendar;
      import java.util.GregorianCalendar;
      import java.util.Locale;

      import org.apache.ibatis.type.JdbcType;
      import org.apache.ibatis.type.TypeHandler;

      public class GregorianCalendarTypeHandler implements TypeHandler
      public Object getResult(final ResultSet rs, final String columnName) throws SQLException

      { return this.createCalendar(rs.getObject(columnName)); }

      public Object getResult(final CallableStatement cs, final int columnIndex) throws SQLException

      { return this.createCalendar(cs.getObject(columnIndex)); }

      public void setParameter(final PreparedStatement ps, final int i, final Object parameter, final JdbcType jdbcType) throws SQLException
      final Calendar calendar = (Calendar) parameter;
      if (JdbcType.DATE.equals(jdbcType))

      { ps.setDate(i, new Date(calendar.getTimeInMillis()), calendar); }

      else if (JdbcType.TIME.equals(jdbcType))

      { ps.setTime(i, new Time(calendar.getTimeInMillis()), calendar); }

      else if (JdbcType.TIMESTAMP.equals(jdbcType))

      { ps.setTimestamp(i, new Timestamp(calendar.getTimeInMillis()), calendar); }


      { throw new SQLException("The " + this.getClass() + " typehandler can handle only DATE, TIME, TIMESTAMP types not: " + jdbcType); }


      protected Calendar createCalendar(final Object sqlDate) throws SQLException
      if (!(sqlDate instanceof java.util.Date))

      { throw new SQLException("The following object is not an instance of the java.util.Date: " + sqlDate); }

      final Locale locale = Get it from somewhere;
      final TimeZone timeZone = Get it from somewhere;

      final GregorianCalendar calendar = new GregorianCalendar(locale,timezone);

      calendar.setTime((java.util.Date) sqlDate);

      return calendar;

      As you can see below, I have registered this new type handler into my config.xml:

      <typeHandler javaType="java.util.GregorianCalendar" jdbcType="DATE" handler="somepackage.GregorianCalendarTypeHandler"/>

      I'm facing with the following problem:

      When I run an insert statement anything is fine, the GregorianCalendar is converted to date with the type handler's setParameter method and the row is inserted in the database.

      The problem is with the reading. When a select statement is executed the Date value from the database is queried but it is not set to the POJO's proper property.

      I had done some investigation and debug and figured out the problem is with the TypeHandlerRegistry#hasTypeHandler(Class) method. This method calls the other hasTypeHandler(Class, JDBCType) but with null value as jdbc type. The "TYPE_HANDLER_MAP" map member contains my handler object with the key "DATE", but the method tries to get a handler with the key null!

      It is also strange that the other built in type handlers are stored into this member with null as key.

      Please fix this error (if this is a bug, or I don't know I'm doing something wrong?) and help me. If you need more info about this issue please let me know.

      Regards, István.




            Unassigned Unassigned
            itoth István Tóth
            0 Vote for this issue
            0 Start watching this issue