Search code examples
playframework-2.0derbyanorm

Why does Anorm's executeInsert complain about converting from BigDecimal?


I am using a Derby database with Anorm in Play 2.0.2. Even though no column has a BigDecimal field, I get the following error when inserting into a table.

Execution exception [[RuntimeException: TypeDoesNotMatch(Cannot convert 1:class java.math.BigDecimal to Long for column .1)]]

If I look in the table, I can see that my data has been entered so I infer that the problem has to do with returning the new key. But I clearly declare the key as a bigint--why the complaint about BigDecimal?


Solution

  • It turns out that Derby always returns the key as a BigDecimal--even if they key is defined otherwise. The solution is to use a custom ResultSetParser to handle the BigDecimal. The id column returned is named "1".

      def idResultSetParser(implicit extractor: anorm.Column[java.math.BigDecimal]) =
        ResultSetParser.singleOpt[java.math.BigDecimal](
          anorm.SqlParser.get[java.math.BigDecimal]("1"))
    

    Or, putting it all together.

      import java.math.BigDecimal
      s.executeInsert[Option[BigDecimal]](
          ResultSetParser.singleOpt[BigDecimal](
              anorm.SqlParser.get[BigDecimal]("1")))
    

    You can then map it to a Long with map (_.longValue).