由于@Michal Tomanski提到below - 使用trait
/case object
■当存在一定问题。什么,你需要做的是:
sealed trait UserType {
val code: Int
}
case object Member extends UserType {
override val code: Int = 0
}
case object Anonymous extends UserType {
override val code: Int = 1
}
object UserType {
def byId(id: Int): UserType = id match {
case Member.code => Member
case Anonymous.code => Anonymous
case _ => throw new IllegalArgumentException("...")
}
}
implicit val enumColumnType = MappedColumnType.base[UserType, Int](
e => e.code,
i => UserType.byId(i)
)
上面会允许你做这样的查询:
UserTable
.filter(_.userType === (Member :: UserType))
.result
这正是@Michal Tomanski指出。你可以做一些小技巧来平滑一点。
只需修改您的特点是这样的:
sealed trait UserType {
val code: Int
// I added field below
val base: UserType = this
}
,然后你可以有你这样的查询:
UserTable
.filter(_.userType === Member.base)
.result
它可能会稍微更好的选择比铸造。
除此之外 - 我跟随@Michal Tomanski答案(使用Enumeration
)假设就足够了你的情况(或许与解决方案trait
/case object
s是更灵活,但另一方面有更多的管道,你需要做的事情可以在这个答案中看到)。
重复此:http://stackoverflow.com/questions/28331528/scala-enumerations-case-objects-in-slick-good-ractices –