jOOQ JSON 查询结果为ORA-00979

jOOQ JSON query results in ORA-00979

我正在尝试对 Oracle 19c 数据库执行此查询:

Field<JSON> employee = DSL.field("employee", JSON.class);

Table<Record1<JSON>> employees = dsl
            .select(jsonObject(jsonEntry("id", EMPLOYEE.ID), jsonEntry("name", EMPLOYEE.NAME), jsonEntry("phones",
                    jsonArrayAgg(
                            jsonObject(jsonEntry("number", PHONE.PHONENUMBER), jsonEntry("type", PHONE.TYPE)))
            )).as(employee))
            .from(EMPLOYEE)
            .join(PHONE).on(PHONE.EMPLOYEE_ID.eq(EMPLOYEE.ID))
            .groupBy(EMPLOYEE.ID)
            .asTable();

String json = dsl
            .select(jsonArrayAgg(employees.field(employee)))
            .from(employees)
            .fetchOneInto(String.class);

但是我明白了

org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar 
    [select json_arrayagg("alias_113372058".employee) from 
        (select json_object(key ? value "EMPLOYEE"."ID", key ? value "EMPLOYEE"."NAME", key ? value json_arrayagg(json_object(key ? value "PHONE"."PHONENUMBER", key ? value "PHONE"."TYPE"))) employee from "EMPLOYEE" join "PHONE" on "PHONE"."EMPLOYEE_ID" = "EMPLOYEE"."ID" group by "EMPLOYEE"."ID") "alias_113372058"]; 
nested exception is java.sql.SQLSyntaxErrorException: ORA-00979: Kein GROUP BY-Ausdruck

jOOQs JSON 功能是否不适用于 Oracle?

这与您的 JSON 用法无关。如果您删除所有内容并改为编写此查询,也会发生同样的事情:

dsl.select(EMPLOYEE.ID, EMPLOYEE.NAME)
   .from(EMPLOYEE)
   .join(PHONE).on(PHONE.EMPLOYEE_ID.eq(EMPLOYEE.ID))
   .groupBy(EMPLOYEE.ID);

您的查询可以在 MySQL、PostgreSQL 或标准 SQL 中运行,您仍然可以在按主键列分组后投影所有功能相关的列。但在 Oracle 中,这是行不通的。因此,您必须将 EMPLOYEE.NAME 添加到 GROUP BY 子句中。

有一个功能请求可以相应地转换你的 SQL,但是 jOOQ 3.14 还不支持这个:https://github.com/jOOQ/jOOQ/issues/4725

请注意,JSON_ARRAYAGG() 将空集聚合到 NULL,而不是空集 []