MappedColumnType:当MySQL列定义为选项[String]-ergo nullable时,将字符串转换为向量[String]
我的目标是用MappedColumnType:当MySQL列定义为选项[String]-ergo nullable时,将字符串转换为向量[String],mysql,scala,slick,Mysql,Scala,Slick,我的目标是用向量[String]填充Slick实体的filters变量,该向量是通过解析MySQL的filters列中存储的字符串生成的 我拥有的Slick和MySQL表定义的简化版本如下: object Something { class Something(tag: Tag) extends Table[SomethingRow](tag, "SOMETHING") { def filters = column[Vector[String]]("FILTERS")
向量[String]
填充Slick实体的filters
变量,该向量是通过解析MySQL的filters列中存储的字符串生成的
我拥有的Slick和MySQL表定义的简化版本如下:
object Something {
class Something(tag: Tag) extends Table[SomethingRow](tag, "SOMETHING") {
def filters = column[Vector[String]]("FILTERS")
override def * = (filters).mapTo[SomethingRow]
}
}
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[SlickException: Read NULL value (null) for ResultSet column <computed>]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:310)
用于映射db行的case类是:
case类SomethingRow(过滤器:Vector[String])
为了启用此转换,我定义了以下隐式:
隐式val stringToVectorMapper=MappedColumnType.base[Vector[String],String](
{vctr=>
如果(vctr==null | | vctr.isEmpty)StringUtils.EMPTY
else vctr.mkString(“,”)
},
{str=>
if(StringUtils.isEmpty(str))Vector.empty
else str.split(“,”).toVector
})
我将其导入Something
类和相应的存储库中
我得到的错误如下:
object Something {
class Something(tag: Tag) extends Table[SomethingRow](tag, "SOMETHING") {
def filters = column[Vector[String]]("FILTERS")
override def * = (filters).mapTo[SomethingRow]
}
}
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[SlickException: Read NULL value (null) for ResultSet column <computed>]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:310)
我收到一个“未找到”错误:
No implicits found for evidence: MySQLProfile.BaseColumnType[Option[String]]
我通过添加一个空的字符串
作为数据库中过滤器
列的默认值来解决这个问题,这样它就不会为null,但这不是一个解决方案。问题仍然存在:如何为可为空的db列定义灵活的自定义数据类型。我解决了这个问题,添加了一个空的String
作为数据库中filters
列的默认值,这样它就不会为空,但这不是一个解决方案。问题仍然存在:如何为可为空的db列定义灵活的自定义数据类型。