Search code examples
jooqjooq-codegen-maven

JOOQ forcedType unable to generate NUMERIC SQL data type via JPADatabase code gen


I am using JPADatabase to generate JOOQ classes from my existing JPA entites. From manual, I know that JPADatabase internally uses H2 database to read the meta data schema and generate the code. Due to H2 database has no data type for Numeric, instead it has Decimal. Therefore all my numeric data type are converted to SQLDataType.DECIMAL.

public final TableField<MyTableRecord, BigDecimal> NUM_FIELD = createField(DSL.name("NUM_FIELD"), SQLDataType.DECIMAL, this, "");

I even try to use forcedType to force generate the SQLDataType.NUMERIC when running the code gen, but the result still shows SQLDataType.DECIMAL. And I found it even works for Double, if I put SQLDataType.DOUBLE in the forcedType, but just not work for SQLDataType.NUMERIC.

<forcedTypes>
                    <forcedType>
                      <name>NUMERIC</name>
                      <includeExpression>.*\.num_field</includeExpression>
                      <includeTypes>.*</includeTypes>
                      <nullability>ALL</nullability>
                      <objectType>ALL</objectType>
                    </forcedType>
                  </forcedTypes>

Is there any way to make JPADatabase code gen to produce SQLDataType.NUMERIC?


Solution

  • In most dialects, the distinction between DECIMAL and NUMERIC shouldn't matter, especially when you generate client code from pre-existing DDL statements. The client logic to bind variables of types DECIMAL and NUMERIC is almost the same in jOOQ.

    The reason why your forced type was likely not applied is because the regular expressions are case sensitive. As your generated code shows, the column is called NUM_FIELD (in upper case). One way to solve this is by making your regex case insensitive:

    <includeExpression>(?i:.*\.num_field)</includeExpression>