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

左心房为你撑大大i 提交于 2019-12-20 11:31:28

问题


I define a type T and a view V in a PostgreSQL database.

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);

Using the code generation on release 3.7 I get both an UDT record class MyTypeRecord and a table record class MyViewRecord and the UDT POJO class MyType and table POJO class MyView.

The MyView generated class has an array of 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;
}

while in a POJO I would expect an array of POJOs, e.g.:

    private final MyType[] my_view_types;

Another interesting fact is that the pojo and the record for the type are in the udt folder, while for the view they are in the tables folder: maybe this can help to find a solution/explanation.

Is there a way to make the View a pojo-only conversion at generation time?


Upon request, I attached a working example that generates the records and POJOs as I described. It is shared with FileDropper at this link.


I also report one possible trick to avoid this issue, to be used iff you are really desperate. As reported in this stackoverflow question/answer, jOOQ even if we assign a POJO instead of the record, will not be able to automatically convert the array of records into the record class MyTypeRecord. Hence, you can parse the array of ROWs to json using function array_to_json. In my example would be:

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 should be converted automatically by jOOQ to a JSON if you register this binding.


回答1:


This is a bug in the jOOQ code generator:
https://github.com/jOOQ/jOOQ/issues/5103

It appears only in PostgreSQL, when generating POJOs for tables with composite type arrays. I currently don't see a workaround.




回答2:


The reason it is doing what it is doing is because a View does not have a PrimaryKey associated with it, at least not with most databases, I can't think of a single one that would report back a PrimaryKey for a view.

You can specify the primary key to the generate using either the <syntheticPrimaryKeys> or you can use <overridePrimaryKeys> as described in the advanced generator configuration section of the manual.

Relevant parts of 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>


来源:https://stackoverflow.com/questions/35403931/jooq-and-autogeneration-how-to-avoid-udt-records-inside-table-pojos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!