【问题标题】:slick schema projection issue with date日期的光滑模式投影问题
【发布时间】:2018-05-31 16:50:06
【问题描述】:

我正在尝试为 slick 3.2 中的简单表创建模式。我对 scala 很陌生,所以这可能是一个愚蠢的问题。当我尝试删除日期投影时似乎工作正常,并且我能够正常运行选择查询,但是当我添加日期时,我什至无法编译代码。

以下是我的架构代码:

import slick.jdbc.OracleProfile.api._
import java.sql.Date

class User(tag: Tag)
  extends slick.jdbc.OracleProfile.api.Table[(Long , String , Date)](tag, "USR") {

  def usr_arch_dt : Rep[Date]= column[Date]("USR_ARCH_DT")
  def usr_id : Rep[Long] = column[Long]("USR_ID", O.PrimaryKey)
  def usr_subj_txt : Rep[String]= column[String]("USR_SUBJ_TXT")

  def * : (Long, String,  Date) =
    (usr_id , usr_subj_txt , usr_arch_dt) // I see mentioned error here

}

我在 Intellij 中看到的异常或错误是:

Rep[Long] 类型的表达式不符合预期的 Long 类型。

如果我能够编译此代码,我希望根据日期范围进行搜索,例如

val filterQuery: Query[User, (Long, String , Date), Seq] =
        ntfns.filter(_.usr_arch_dt > Calendar.getInstance().getTime )

谢谢!

【问题讨论】:

    标签: scala slick-3.0


    【解决方案1】:

    您的def * : (Long, String, String, Date) 好像多了一个String

    在日期搜索中,您的过滤器查询将不起作用,因为 java.sql.Date 的方法有限。 Slick 的一个常见做法是在 java.sql.Date 和功能更丰富的 Joda Time 之间创建一个隐式映射器:

    import scala.slick.driver.JdbcProfile.MappedColumnType
    import java.sql.Date
    import org.joda.time.DateTime
    // ...
    
    implicit def dateTimeMapper = MappedColumnType.base[DateTime, Date] (
      dt => new Date(dt.getMillis),
      d => new DateTime(d)
    )
    

    这是一个相关的SO link

    【讨论】:

    • 将尝试 Joda 时间并让您知道。
    【解决方案2】:

    谢谢@Leo,额外的字符串是一个错字,我现在将其从问题中删除以供其他人受益。下面的代码对我有用:

    case class User(tag: Tag)
      extends slick.jdbc.OracleProfile.api.Table[(Long , String , Date)](tag, "USR") {
    
      def usr_arch_dt : Rep[Date]= column[Date]("USR_ARCH_DT")
      def usr_id : Rep[Long] = column[Long]("USR_ID", O.PrimaryKey)
      def usr_subj_txt : Rep[String]= column[String]("USR_SUBJ_TXT")
    
      def * = (usr_id  , usr_subj_txt , usr_arch_dt)
    }
    

    我还需要看过滤器,我会试试你的方法,让你知道结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      相关资源
      最近更新 更多