简体   繁体   中英

converting java.io.File to java.sql.Blob in Scala

I am trying to transform a java.io.File to a java.sql.Blob. More specifically I have a model in Scala Play that needs to store a file inside of a database, here is what I have so far.

    case class Complication(complicationId: Long,
    vitalSignId: Long,
    complication: String, definition: String, reason: String,
    treatment: String, treatmentImage: Option[java.io.File], notes: String,
    weblinks: String, upperlower: String)

  object ComplicationDAO extends Table[Complication]("complications") with GasGuruDatabase {

    def complicationId = column[Long]("complication_id", O.PrimaryKey, O.AutoInc)
    def vitalSignId = column[Long]("vital_sign_id")
    def complication = column[String]("complication")
    def definition = column[String]("definition", O.DBType("varchar(4000)"))
    def reason = column[String]("reason", O.DBType("varchar(4000)"))
    def treatment = column[String]("treatment", O.DBType("varchar(4000)"))
    def treatmentImage = column[Option[Blob]]("treatment_image")
    def notes = column[String]("notes", O.DBType("varchar(4000)"))
    def weblinks = column[String]("weblinks")
    def upperlower = column[String]("upperlower")
    def * = complicationId ~ vitalSignId ~ complication ~ definition ~ reason ~ treatment ~ treatmentImage ~ notes ~ weblinks ~ upperlower <> (Complication, Complication.unapply _)

The error is happening in between the mapping from a java.io.File to a Blob to store the file inside of the database, how can I do this?

Thanks!

You have to actually read the file into something compatible with blob before trying to store it, java.io.File is just a descriptor

From Slick's documentation LOB types: java.sql.Blob, java.sql.Clob, Array[Byte]

case class Complication(complicationId: Long,
vitalSignId: Long,
complication: String, definition: String, reason: String,
treatment: String, treatmentImage: **Option[Array[Byte]]**, notes: String,
weblinks: String, upperlower: String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM