2016-11-06 47 views
0

我有一個模型需要一個org.joda.time.DateTime 但我傳遞了一個java .sql.Timestamp這是由光滑的物體表[]用,我試圖用隱式轉換,但它不工作將org.joda.time.DateTime隱式轉換爲java.sql.Timestamp從模型轉換爲浮動Table []

import models.Carros.convertDateToTimestamp // this has a parameter DateTime and a return Timestamp 
def * = (id, name, year, description, img, keywords, state, model, datein) <> 
((Carro.apply _).tupled, Carro.unapply) // so here when unpacking shouldn't the implicit conversion do it's job? 

所示的錯誤是在這裏:

找不到匹配的形狀。 Slick不知道如何映射給定的 類型。可能的原因:表[T]中的T與您的* 投影不匹配。或者您在查詢中使用不受支持的類型(例如,scala 列表)。需要等級:slick.lifted.FlatShapeLevel來源類型: (slick.lifted.Rep [Option [Long]],slick.lifted.Rep [String], slick.lifted.Rep [Int],slick.lifted.Rep [ String], slick.lifted.Rep [String],slick.lifted.Rep [String], slick.lifted.Rep [String],slick.lifted.Rep [Long], slick.lifted.Rep [java。 sql.Timestamp])未包裝的類型:(選項[龍], 字符串,整數,字符串,字符串,字符串,字符串,龍, org.joda.time.DateTime)盒裝類型:任何

回答

1

你會需要將datebin的類型聲明爲org.joda.DateTime,而不是java.sql.Timestamp

class CarroTable extends Table[Carro](tag: "carro") { 
    ... 
    val datebin = column[org.joda.DateTime]("datebin") 

    def * = (id, name, year, description, img, keywords, state, model, datein) <> (Carro.tupled, Carro.unapply) 

} 

然後確保你在的地方有一個隱式類型映射:

implicit val JodaDateTimeMapper = MappedColumnType.base[DateTime, Timestamp](
    dt => new Timestamp(dt.getMillis), 
    ts => new DateTime(timestamp.getTime()) 
) 
0

羅馬的答案是正確的,但有一點需要注意...

你必須把上面的隱式MappedColumnType表格定義,如果你把它放在同一個文件中,或放在另一個文件中並導入它。如果你不這樣做,那麼隱式解析就無法找到它。

你可以看到這在此other StackOverflow question

所以要迂腐糾正你應該這樣做: -

object implicitDateTimeConverters { 
    implicit val JodaDateTimeMapper = MappedColumnType.base[DateTime, Timestamp](
    dt => new Timestamp(dt.getMillis), 
    ts => new DateTime(timestamp.getTime()) 
) 
} 

import implicitDateTimeConverters._ 

class CarroTable extends Table[Carro](tag: "carro") { 
    ... 
    val datebin = column[org.joda.DateTime]("datebin") 

    def * = (id, name, year, description, img, keywords, state, model, datein) <> (Carro.tupled, Carro.unapply) 

} 
相關問題