我使用的是Slick 3.1.0,需要保留java.time.LocalDate类型的字段。
我有模特课:
case class Position(companyName: String, title: String, startDate: Option[LocalDate], endDate: Option[LocalDate], positionId: Option[Int] = None)
以下映射:
private[PositionTable] class PositionTable(tag: Tag) extends Table[Position](tag, "POSITIONS") {
val positionId = column[Int]("POSITION_ID", O.PrimaryKey, O.AutoInc)
val companyName = column[String]("COMPANY_NAME")
val title = column[String]("TITLE")
val startDate = column[Date]("START_DATE")
val endDate = column[Date]("END_DATE")
def * = (companyName, title, startDate, endDate, positionId.?) <>(Position.tupled, Position.unapply)
}
如何在数据库中映射使用DATE类型表示的startDate和endDate字段?
列映射器中的NullPointerException的Stacktrace:
java.lang.NullPointerException
at com.tiedin.repo.PositionTable$class.$init$(PositionRepository.scala:54)
at repo.PositionRepositoryTest.<init>(PositionRepositoryTest.scala:13)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at org.scalatest.tools.Runner$.genSuiteConfig(Runner.scala:1422)
at org.scalatest.tools.Runner$$anonfun$31.apply(Runner.scala:1236)
at org.scalatest.tools.Runner$$anonfun$31.apply(Runner.scala:1235)
at scala.collection.immutable.List.map(List.scala:273)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1235)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
at org.scalatest.tools.Runner$.run(Runner.scala:850)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
答案 0 :(得分:8)
<强>步骤:强>
MappedColumnType
Date
中的PositionTable
类型更改为Option[LocalDate]
。从您的模型类来看,我猜startDate
和endDate
可以为空。如果没有,请省略Option
。示例:强>
import java.sql.Date
import java.time.LocalDate
// other imports
class SomeDbClass {
implicit val localDateToDate = MappedColumnType.base[LocalDate, Date](
l => Date.valueOf(l),
d => d.toLocalDate
)
private[PositionTable] class PositionTable(tag: Tag) extends Table[Position](tag, "POSITIONS") {
val positionId = column[Int]("POSITION_ID", O.PrimaryKey, O.AutoInc)
val companyName = column[String]("COMPANY_NAME")
val title = column[String]("TITLE")
val startDate = column[Option[LocalDate]]("START_DATE")
val endDate = column[Option[LocalDate]]("END_DATE")
def * = (companyName, title, startDate, endDate, positionId.?) <>(Position.tupled, Position.unapply)
}
}
您可以将自定义映射列类型提取到特征中,然后在其他数据库类中重用它们。