Today i came across a new Data Type.. Its called BigDecimal/BigInteger,etc.... I actually did not use this in my program but chanced on it because of an error i got while i tried to run my simple code for user details.
case class UserInfo (
id: Int,
name: String,
age: Int,
address: String
)
object UserInfo{
val ds = DB.getDataSource()
val data = {
get[Pk[Int]]("id") ~
get[String] ("name") ~
get[Int]("age") ~
get[String]("address") map {
case id~name~age~address => UserInfo(id, name, age, address)
}
}
This was a simple code and should've run without any errors. I had no errors on my eclipse window... But on executing the code i got the following error for output.
[RuntimeException: TypeDoesNotMatch(Cannot convert 1:class java.math.BigInteger to Int for column ColumnName(userinfo.id,Some(id)))]
So i did what every newbie does... I googled the error.... To my surprise I wasn't the only one who came across this error. And after fiddling around with the code i figured out the following :
- The Database returns BigDecimal values that are part of the java.math.BigDecimal package, which is not a scala package. So we need to explicitly import this package.
import java.math.BigDecimal
So, this is my finally working code,
import java.math.BigInteger
case class UserInfo (
id: Int,
name: String,
age: Int,
address: String
)
object UserInfo{
val ds = DB.getDataSource()
val data = {
get[BigInteger]("id") ~
get[String] ("name") ~
get[Int]("age") ~
get[String]("address") map {
case id~name~age~address => UserInfo(id.intValue, name, age, address)
} //> data : anorm.RowParser[models.UserInfo] = <function1>
}
Yep, I made a few changes.. Well, thats what I'm coming to next... Here's what i couldn't figure out...
I couldn't figure out how to get Pk[Int] to be compatible with BigInteger. So even after importing the java package and changing my id type to Pk[BigInteger] i couldn't return a compatible id value.
If anyone has the answer to that, please let me know. Else whenever I figure it out I will update it.