【发布时间】:2014-01-21 16:08:19
【问题描述】:
我有一个枚举类型 ResourceType,我试图使用 slick API 将它作为 Int 存储在数据库中。我已经为 ResourceType 定义了一个自定义类型映射器,但是我在表定义中对 * 的定义出现编译器错误,提示“找不到匹配的形状。Slick 不知道如何映射给定的类型。”。是否有可能完成这项工作?
import scala.slick.driver.H2Driver.simple._
case class Resource(val id : Option[Int], val creationDate : Date, val title : String, val resourceType : ResourceType, val description : String) {
}
case class ResourceType private(val databaseCode : Int, val label : String) {
}
object ResourceType {
val lessonPlan = new ResourceType(1, "Lesson Plan")
val activity = new ResourceType(2, "Activity")
val all = scala.collection.immutable.Seq(lessonPlan, activity)
private val _databaseCodeMap = all.map(t => t.databaseCode -> t).toMap
def apply(databaseCode : Int) = _databaseCodeMap(databaseCode)
}
class ResourceTable(tag : Tag) extends Table[Resource](tag, "Resource") {
def id = column[Option[Int]]("ID", O.PrimaryKey, O.AutoInc)
def creationDate = column[Date]("CreationDate")
def title = column[String]("Title")
def resourceType = column[Int]("ResourceType")
def description = column[String]("Description")
implicit val resourceTypeTypeMapper = MappedColumnType.base[ResourceType, Int](_.databaseCode, ResourceType(_))
//Compile error on this line
def * = (id, creationDate, title, resourceType, description) <> (Resource.tupled, Resource.unapply)
}
【问题讨论】: