1

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!

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
Lilluda 5
  • 1,111
  • 3
  • 18
  • 38
  • TIP: don't store your files in DB - instead put them in the filesystem and in DB save only path to it, here's why: http://stackoverflow.com/q/3748/1066240 – biesior Jun 06 '14 at 10:49

1 Answers1

1

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)
Azzie
  • 366
  • 1
  • 4