Skip to content
Advertisement

JOOQ Forced types to convert a BigInteger to BigDecimal for POSTGRES

for a Table , lets say myTable, I have a column myColumn..

the default JOOQ generation creates myColumn as BigInteger,

I want to create it as BigDecimal.

This is the converter I am using.

public class myConverter extends AbstractConverter<BigInteger, BigDecimal> {

    public BigIntToBigDecConverter(Class<BigInteger> fromType, Class<BigDecimal> toType) {

        super(fromType, toType);
    }

    @Override
    public BigDecimal from(BigInteger databaseObject) {
        return new BigDecimal(databaseObject);
    }

    @Override
    public BigInteger to(BigDecimal userObject) {
        return new BigInteger(String.valueOf(userObject.intValue()));
    }
}

How should be the forceType configuration look like in the XML file?

Advertisement

Answer

The reason why your columns are generated as BigInteger is because they’re of type NUMERIC(n, 0), NUMBER(n, 0), or DECIMAL(n, 0) depending on the dialect you’re using, but the key here is that the scale is 0, and n is large enough that it doesn’t fit in a Long anymore.

The reason why your <forcedType/> configuration hasn’t worked is because the type you’ve listed in <types/> is the Java type BigInteger, not the SQLDataType, see documentation here. In order to rewrite all zero scale decimals to produce BigDecimal, just write this:

<forcedType>
  <name>NUMERIC</name>
  <inputTypes>(?i:(NUMERIC|NUMBER|DECIMAL)(d+,0))</inputTypes>
</forcedType>

You won’t need a custome converter for that

5 People found this is helpful
Advertisement