【问题标题】:Error trying to implement EnumConverter in JOOQ with Maven尝试使用 Maven 在 JOOQ 中实现 EnumConverter 时出错
【发布时间】:2020-01-04 16:34:42
【问题描述】:

我无法找到完整的示例,因此我认为我在某处遗漏了一部分。我收到一条映射错误消息,特别是“将记录映射到类时发生错误...”。

我的enum

public enum CustomType {
    CustomType(1, "some text"),
    CustomType(2, "another");

    private int id;
    private String value;

    private CustomType(int id, String value) {
        this.id = id;
        this.value = value;
    }

    public String toString() {
        return name;
    }

    public int getValue() {
        return value;
    }
}

我的枚举转换器:

public class CustomTypeConverter extends EnumConverter<Integer, CustomType>
{
    public CustomTypeConverter() {
        super(Integer.class, CustomType.class);
    }
}

我的POJO 是:

public class MyPojo {
    private CustomType customType;

    public MyPojo(CustomType customType) {
        this.customType = customType;
    }

    // setter/getter
}

据我了解,转换器链接可以在配置文件中、在 Maven 中和以编程方式完成。我宁愿在 Maven 中做。

我的pom.xml

...
<plugins>
    <plugin>
       <groupId>org.jooq</groupId>
       <artifactId>jooq-codegen-maven</artifactId>
        <executions>
           <execution>
               <phase>generate-sources</phase>
               <goals>
                   <goal>generate</goal>
               </goals>
           </execution>
       </executions>
        <configuration>
           <jdbc>
               <url>${db.url}</url>
               <user>${db.username}</user>
           </jdbc>
            <generator>
                <database>
                    <includes>.*</includes>
                    <inputSchema>mySchema</inputSchema>
                    <customTypes>
                        <customType>
                            <name>com.myPackage.enum.CustomType</name>
                            <converter>com.myPackage.converters.CustomTypeConverter</converter>
                        </customType>
                    </customTypes>
                    <forcedTypes>
                        <forcedType>
                            <name>com.myPackage.data.MyPojo</name>
                            <expression>.*\custom_type</expression>
                            <types>.*</types>
                        </forcedType>
                    </forcedTypes>
                </database>
                <target>
                  <packageName>com.myPackage</packageName>
                  <directory>target/generated-sources/jooq</directory>
                </target>
            </generator>
       </configuration>
    </plugin>
</plugins>
...

如果这有助于数据库列的数据库更改日志是:

<column name="custom_type" type="INT">

而选择码是:

public List<MyPojo> getPojos(long id) {
    return dslContext
            .selectFrom(MY_POJO)
            .where(MY_POJO.ID.eq(id))
            .fetchInto(MyPojo.class);
}

【问题讨论】:

    标签: java maven enums type-conversion jooq


    【解决方案1】:

    这里有几个可能的问题:

    您的枚举转换器不正确

    jOOQ内置的EnumConverter只能在“序数”枚举或“名称”枚举之间进行转换,通常对应Enum::ordinal,名称对应Enum::name。在您的示例中,您在枚举中添加了类似“标签”的内容:

    CustomTypeA(1, "some text"),
    CustomTypeB(2, "another");
    

    我假设您的实际枚举不是都命名为 CustomType,这在 Java 中是不可能的,所以我添加了 AB 后缀。有了这些枚举,jOOQ 的EnumConverter 现在可以映射0 &lt;-&gt; CustomTypeA1 &lt;-&gt; CustomTypeB,或者'CustomTypeA' &lt;-&gt; CustomTypeA'CustomTypeB' &lt;-&gt; CustomTypeB

    我猜您可能希望 jOOQ 将您的数据库值映射到您的 idvalue 字符串,但是 jOOQ 确实没有任何自动方法可以发现这就是您的意思。

    您的代码生成器配置不正确

    您的 MyPojo 类不是您要应用于特定列的类,它是一种用于包装记录的包装器类型,而不是单独的价值观

    您可能想要配置的是:

    <forcedType>
        <userType>com.myPackage.enums.CustomType</userType>
        <converter>com.myPackage.converters.CustomTypeConverter</converter>
        <expression>.*\custom_type</expression>
        <types>.*</types>
    </forcedType>
    

    您正在选择所有列,并尝试将它们映射到 POJO

    最后,您的 POJO 没有默认构造函数,因此 jOOQ 将其视为不可变的 POJO。这意味着 jOOQ 将按照投影顺序(您的 SELECT 子句)将列映射到构造函数参数。您的构造函数只有一个参数,但您要从表中选择所有列。很可能,源列和目标列之间也存在不匹配。

    【讨论】:

      猜你喜欢
      • 2012-02-05
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2012-06-13
      • 2014-06-04
      • 2014-10-15
      • 1970-01-01
      • 2020-07-21
      相关资源
      最近更新 更多