Today, I took another closer look at the problem I had already reported in
https://github.com/jOOQ/jOOQ/issues/18793.
The issue is, that when using a UDT in Postgresql, the jOOQ code generator creates record classes with compile errors.
Our workaround so far has been to correct the incorrectly generated record classes using Linux CLI sed. However, this now requires adjustments to the Docker image for the CI pipeline and much more, so today I took the trouble to build an MVE with jbang so that it can be reproduced quickly. (see attachment jbang-udt.zip)
Here are the steps to reproduce:
1) We create a UDT in PostgreSQL for a monetary amount consisting of amount and currency, using that UDT for a column within the
invoice table and insert a value.
create type monetary_value as (
amount numeric(14,2),
currency varchar(4)
);
create table invoice (
id int not null primary key generated by default as identity,
amount monetary_value
);
insert into invoice (amount) values ('(42,EUR)');
2) The jOOQ code generator is now configured and started for this database:
<generator>
<database>
<inputSchema>public</inputSchema>
<includeUDTs>true</includeUDTs>
<forcedTypes>
<forcedType>
<userType>javax.money.MonetaryAmount</userType>
<converter>de.todo42.test.MonetaryValueConverter</converter>
<includeExpression>amount</includeExpression>
<objectType>COLUMN</objectType>
</forcedType>
</forcedTypes>
</database>
<generate>
<pojos>true</pojos>
</generate>
<target>
<packageName>de.todo42.test</packageName>
<directory>.</directory>
</target>
</generator>3) The code generator now generates, among other things, the InvoiceRecord.java for the INVOICE table.
This class has the following as its last method:
/**
* Create a detached, initialised InvoiceRecord
*/
public InvoiceRecord(de.todo42.test.tables.pojos.Invoice value) {
super(Invoice.INVOICE);
if (value != null) {
setId(value.getId());
setAmount(value.getAmount() == null ? null : new MonetaryAmount(value.getAmount()));
resetTouchedOnNotNull();
}
}
There is now a compile error here because the used constructor
new MonetaryAmount(value.getAmount())); does not exist.
4) As can be seen above, POJO generation was enabled. These are essential for our project. However, if this is deactivated in the configuration by
<pojos>false</pojos>then this incorrect method is NOT generated and no compile errors occur.
Our workaround now consists of using a sed call to replace the line
setAmount(value.getAmount() == null ? null : new MonetaryAmount(value.getAmount()));
with the line
setAmount(value.getAmount()); This also corresponds to the generated assignments for all other table columns/class attributes that are not UDTs.
However, the generation of POJOs is essential for us and we cannot do without it.
How can I generate record classes without compile errors, even with POJO generation enabled ?
Kind regards
Dominik