gpt4 book ai didi

java - Slick 3 java.time.LocalDate 映射

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:22 24 4
gpt4 key购买 nike

我正在使用 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)
}

如何映射 startDate 和 endDate 字段以在数据库中用 DATE 类型表示?

列映射器中 NullPointerException 的堆栈跟踪:

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)

最佳答案

步骤:

  1. 定义隐式MappedColumnType
  2. PositionTable 中的类型 Date 更改为 Option[LocalDate]。从您的模型类来看,我猜 startDateendDate 可以为空。如果不是,请省略 Option
  3. 你完成了

示例:

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)
}
}

当然,您可以将自定义映射列类型提取到特征中,并在其他数据库类中重用它们。

进一步阅读:Using Custom Scalar Types in Queries

关于java - Slick 3 java.time.LocalDate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com