我想构建一个Task / Todo-List.有两个实体:
>任务:每个任务都有一个对下一个任务的可选引用.这样就构建了一个链表.目的是用户可以按其优先级排序任务.此顺序由任务到任务的引用表示.
> TaskList:表示带有标签的TaskList,以及对列表的第一个Task的引用.
case class Task(id:Option [Long],title:String,nextTask:Option [Task])
case class TaskList(label:String,firstTask:Option [Task])
现在我尝试为这两个实体编写数据访问对象(DAO).
import scala.slick.driver.H2Driver.simple._ import slick.lifted.MappedTypeMapper implicit val session: Session = Database.threadLocalSession val queryById = Tasks.createFinderBy( t => t.id ) def task(id: Long): Option[Task] = queryById(id).firstOption private object Tasks extends Table[Task]("TASKS") { def id = column[Long]("ID",O.PrimaryKey,O.AutoInc) def title = column[String]("TITLE") def nextTaskId = column[Option[Long]]("NEXT_TASK_ID") def nextTask = foreignKey("NEXT_TASK_FK",nextTaskId,Tasks)(_.id) def * = id ~ title ~ nextTask <> (Task,Task.unapply _) } private object TaskLists extends Table[TaskList]("TASKLISTS") { def label = column[String]("LABEL",O.PrimaryKey) def firstTaskId = column[Option[Long]]("FIRST_TASK_ID") def firstTask = foreignKey("FIRST_TASK_FK",firstTaskId,Tasks)(_.id) def * = label ~ firstTask <> (Task,Task.unapply _) }
不幸的是它没有编译.问题出在nextTask和firstTask的两个表的*投影中.
>找不到类型的证据参数的隐含值
scala.slick.lifted.TypeMapper [scala.slick.lifted.ForeignKeyQuery [SlickTaskRepository.this.Tasks.type,justf0rfun.bookmark.model.Task]]
>找不到scala.slick.lifted.TypeMapper类型的证据参数的隐含值[scala.slick.lifted.ForeignKeyQuery [SlickTaskRepository.this.Tasks.type,justf0rfun.bookmark.model.Task]]
我尝试使用以下TypeMapper来解决这个问题,但是也没有编译.
implicit val taskMapper = MappedTypeMapper.base[Option[Long],Option[Task]]( option => option match { case Some(id) => task(id) case _ => None },option => option match { case Some(task) => task.id case _ => None })
>找不到参数tm的隐含值:scala.slick.lifted.TypeMapper [Option [justf0rfun.bookmark.model.Task]]
>方法库的参数不足:(隐式tm:scala.slick.lifted.TypeMapper [Option [justf0rfun.bookmark.model.Task]])scala.slick.lifted.BaseTypeMapper [Option [Long]].未指定的值参数tm.
主要问题:如何以正确的方式使用Slick的提升嵌入和映射表?如何让这个工作?
提前致谢.