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

ClassCastException: BigDecimal cannot be cast to Integer

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0 Beta 3
    • 3.0 Beta 4
    • Core
    • None
    • Oracle 10, ojdbc14.jar 10.2.0.4.0, Java 5, Java 6

    Description

      Hello,

      This happend to me when using Ibatis 3 beta 3. Also happens with beta 2. I did not test beta 1.
      Database: oracle 10g, using ojdbc14.jar 10.2.0.4.0. Happens on Java 5 and Java 6.

      The exception:

      java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer

      Also happens if I use Long instead of Integer:
      java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long

      The Exception happens when accessing the results, not during the query execution.

      The query , from ProductMapper.xml

      <select id="selectProductCodes" resultType="Integer">
      select distinct productcode from product
      </select>

      productcode defined on product table as NUMBER(5) not null,

      The test code:
      ------------------------
      import java.util.List;

      public class Test {

      public static void main(String[] args)

      { List<Integer> codes = ProductDA.selectProductCodes(); for (Integer code: codes) System.out.println("code: " + code); }

      }
      ------------------------

      ProductDA.java:
      ------------------------
      import java.util.List;

      import org.apache.ibatis.session.SqlSession;

      public class ProductDA {

      public static List<Integer> selectProductCodes() {

      SqlSession session = Ibatis.getSession();

      try

      { ProductMapper productMapper = session.getMapper(ProductMapper.class); return productMapper.selectProductCodes(); }

      finally

      { session.close(); }

      }
      }
      ------------------------

      ProductMapper.java:

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

      public interface ProductMapper

      { List<Integer> selectProductCodes() ; }

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

      Ibatis.java:

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

      import java.io.Reader;

      import org.apache.ibatis.io.Resources;
      import org.apache.ibatis.session.SqlSession;
      import org.apache.ibatis.session.SqlSessionFactory;
      import org.apache.ibatis.session.SqlSessionFactoryBuilder;

      public class Ibatis {

      private static SqlSessionFactory sqlSessionFactory;

      private Ibatis () {
      }

      private static synchronized void init () {

      if (sqlSessionFactory == null)
      try

      { Reader reader = Resources.getResourceAsReader("ibatis.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); }

      catch (Exception e)

      { throw new RuntimeException(e); }

      }

      public static SqlSession getSession()

      { if (sqlSessionFactory == null) init(); return sqlSessionFactory.openSession(); }

      }
      ------------------------

      Ibatis.xml (partial):
      <environment id="test">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
      <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="url" value="jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:ORACLE10G"/>
      <property name="username" value="username"/>
      <property name="password" value="***"/>
      </dataSource>
      </environment>

      Attachments

        1. DefaultResultSetHandler.java
          22 kB
          Eduardo Moritz Cavalcanti
        2. Ibatis3Test2.hsqldb.zip
          1.14 MB
          Eduardo Moritz Cavalcanti
        3. src2.zip
          2 kB
          Eduardo Moritz Cavalcanti

        Activity

          People

            cbegin Clinton Begin
            eduardo.m.cavalcanti@uol.com.br Eduardo Moritz Cavalcanti
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: