I am using Slick 3.1.0 and need to persist the field of type java.time.LocalDate.
I have the model class:
case class Position(companyName: String, title: String, startDate: Option[LocalDate], endDate: Option[LocalDate], positionId: Option[Int] = None)
And the following mapping:
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)
}
How can I map startDate and endDate fields to be represented with DATE type in the database?
Stacktrace for NullPointerException in column mapper:
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)
Steps:
MappedColumnType
Date
in PositionTable
to Option[LocalDate]
. Judging from your model class, I guess startDate
and endDate
are nullable. If not, omit Option
.Example:
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)
}
}
You can extract your custom mapped column type into a trait and reuse them in other db classes of course.
Further reading: Using Custom Scalar Types in Queries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With