0 Replies Latest reply on Dec 18, 2002 5:37 AM by Raymond Brandon

    Urgent!!: Got a java.math.BigDecimal while looking for java.

    Raymond Brandon Newbie

      Hi,

      I'm using JBoss 3.0.3 with Solid 2.0. I also tested this with JBoss 3.0.4 but got the same result. My application tries to store a row in to a table. The table looks like this:

      CREATE TABLE X (
      id VARCHAR (40) NOT NULL,
      bigDecimalType DECIMAL (18, 4) NOT NULL,
      bigIntegerType DECIMAL (18) NOT NULL,
      PRIMARY KEY (id)
      );

      JBoss debug output:

      2002-12-17 14:35:31,543 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Create command executing: INSERT INTO BigNumbersMandatory (bigDecimalType,bigIntegerType,id) VALUES (?,?,?)
      2002-12-17 14:35:31,543 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Set parameter: idx=1, jdbcType=DECIMAL, value=10
      2002-12-17 14:35:31,543 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Set parameter: idx=2, jdbcType=DECIMAL, value=100
      2002-12-17 14:35:31,543 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Set parameter: idx=3, jdbcType=VARCHAR, value=Piet
      2002-12-17 14:35:31,558 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCUpdateCommand] Rows affected = 1
      2002-12-17 14:35:31,574 TRACE [EjbTier] BigNumbersMandatoryBean.ejbPostCreate(BigNumbersMandatoryUpdate update)
      2002-12-17 14:35:31,590 TRACE [EjbTier] BigNumbersMandatoryBean.ejbStore(): key=id(Piet)
      2002-12-17 14:35:31,590 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Load command executing: SELECT BigNumbersMandatory.id,BigNumbersMandatory.bigDecimalType,BigNumbersMandatory.bigIntegerType FROM BigNumbersMandatory WHERE id=?
      2002-12-17 14:35:31,590 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Set parameter: idx=1, jdbcType=VARCHAR, value=Piet
      2002-12-17 14:35:31,621 DEBUG [org.jboss.ejb.plugins.jaws.jdbc.JDBCCommand] Got a java.math.BigDecimal: '100' while looking for a java.math.BigInteger


      Note that in the create table script the difference between bigint and bigdecimal is the precision.

      I looked at Informix, Oracle, InstandDB and DB2. They all use BigInteger and BigDecimal the same way, that is: A BigInteger is a BigDecimal without precision (on SQL table creation level). So why not then do the following:

      below is a piece of jboss-3.0.3-src\server\src\main\org\jboss\ejb\plugins\jaws\jdbc\JDBCCommand.java.

      See /*** ***/

      protected Object getResultObject(ResultSet rs, int idx, Class destination)
      throws SQLException{

      // log.debug("getting a "+destination.getName()+" from resultset at index "+idx);
      Object result = null;

      Method method = (Method)rsTypes.get(destination.getName());
      if(method != null) {
      try {
      result = method.invoke(rs, new Object[]{new Integer(idx)});
      if(rs.wasNull()) return null;
      return result;
      } catch(IllegalAccessException e) {
      log.debug("Unable to read from ResultSet: ",e);
      } catch(InvocationTargetException e) {
      log.debug("Unable to read from ResultSet: ",e);
      }
      }

      result = rs.getObject(idx);
      if(result == null)
      return null;

      if(destination.isAssignableFrom(result.getClass()) && !result.getClass().equals(MarshalledObject.class) )
      return result;
      else if(log.isDebugEnabled()) {
      /****
      Normally we get here
      ***/
      log.debug("Got a "+result.getClass().getName()+": '"+result+"' while looking for a "+destination.getName());

      }

      /***

      Why not test here to see if the required type is
      BigInteger and this BigDecimal has a scale of 0.
      if so, return ((java.math.BigInteger)result).toBigInteger() ?

      So:

      if ( !result.getClass().equals(MarshalledObject.class) ) {
      if(destination.isAssignableFrom(result.getClass()) )
      return result;
      else if ( result instanceof java.math.BigDecimal && destination.isAssignableFrom( java.math.BigInteger.class) ) {
      java.math.BigDecimal bd = (java.math.BigDecimal)result;
      if (bd.scale() == 0) {
      return (bd.toBigInteger());
      }
      }
      } else if(log.isDebugEnabled()) {
      log.debug("Got a "+result.getClass().getName()+": '"+result+"' while looking for a "+destination.getName());
      }

      Or am I missing something??

      ***/

      rest of the function as is.

      }

      Regards,

      Raymond Brandon