【问题标题】:Insert Query to Oracle using Slick 2.1使用 Slick 2.1 向 Oracle 插入查询
【发布时间】:2015-03-16 07:19:07
【问题描述】:

在使用 Slick 向 oracle 表插入查询时,我遇到了非常难以理解的问题。而且我不知道如何定位错误。

"com.typesafe.slick"      %%  "slick"                   % "2.1.0",
 "com.typesafe.slick"      %%  "slick-extensions"        % "2.1.0"

通过插入,我总是得到这个异常:

[DEBUG] - from [scala.slick.jdbc.JdbcBackend.statement] in [pool-1-thread-1] - [Preparing insert statement: insert into "NOTIFICATION_MESSAGE" ("CLIENT_ID","IMPORTANCE","TYPE_ID","ACCOUNT","TITLE","MESSAGE","STATUS","CREATION_DATE")  values (?,?,?,?,?,?,?,?), returning: ID]
[ERROR] - from [mypackage.db.core.OracleDatabaseComponent] in [pool-1-thread-1] - [DB operation failed - error: [An SQLException was provoked by the following failure: java.lang.ArrayIndexOutOfBoundsException: 8]]
mypackage.db.core.DBProcessException: DB operation failed - error: [An SQLException was provoked by the following failure: java.lang.ArrayIndexOutOfBoundsException: 8]

但最奇怪的是,如果我从插入查询中删除一个参数(将填充为默认值),它将成功执行。 看起来像插入查询中的值数量限制,但其他表没有问题(其中一些表超过 8 列)。 也许有人遇到过这个问题或知道如何定位错误?

表 ddl:

create table NOTIFICATION_MESSAGE (
  id         number(20)                   not null,
  client_id          nvarchar2(32)                not null,
  importance         number(1)      default 1     not null,
  type_id            number(2)                    not null,
  account            nvarchar2(32),
  title              nvarchar2(255)               not null,
  message            nvarchar2(2000)              not null,
  status             nvarchar2(32)  default 'NEW' not null,
  creation_date      date       default sysdate   not null
)

光滑的模型:

import com.typesafe.slick.driver.oracle.OracleDriver.simple._   
case class NotificationMessage(
                                    id: Option[Long],
                                    clientId: String,
                                    importance: Int = 1,
                                    typeId: Int,
                                    account: Option[String],
                                    title: String,
                                    message: String,
                                    status: NotificationStatus,
                                    creationDate: Instant
                                    )
    class NotificationMessageTable(tag: Tag) extends Table[NotificationMessage](tag, "NOTIFICATION_MESSAGE") {

        def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
        def clientId = column[String]("CLIENT_ID", O.NotNull)        
        def importance = column[Int]("IMPORTANCE", O.NotNull)        
        def typeId = column[Int]("TYPE_ID", O.NotNull)        
        def account = column[Option[String]]("ACCOUNT")        
        def title = column[String]("TITLE", O.NotNull)        
        def message = column[String]("MESSAGE", O.NotNull)        
        def status = column[NotificationStatus]("STATUS", O.NotNull)        
        def creationDate = column[Instant]("CREATION_DATE", O.NotNull)        
        override def * : ProvenShape[NotificationMessage] =
          (id.?, clientId, importance, typeId, account, title, message, status, creationDate) <>
            (NotificationMessage.tupled, NotificationMessage.unapply)
    }

【问题讨论】:

    标签: oracle scala slick


    【解决方案1】:

    删除id。?

    只是身份证。该列未标记为可选。

    如您所见,插入中只使用了 8 个参数。

    【讨论】:

    • id 对于插入查询应该是可选的,它将由触发器在数据库中定义。您能否解释一下 column[Option[Long]] 和使用“.”之间的区别?在定义 * 中?
    • def id = column[Long]("ID", O.NotNull, O.PrimaryKey) def * = (id, clientId, important, typeId, account, title, message, status, creationDate) (NotificationMessage.tupled, NotificationMessage.unapply)
    • 它不会编译,因为 Slick 不知道如何映射给定的类型。
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2017-09-29
    • 2015-08-23
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2014-08-03
    • 2016-02-08
    相关资源
    最近更新 更多