我可以将case类映射到光滑的数据库表: -
case class SomeTimeStamp(id: Option[Long], timestamp: java.sql.Timestamp )
class TimeStampTable(tag: Tag) extends Table[SomeTimeStamp](tag, "TSTAMP_TABLE") {
def id = column[Long]("ID", O.AutoInc, O.PrimaryKey)
def time = column[java.sql.Timestamp]("TIME")
def * = (id.?, time) <> (SomeTimeStamp.tupled, SomeTimeStamp.unapply)
}
case类具有默认情况下通过slick转换为数据库类型的字段,所以一切都很好。
但是,如果我的case类中的字段不是默认数据库类型,则这不起作用。 Slick允许我使用MappedColumnType提供与有效数据库类型的隐式转换,所以我尝试了这一点,请注意我现在使用的是Java LocalDatetime而不是sql Timestamp。
case class SomeLocalDate(id: Option[Long], timestamp: java.time.LocalDateTime )
class LocalDateTable(tag: Tag) extends Table[SomeLocalDate](tag, "TLDATE_TABLE") {
import LocalDateTable._
def id = column[Long]("ID", O.AutoInc, O.PrimaryKey)
def time = column[java.time.LocalDateTime]("TIME")
def * = (id.?, time) <> (SomeLocalDate.tupled, SomeLocalDate.unapply)
}
object LocalDateTable {
implicit val localDateTimeToTimestamp = MappedColumnType.base[LocalDateTime, Timestamp](
{ Timestamp.valueOf(_) } ,
{ ts => ts.toLocalDateTime }
)
}
我添加了隐式映射列,我仍然在投影和隐式解析中得到编译错误 编译错误是: -
[info] Compiling 1 Scala source to ...target/scala-2.11/classes...
[error] db/Tables.scala:92: could not find implicit value for parameter tt: slick.ast.TypedType[java.time.LocalDateTime]
[error] def time: Rep[LocalDateTime] = column[java.time.LocalDateTime]("TIME")
如果我修改了def时间并添加了类型归属,我会得到一个不同的错误,见下文。
case class SomeLocalDate(id: Option[Long], timestamp: java.time.LocalDateTime )
class LocalDateTable(tag: Tag) extends Table[SomeLocalDate](tag, "TLDATE_TABLE") {
import LocalDateTable._
def id = column[Long]("ID", O.AutoInc, O.PrimaryKey)
def time: Rep[LocalDateTime] = column[java.time.LocalDateTime]("TIME")
def * = (id.?, time) <> (SomeLocalDate.tupled, SomeLocalDate.unapply)
}
object LocalDateTable {
implicit val localDateTimeToTimestamp = MappedColumnType.base[LocalDateTime, Timestamp](
{ Timestamp.valueOf(_) } ,
{ ts => ts.toLocalDateTime }
)
}
给出了这个错误: -
[info] Compiling 1 Scala source to ...target/scala-2.11/classes...
[error] db/Tables.scala:92: could not find implicit value for parameter tt: slick.ast.TypedType[java.time.LocalDateTime]
[error] def time: Rep[LocalDateTime] = column[java.time.LocalDateTime]("TIME")
[error] ^
[error]db/Tables.scala:94: No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
[error] Required level: slick.lifted.FlatShapeLevel
[error] Source type: (slick.lifted.Rep[Option[Long]], slick.lifted.Rep[java.time.LocalDateTime])
[error] Unpacked type: (Option[Long], java.time.LocalDateTime)
[error] Packed type: Any
[error] def * = (id.?, time) <> (SomeLocalDate.tupled, SomeLocalDate.unapply)
[error] ^
[error] two errors found
如果案例类没有默认支持的数据库类型(例如LocalDateTime),我需要做什么才能将案例类映射到表?
欢呼声
答案 0 :(得分:1)
问题是在Table类之后声明了隐式MappedColumnType。由于某种原因,即使我将其导入表格之上,这也会破坏隐式解决方案。
当我删除案例类并简单地使用元组时,我找到了答案。当我这样做时,出现了以下错误
[info] Compiling 1 Scala source to target/scala-2.11/classes...
[error] /db/Tables.scala:100: could not find implicit value for parameter tt: slick.ast.TypedType[java.time.LocalDateTime]
[error] def time: Rep[LocalDateTime] = column[java.time.LocalDateTime]("TIME")
[error] ^
[error] /db/Tables.scala:102: type mismatch;
[error] found : (slick.lifted.Rep[Option[Long]], slick.driver.H2Driver.api.Rep[java.time.LocalDateTime])
[error] (which expands to) (slick.lifted.Rep[Option[Long]], slick.lifted.Rep[java.time.LocalDateTime])
[error] required: slick.lifted.ProvenShape[(Option[Long], java.time.LocalDateTime)]
[error] Note: implicit value localDateTimeToTimestamp is not applicable here because it comes after the application point and it lacks an explicit result type
[error] def * : ProvenShape[(Option[Long], LocalDateTime)] = (id.?, time)
[error] ^
所以要明确解决方案是将隐式转换放在表定义之上,如果在同一个文件中或将其放在另一个文件中并导入它,请参阅下面的工作解决方案。感谢。
case class SomeLocalDate(id: Option[Long], timestamp: java.time.LocalDateTime )
object LocalDateTableConversions {
implicit val localDateTimeToTimestamp = MappedColumnType.base[LocalDateTime, Timestamp](
{ Timestamp.valueOf(_) } ,
{ ts => ts.toLocalDateTime }
)
}
import LocalDateTableConversions._
class LocalDateTable(tag: Tag) extends Table[SomeLocalDate](tag, "TLDATE_TABLE") {
def id = column[Long]("ID", O.AutoInc, O.PrimaryKey)
def time: Rep[LocalDateTime] = column[java.time.LocalDateTime]("TIME")
def * = (id.?, time) <> (SomeLocalDate.tupled, SomeLocalDate.unapply)
}