Scala + Slick how to map json to the table column with datatype as blob

微笑、不失礼 提交于 2019-12-12 02:07:59

问题


I need to map json values to the table using slick and store whole json as a blob in the same table as well.

def createJob(jobs: JobEntity): Future[Option[Long]] =
db.run(job returning job.map((_.id)) += jobs)

My Json contains values like

id(long),name(string), type(string) 

while the table I am trying to map has columns

id(long),name(string), type(string),json_data(blob)

I have JobEntity class as

case class JobEntity(id: Option[Long] = None, name: String, type: String) {

require(!jobname.isEmpty, "jobname.empty")

}

How do I map the json to the json_data column?


回答1:


Slick supports the following LOB types: java.sql.Blob, java.sql.Clob, Array[Byte] (see the documentation)

Therefore you want to use one of these types. Your table definition could look like this:

  case class Job(id: Option[Long] = None, name: String, `type`: String, json_data: java.sql.Blob)

  class Jobs(tag: Tag) extends Table[Job](tag, "jobs") {
    def id = column[Option[Long]]("id")
    def name = column[String]("name")
    def `type` = column[String]("type")
    def json_data = column[java.sql.Blob]("json_data")
    def * = (id, name, `type`, json_data) <> (Job.tupled, Job.unapply)
  }


来源:https://stackoverflow.com/questions/43784131/scala-slick-how-to-map-json-to-the-table-column-with-datatype-as-blob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!