Tôi có enum sau:Làm thế nào để tồn tại giá trị enum trong trơn
object LoginStatus extends Enumeration() with BitmaskedEnumeration {
type LoginStatus = Value
val Active = Value("A")
val Inactive = Value("I")
}
tôi cần phải kiên trì giá trị của enum "A", nhưng khi sql được tạo ra kết quả là 0. đây là ánh xạ bảng:
object LoginTable extends Table[Login]("login") {
def idLogin = column[Int]("idlogin", O.PrimaryKey, O.AutoInc)
def cdLogin = column[String]("cdlogin", O.NotNull)
def cdPass = column[String]("cdPass", O.NotNull)
def stLogin = column[LoginStatus]("stlogin", O.NotNull, O.DBType("character(1)"))
}
làm thế nào để duy trì giá trị enum?
tôi thực hiện
implicit val charMapper = MappedTypeMapper.base[Char, String](
b => b.toString(),
i => i.charAt(0))
implicit def enum2StringMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, Char](
b => b.toString.charAt(0),
i => enum.withName(i.toString))
implicit val LoginStatusMapper = enum2StringMapper(LoginStatus)
nhưng cho kết quả:
[error] c.Login - Invalid value for type int : A
điều này có phần liên quan http://stackoverflow.com/questions/19030875/how-can-i-create-a-custom-column-type-with-typesafe-slick-in-scala/19040124#19040124 – cvogt
Điều này cũng được thảo luận trong danh sách gửi thư của Slick https://groups.google.com/d/msg/scalaquery/Cd5iG-tJchM/fEIhq8IPVJQJ – cvogt
cũng liên quan đến http://stackoverflow.com/questions/18752929/how-to-use -enums-in-scala-slick – cvogt