jOOQ 和自动生成,如何避免 table POJO 中的 UDT 记录

jOOQ and autogeneration, how to avoid UDT Records inside table POJOs

我在 PostgreSQL 数据库中定义了一个类型 T 和一个视图 V

CREATE TYPE my_type AS
(
  mt_column1 smallint NOT NULL
);

CREATE VIEW my_view
AS SELECT
   some_column_id integer
   ARRAY(SELECT
      ROW(an_int)::my_type
      FROM a_table
   ) AS my_view_types
  FROM a_regular_table 
  WHERE my_condition_hold);

使用 版本 3.7 上的代码生成,我得到了一个 UDT 记录 class MyTypeRecord 和一个 table 记录 class MyViewRecord 和 UDT POJO class MyType 和 table POJO class MyView.

生成class的MyView有一个MyTypeRecord的数组。

public class MyView extends Object implements Serializable, Cloneable, IMyView {

    private static final long serialVersionUID = 1984808170;

    private final Long           some_column_id;
    private final MyTypeRecord[] my_view_types;
}

在 POJO 中我希望有一个 POJO 数组,例如:

    private final MyType[] my_view_types;

另一个有趣的事实是类型的 pojo 和记录在 udt 文件夹中,而对于视图它们在 tables 文件夹中:也许这可以帮助找到一个solution/explanation.

有没有办法在生成时使 View 成为仅限 pojo 的转换?


根据要求,我附上了一个工作示例,它按照我的描述生成记录和 POJO。它在 this link.

与 FileDropper 共享

我还报告了一个避免这个问题的可能技巧,如果你真的绝望的话可以使用。据报道,jOOQ即使我们分配一个POJO而不是记录,也无法自动将记录数组转换为记录classMyTypeRecord。因此,您可以使用函数 array_to_jsonROW 的数组解析为 json。在我的例子中是:

CREATE VIEW my_view
AS SELECT
   some_column_id integer
   array_to_json(ARRAY(SELECT        
        ROW(an_int)::my_type         
      FROM a_table
   ))::json AS my_view_types
  FROM a_regular_table 
  WHERE my_condition_hold);

如果您注册 this 绑定,jOOQ 应该会自动将其转换为 JSON。

它之所以这样做,是因为 View 没有与之相关联的 PrimaryKey,至少对于大多数数据库而言,我想不出一个一个会报告 PrimaryKey 的视图。

您可以使用 <syntheticPrimaryKeys> 指定生成的主键,也可以按照手册 advanced generator configuration 部分中的说明使用 <overridePrimaryKeys>

jooq-meta configuration的相关部分:

<!-- A regular expression matching all columns that participate in "synthetic" primary keys,
       which should be placed on generated UpdatableRecords, to be used with

        - UpdatableRecord.store()
        - UpdatableRecord.update()
        - UpdatableRecord.delete()
        - UpdatableRecord.refresh()

       Synthetic primary keys will override existing primary keys. -->
  <syntheticPrimaryKeys>SCHEMA\.TABLE\.COLUMN(1|2)</syntheticPrimaryKeys>

<!-- All (UNIQUE) key names that should be used instead of primary keys on
       generated UpdatableRecords, to be used with

        - UpdatableRecord.store()
        - UpdatableRecord.update()
        - UpdatableRecord.delete()
        - UpdatableRecord.refresh()

        If several keys match, a warning is emitted and the first one encountered will be used.

        This flag will also replace synthetic primary keys, if it matches. -->
  <overridePrimaryKeys>MY_UNIQUE_KEY_NAME</overridePrimaryKeys>

这是 jOOQ 代码生成器中的错误:
https://github.com/jOOQ/jOOQ/issues/5103

它只出现在 PostgreSQL 中,当为具有复合类型数组的表生成 POJO 时。我目前没有看到解决方法。