我认为这可以解决您的问题
trait Mapping {
//Need to change JdbcDriver to the driver that you will use in your code (MySql, Postgres, etc)
import scala.slick.driver.JdbcDriver.profile.simple._
// Models
case class EntryDetails(createDate: Option[DateTime] = None, updateDate: Option[DateTime] = None)
case class Entry(id: Int, text: String, details: EntryDetails)
//Implicit Joda Mappers for datetime columns
implicit def timestamp2dateTime = MappedColumnType.base[DateTime, Timestamp](
dateTime => new Timestamp(dateTime.getMillis),
date => new DateTime(date))
//Table mapping
class Entries(tag: Tag) extends Table[Entry](tag, "entry") {
def entryDetails = (createDate, updateDate) <>(EntryDetails.tupled, EntryDetails.unapply)
def * = (id, text, entryDetails) <>(Entry.tupled, Entry.unapply)
val id: Column[Int] = column[Int]("id")
val text: Column[String] = column[String]("text")
val createDate: Column[Option[DateTime]] = column[Option[DateTime]]("createDate")
val updateDate: Column[Option[DateTime]] = column[Option[DateTime]]("updateDate")
}
//Table query, used in slick 2.0 for querying a table
lazy val EntryTableQuery = TableQuery[Entries]
}
我将所有内容都包含在特征映射中,以便为您的答案打包代码。据我了解,您希望将一个表映射到两个对象,一个在另一个对象中,这可以通过创建另一个映射方法来实现,这里称为 entryDetails,将表查询映射到 EntryDetails 模型对象。然后您可以将此映射方法添加到您的对象映射中,即 * 方法。方法 entryDetails 将只是该映射方法的另一个参数。