You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决JOOQ自定义EnumConverter构造器参数不匹配错误

JOOQ自定义枚举转换器构造器参数不匹配问题解决

问题背景

我实现了CustomAntragTypEnumConverter,用于将枚举AntragTyp的typeId而非默认ordinal存入数据库,但Maven构建时触发错误:

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

生成的Antrag表代码中调用了无参构造new CustomAntragTypEnumConverter(),但我的转换器仅定义了带Class参数的构造器。项目环境:JOOQ 3.18.14、Java 17、Spring Boot 3.1.11。

相关代码

自定义枚举转换器

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

枚举AntragTyp定义

public enum AntragTyp {
    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;
    }

    public String getTitle() {
        return title;
    }

    public Integer getTypeId() {
        return typeId;
    }
}

POM中JOOQ forcedType配置

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

解决方案

1. 添加无参构造器供JOOQ调用

JOOQ生成代码时默认会调用转换器的无参构造,因此必须在CustomAntragTypEnumConverter中显式添加无参构造,并完整指定双向转换逻辑:

public class CustomAntragTypEnumConverter extends EnumConverter<Integer, AntragTyp> {
    // 无参构造,JOOQ生成代码时自动调用
    public CustomAntragTypEnumConverter() {
        super(Integer.class, AntragTyp.class, 
              AntragTyp::getTypeId,       // 枚举转数据库存储的Integer
              AntragTyp::fromTypeId);     // 数据库Integer转回枚举
    }

    // 保留原带参构造(若有其他场景需要)
    public CustomAntragTypEnumConverter(Class fromType, Class toType) {
        super(fromType, toType, type -> type.getTypeId());
    }
}

2. 给枚举添加反向查找方法

JOOQ需要从数据库的typeId值反向转换为枚举实例,因此必须在AntragTyp中添加静态查找方法:

public enum AntragTyp {
    // 原有枚举实例及属性不变...

    // 根据typeId查找对应枚举
    public static AntragTyp fromTypeId(Integer typeId) {
        for (AntragTyp typ : values()) {
            if (typ.typeId.equals(typeId)) {
                return typ;
            }
        }
        throw new IllegalArgumentException("无效的typeId: " + typeId);
    }
}

3. 优化forcedType配置(可选)

在POM的forcedType中明确指定数据库类型与枚举类型的映射,让JOOQ更清晰识别转换规则:

<forcedType>
  <userType>de.xxxx.xxxx.enums.AntragTyp</userType>
  <converter>de.xxxx.xxxx.enums.CustomAntragTypEnumConverter</converter>
  <fromType>INTEGER</fromType> <!-- 数据库字段类型 -->
  <toType>de.xxxx.xxxx.enums.AntragTyp</toType>
  <includeExpression>.*\.ANTRAG\.TYP</includeExpression>
  <types>.*</types>
</forcedType>

内容的提问来源于stack exchange,提问作者Isa dBC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 22:22:06