使用Jooq从scala插入数组字段

kyvafyod  于 2023-11-18  发布在  Scala
关注(0)|答案(1)|浏览(138)

我在用Postgres和Jooq

object JooqBuilder {
  private val defaultSettings = new Settings()
    .withRenderFormatted(true)
    .withRenderNameStyle(RenderNameStyle.AS_IS)
    .withParseDialect(SQLDialect.POSTGRES)

  def default(conn: Connection) =
    DSL.using(conn, SQLDialect.POSTGRES, defaultSettings)
}

字符串
这是我的插页

val insert = JooqBuilder
          .default(conn)
          .insertInto(
            table(MY_TABLE),
            field("id", classOf[String]),
            field("dimensions", classOf[Array[String]]),            
            field("dimensionCodes", classOf[Array[Long]])
          )

        items.foreach { i =>
          insert.values(
            i.id,            
            i.dimensions.toArray,     // scala List[String] to java array     
            i.dimensionCodes.toArray  // scala List[Long] to java array        
          )
        }
        insert.execute()


它在运行时失败,

Cause: java.lang.ClassCastException: class [J cannot be cast to class [Ljava.lang.Object; ([J is in module java.base of loader 'bootstrap')
  at org.jooq.tools.Convert$ConvertAll.from(Convert.java:520)
  at org.jooq.tools.Convert.convert0(Convert.java:324)
  at org.jooq.tools.Convert.convert(Convert.java:316)
  at org.jooq.tools.Convert.convert(Convert.java:387)
  at org.jooq.impl.DefaultDataType.convert(DefaultDataType.java:825)
  at org.jooq.impl.InsertImpl.addValue(InsertImpl.java:334)
  at org.jooq.impl.InsertImpl.values(InsertImpl.java:314)
  at org.jooq.impl.InsertImpl.values(InsertImpl.java:273)
  at org.jooq.impl.InsertImpl.values(InsertImpl.java:124)


我怀疑i.dimensionCodes.toArray返回了我检查过的基本类型数组调试器

else if (fromClass.isArray()) {
    Object[] fromArray = (Object[]) from;

    // [#3062] [#5796] Default collections if no specific collection type was requested


from是long[]
有什么变通办法吗?

qacovj5a

qacovj5a1#

t.dimensionCodes.toArray.map(l => java.lang.Long.valueOf(l))

字符串
明确的拳击帮助。

相关问题