Search code examples
javaspring-bootenumsconvertersjooq

How to solve error "Constructor cannot be applied to the specified types", when using jooq custom EnumConverter


I created a custom EnumConverter so that instead of the Enum ordinal the customized value typeId is saved in the database:

public class CustomAntragTypEnumConverter extends EnumConverter<Integer, AntragTyp> {
  public CustomAntragTypEnumConverter(Class fromType, Class toType) {
    super(fromType, toType, type -> type.getTypeId());
  }

The Enum AntragTyp looks like this:


    STAMMDATENAENDERUNG("Stammdatenmitteilung", 1),
    PFLEGEANTRAG("Pflegeantrag", 2),
    BEIHILFEANTRAG("Beihilfeantrag", 3),
    APPANTRAG("App-Einreichung", 4);


    private String title;
    private Integer typeId;

    private AntragTyp(String title, Integer typeId) {
        this.title = title;
        this.typeId = typeId;
    }

    /**
     * Getter für den Title
     *
     * @return String mit title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Getter für den Type
     *
     * @return Integer mit der typeId
     */
    public Integer getTypeId() {
        return typeId;

The configuration in pom looks like this:

<forcedType>
  <userType>de.xxxx.xxxx.enums.AntragTyp</userType>
  <converter>de.xxxx.xxxx.enums.CustomAntragTypEnumConverter</converter>
  <includeExpression>.*\.ANTRAG\.TYP</includeExpression>
  <types>.*</types>
</forcedType>

However when I build the project with maven, I get the following error:

"Constructor CustomAntragTypEnumConverter in class de.xxxx.xxxx.enums.CustomAntragTypEnumConverter cannot be applied to the specified types."

And in the jooq generated Antrag table I get the error that CustomAntragTypEnumConverter is missing two parameters which explains the error above:

public final TableField<AntragRecord, AntragTyp> TYP = createField(DSL.name("TYP"), SQLDataType.INTEGER.defaultValue(DSL.field(DSL.raw("NULL"), SQLDataType.INTEGER)), this, "", new CustomAntragTypEnumConverter());

I would have expected new CustomAntragTypEnumConverter(<Integer, AntragTyp>(Integer.class, AntragTyp.class))

What am I missing? How can I solve this error?

The project is using jooq 3.18.14, java 17, spring-boot 3.1.11

I implemented the custom EnumConverter as described in https://stackoverflow.com/questions/71066313/jooq-enum-converter-uses-the-ordinal-number-how-can-i-switch-to-use-the-enum-va

I also adjusted the configuration in the project's pom as described in https://stackoverflow.com/questions/57743387/error-trying-to-implement-enumconverter-in-jooq-with-maven


Solution

  • Just replace this:

      public CustomAntragTypEnumConverter(Class fromType, Class toType) {
        super(fromType, toType, type -> type.getTypeId());
      }
    

    By this:

      public CustomAntragTypEnumConverter() {
        super(Integer.class, AntragTyp.class, type -> type.getTypeId());
      }