在Scala中将java.io.File转换为java.sql.Blob

时间:2014-06-06 00:16:08

标签: java scala playframework-2.0 play-slick

我正在尝试将java.io.File转换为java.sql.Blob。更具体地说,我在Scala Play中有一个需要在数据库中存储文件的模型,这是我到目前为止所拥有的。

    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 _)

错误发生在从java.io.File到Blob的映射之间,以便将文件存储在数据库中,我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:1)

在尝试存储文件之前,你必须实际将文件读入与blob兼容的东西,java.io.File只是一个描述符

来自Slick的文档 LOB类型: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)