Slick 2.0:无法使用HList将java.sql.Timestamp插入到表中

时间:2014-02-05 22:28:43

标签: scala casting timestamp slick

我在这个脚本中遇到了一个类强制转换异常,我根本无法理解。错误是:

java.lang.ClassCastException:java.sql.Timestamp无法强制转换为scala.Product

这是我的代码:

import java.sql.Timestamp
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.collection.heterogenous._
import syntax._
import org.joda.time.DateTime
import db.DealDAO
import DateTimeImplicits._

// Implicitly convert DateTime to Timestamp (and it seems to work)
object DateTimeImplicits {
   implicit def DateTime2Timestamp(value : DateTime) = new Timestamp(value.getMillis) 
}

object TryHList {

  class Tests(tag: Tag) extends Table[Timestamp :: HNil](tag, "tests") {
    def timeCol = column[Timestamp]("time_col")
    def * = (timeCol :: HNil)
  }

  def tests = TableQuery[Tests]

  def createTable = DealDAO.db.withSession { implicit session =>
    tests.ddl.create
  }

  def insert(dt: DateTime) = DealDAO.db.withSession { implicit session =>
    tests += (dt :: HNil)
  }

}

object Main {

  def main(args: Array[String]): Unit = {
  //  TryHList.createTable
     TryHList.insert(new DateTime())
  }
}

1 个答案:

答案 0 :(得分:2)

使用单元素HList时,您在Slick中发现了一个错误。我提交了2.0.1修正案(RC1可能会在下周末到达):https://github.com/slick/slick/pull/658

无关:如果您还不知道,请查看https://github.com/tototoshi/slick-joda-mapper