问题描述
我在PostgreSQL数据库中定义了一个类型 T
和一个视图 V
。
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);
在版本3.7 上使用代码生成我得到了一个UDT记录类 MyTypeRecord
和一个表记录类 MyViewRecord
和UDT POJO类 MyType
和表POJO类 MyView
。
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
.
MyView
生成的类有一个 MyTypeRecord
的数组。
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;
}
在POJO中我会期待一系列POJO,例如:
while in a POJO I would expect an array of POJOs, e.g.:
private final MyType[] my_view_types;
另一个有趣的事实是pojo和该类型的记录在 udt 文件夹,而对于视图,它们位于表
文件夹中:这可能有助于找到解决方案/解释。
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.
根据请求,我附上了一个工作示例,按照我的描述生成记录和POJO。它与FileDropper在链接中共享。
Upon request, I attached a working example that generates the records and POJOs as I described. It is shared with FileDropper at this link.
我还报告了一个可能的伎俩,以避免这个问题,如果你真的很绝望,可以使用它。正如在此stackoverflow问题中,即使我们分配了POJO而不是记录,jOOQ也无法自动将记录数组转换为记录类 MyTypeRecord
。因此,您可以使用函数 array_to_json
将 ROW
的数组解析为json。在我的例子中将是:
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 ROW
s 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);
如果您注册绑定。
推荐答案
这是jOOQ代码生成器中的一个错误:
This is a bug in the jOOQ code generator:
https://github.com/jOOQ/jOOQ/issues/5103
它只出现在PostgreSQL中,为具有复合类型数组的表生成POJO时我目前没有看到解决方法。
It appears only in PostgreSQL, when generating POJOs for tables with composite type arrays. I currently don't see a workaround.
这篇关于jOOQ和autogeneration,如何避免表POJO中的UDT记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!