Search code examples
scalaanorm

PlayFramework 2: Fetch data from h2 database, decimal types not map with Scala decimal type


i am new in PlayFramework and Scala. I am using PlayFramework anorm for enable database connection and use embedded h2 database. When i insert the values into the table, the insertion done successfully. But when i fetch the data from database it generates and error as below:

[RuntimeException: Left(TypeDoesNotMatch(Cannot convert 25.50: class java.math.BigDecimal to Float for column ColumnName(USER_DETAIL.AGE,Some(AGE))))]

Following is my POJO:

case class UserDetail(
 val id: Int,
 val name: String,
 val age: Float
) 

Following is my controller:

def getUserDetail = Action{
val userDetail = UserDetail(13, "James", 25.9F);
var sql: SqlQuery = SQL("SELECT * FROM USER_DETAIL");
def users: List[UserDetail] =  DB.withConnection { implicit connection => 
  sql().map(row => UserDetail(row[Int]("id"), row[String]("name"), row[Float]("age"))).toList
}
println(">>>>>>>>>>>>>>>>>>>>: "+users)
Ok(Json.toJson(users));
}

Following is my table structure:

create table User_Detail(
 id int NOT NULL PRIMARY KEY,
 name varchar(45),
 age decimal(20, 2)
)

I am also trying to replace decimal type with double in table, but it again generate the same error like above

[RuntimeException: Left(TypeDoesNotMatch(Cannot convert 25.50: class java.lang.Double to Float for column ColumnName(USER_DETAIL.AGE,Some(AGE))))

Solution

  • How about

    case class UserDetail(
     val id: Int,
     val name: String,
     val age: Double
    )
    

    edit:

    And then use row[Double]("age") when reading it