简体   繁体   English

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

[英]converting java.io.File to java.sql.Blob in Scala

I am trying to transform a java.io.File to a java.sql.Blob. 我正在尝试将java.io.File转换为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. 更具体地说,我在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 _)

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? 从java.io.File到Blob的映射(将文件存储在数据库内部)之间发生错误,我该怎么做?

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 在尝试存储文件之前,必须将文件实际读入与blob兼容的文件中,java.io.File只是一个描述符

From Slick's documentation LOB types: java.sql.Blob, java.sql.Clob, Array[Byte] 来自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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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