首页 > 解决方案 > 带有 h2 和 jooq 的 JdbcSQLSyntaxErrorException

问题描述

我有一个列类型为 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 一起工作?

标签: javah2dsljooq

解决方案


请查看 的@Support注释DSL.arrayAggDistinct()。从 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 上使用此功能。


推荐阅读