将光滑的3绑定/转换对象播放到列表

时间:2015-08-04 16:26:55

标签: scala playframework slick slick-3.0

我的案例类为 Target does not support branch tracing.

UserAccount

case class UserAccount(Id: Option[Long], Name: String, occupation: Occupation)

的对象
Occupation

我使用play slick 3

创建了一个模式
sealed trait Occupation

object Occupation {

  case object Teacher extends Occupation
  case object Student extends Occupation
  case object Others extends Occupation

}

当我试图运行该项目时。由于列class AccountSchema(tag: Tag) extends Table[UserAccount](tag, "user_account") { def id = column[Long]("id", O.PrimaryKey, O.AutoInc) def name = column[String]("name") def occupation = column[Occupation]("occupation") def * = (id.?, name, occupation) <>((UserAccount.apply _).tupled, UserAccount.unapply) }

,我收到错误

如何在光滑的表列中使用对象占用?

1 个答案:

答案 0 :(得分:1)

您需要添加从关系模型到对象模型的映射,反之亦然。

sealed trait Occupation

object Occupation {

  implicit val occupationType = MappedColumnType.base[Occupation, String](
{ occupation => if(occupation==Teacher)  "Teacher" else if (occupation == Student) "Student" else "Others"},
{ occupation => if(occupation == "Teacher") Teacher else if (occupation == "Student") Student else Others }
)
  case object Teacher extends Occupation
  case object Student extends Occupation
  case object Others extends Occupation
}

以下是更多详情:http://slick.typesafe.com/doc/3.0.0/userdefined.html