Scala / Slick 3.0.1 – 更新多个列
发布时间:2020-12-16 18:01:55  所属栏目:安全  来源:网络整理 
            导读:每当我收到给定id的更新请求时,我都会尝试更新DB表中的masterId和updatedDtTm列(我不想更新我的createdDtTm).以下是我的代码: case class Master(id:Option[Long] = None,masterId:String,createdDtTm:Option[java.util.Date],updatedDtTm:Option[java.util
                
                
                
            | 
                         
 每当我收到给定id的更新请求时,我都会尝试更新DB表中的masterId和updatedDtTm列(我不想更新我的createdDtTm).以下是我的代码: 
  
  
  
case class Master(id:Option[Long] = None,masterId:String,createdDtTm:Option[java.util.Date],updatedDtTm:Option[java.util.Date])
/**
 * This is my Slick Mapping table
 * with the default projection
 */
`class MappingMaster(tag:Tag) extends
Table[Master](tag,"master") {
    implicit val DateTimeColumnType = MappedColumnType.base[java.util.Date,java.sql.Timestamp](
    {
      ud => new Timestamp(ud.getTime)
    },{
      sd => new java.util.Date(sd.getTime)
    })
    def id = column[Long]("id",O.PrimaryKey,O.AutoInc)
    def masterId = column[String]("master_id")
    def createdDtTm = column[java.util.Date]("created_dttm")
    def updatedDtTm = column[java.util.Date]("updated_dttm")
    def * = (id.?,masterId,createdDtTm.?,updatedDtTm.?) <>
      ((Master.apply _).tupled,Master.unapply _) }
/**
 * Some where in the DAO update call
 */
db.run(masterRecords.filter(_.id === id).map(rw =>(rw.masterId,rw.updatedDtTm)).
update(("new_master_id",new Date()))
// I also tried the following
db.run(masterRecords.filter(_id === id).map(rw => (rw.masterId,rw.updatedDtTm).shaped[(String,java.util.Date)]).update(("new_master_id",new Date())) 
 Slick的文档指出,为了更新多个列,需要使用map来获取相应的列,然后更新它们. 这里的问题如下 – 更新方法似乎接受Nothing的值. 我也试过以下做同样的事情: val t = for {
ms <- masterRecords if (ms.id === "1234")
} yield (ms.masterId,ms.updateDtTm)
db.run(t.update(("new_master_id",new Date()))) 
 当我编译代码时,它给了我以下编译异常: Slick does not know how to map the given types. [error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List). [error] Required level: slick.lifted.FlatShapeLevel [error] Source type: (slick.lifted.Rep[String],slick.lifted.Rep[java.util.Date]) [error] Unpacked type: (String,java.util.Date) [error] Packed type: Any [error] db.run(masterRecords.filter(_id === id).map(rw => (rw.masterId,new Date())) 我使用Scala 2.11与Slick 3.0.1和IntelliJ作为IDE.真的很感激,如果你能对此有所了解. 干杯, 解决方法
 (取代原始答案)似乎隐含必须在查询的范围内,这编译: 
  
  
  
        case class Master(id:Option[Long] = None,updatedDtTm:Option[java.util.Date])
implicit val DateTimeColumnType = MappedColumnType.base[java.util.Date,java.sql.Timestamp](
  {
    ud => new Timestamp(ud.getTime)
  },{
    sd => new java.util.Date(sd.getTime)
  })
class MappingMaster(tag:Tag) extends Table[Master](tag,"master") {
    def id = column[Long]("id",O.AutoInc)  
    def masterId = column[String]("master_id")
    def createdDtTm = column[java.util.Date]("created_dttm")
    def updatedDtTm = column[java.util.Date]("updated_dttm")
    def * = (id.?,updatedDtTm.?) <> ((Master.apply _).tupled,Master.unapply _)
}
private val masterRecords = TableQuery[MappingMaster]
val id: Long = 123
db.run(masterRecords.filter(_.id === id).map(rw =>(rw.masterId,rw.updatedDtTm)).update("new_master_id",new Date()))
val t = for {
  ms <- masterRecords if (ms.id === id)
} yield (ms.masterId,ms.updatedDtTm)
db.run(t.update(("new_master_id",new Date())))
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
