【问题标题】:JdbcSQLSyntaxErrorException with h2 and jooq带有 h2 和 jooq 的 JdbcSQLSyntaxErrorException
【发布时间】:2020-10-02 10:36:05
【问题描述】:

我有一个列类型为 text[] 的表格。

我将 dsl 与 jooq 与 H2 一起使用。该表的字符串值数组为:

+-------------+
|column_name  |
+-------------+
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
|[str1, str2] |
+-------------+

Table 是一个使用 jooq 生成的 java 类。 当我执行以下查询并打印结果时:

 println(
        dsl
          .select(Table.column_name)
          .from(Table)
          .fetch())

它打印我上面的表格数据。

但是当我执行时:

 println(
        dsl
          .select(arrayAggDistinct(elementAt(Table.column_name, 1)))
          .from(Table)
          .fetch())

它抛出了一个异常:

o]   Cause: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "db.Table.column_name" not found; SQL statement:
[info] select array_agg(distinct "db"."Table"."column_table"[1]) from db.Table [42122-200]
[info]   at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
[info]   at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
[info]   at org.h2.message.DbException.get(DbException.java:205)
[info]   at org.h2.message.DbException.get(DbException.java:181)
[info]   at org.h2.expression.ExpressionColumn.getColumnException(ExpressionColumn.java:163)
[info]   at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:145)
[info]   at org.h2.expression.function.Function.optimize(Function.java:2594)
[info]   at org.h2.expression.aggregate.AbstractAggregate.optimize(AbstractAggregate.java:92)
[info]   at org.h2.expression.aggregate.Aggregate.optimize(Aggregate.java:705)
[info]   at org.h2.command.dml.Select.prepare(Select.java:1206)

它使用普通 PostgreSQL 执行正常,但是在使用 H2 的这个查询中给我错误。

如何让它与 H2 一起工作?

【问题讨论】:

    标签: java h2 dsl jooq


    【解决方案1】:

    请查看DSL.arrayAggDistinct()@Support 注释。从 jOOQ 3.13 开始,它包括以下方言:

    • AURORA_POSTGRES
    • COCKROACHDB
    • HSQLDB
    • POSTGRES

    在不久的将来,H2 将支持更多符合标准的 ARRAY 类型,这与 PostgreSQL 的类似:https://github.com/h2database/h2database/issues/2190

    届时,jOOQ 还将增加对 H2 数组和数组函数的支持:https://github.com/jOOQ/jOOQ/issues/10175,但目前,您根本无法在 H2 上使用此功能。

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 2016-07-02
      • 2016-09-30
      • 2022-11-21
      • 2021-04-30
      • 2014-05-19
      • 2013-01-05
      • 2018-02-10
      • 2014-06-15
      相关资源
      最近更新 更多