How to troubleshoot RuntimeException: TypeDoesNotMatch (Can not convert 1: java.lang.String to long class for column?

advertisements

I am developing simple web application with play framework with scala and storing databse in mysql and i am getting error

what am i doing :

  • when user enter values and after storing i am redirecting user to page that show all entries present in the database
  • values are getting stored but it give exception at time of redirecting to page

models/Keyword.scala

case class Keyword(blog: String,cat: String,word: String,count: Long, summaryId: String)

object Keyword {

  val keyw = {
    get[String]("keyword.blog")~
    get[String]("keyword.cat")~
    get[String]("keyword.word")~
  get[Long]("keyword.count") ~
  get[String]("keyword.summaryId") map {
    case blog~cat~word~count~summaryId => Keyword(blog,cat,word,count, summaryId)
  }
}

  def all(): List[Keyword] = DB.withConnection { implicit c =>
  SQL("select * from keyword").as(Keyword.keyw *)
}

def create(key: Keyword){DB.withConnection{implicit c=>
  SQL("insert into keyword values({blog}, {cat}, {word},{count},{summaryId})").on('blog->key.blog,
      'cat -> key.cat,
      'word-> key.word,
      'count-> key.count,
      'summaryId -> key.summaryId).executeUpdate()
}
}

controllers/Application.scala

object Application extends Controller {

  val ta:Form[Keyword] = Form(
          mapping(
    "blog" -> nonEmptyText,
    "cat" -> nonEmptyText,
    "word" -> nonEmptyText,
    "count"-> of[Long],
    "summaryId"-> nonEmptyText
  )(Keyword.apply)(Keyword.unapply)
  )

  def index = Action {
    Ok(html.index(ta));
  }
  def newTask=  Action { implicit request =>
  ta.bindFromRequest.fold(
    errors => BadRequest(html.index(errors)),
    keywo => {

      Keyword.create(keywo)
      Ok(views.html.data(Keyword.all()))
    }
  )
}

- when i am using h2-database that time it is running

- when i am using MySQL that time it is giving exception

Give me some idea to solve this issue!


Create an AnormExtension helper in app/helpers folder. Call it AnormExtension.scala. Code below:

object AnormExtension {

implicit def rowJavaLongToLong: Column[Long] = Column.nonNull { (value, meta) =>
  val MetaDataItem(qualified, nullable, clazz) = meta
    value match {
       case d: java.lang.Long => Right(d.longValue())
       case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to Long for column " + qualified))
    }
  }
}