有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

尝试用Maven在JOOQ中实现EnumConverter时发生java错误

我还没有找到一个完整的例子,因此我假设我在某个地方遗漏了一个部分。我收到一条映射错误消息,特别是“将记录映射到类时出错…”

我的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);
}

共 (1) 个答案

  1. # 1 楼答案

    这里有几个可能的问题:

    您的枚举转换器不正确

    jOOQ的内置^{}只能在“序号”之间转换<-&燃气轮机;枚举或“名称”<-&燃气轮机;枚举,依次对应于^{},名称对应于^{}。在您的示例中,向枚举添加了类似“标签”的内容:

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

    我假设您的实际枚举不是同时命名为CustomType,这在Java中是不可能的,所以我添加了AB后缀。有了这些枚举,jOOQ的^{}现在可以映射0 <-> CustomTypeA1 <-> CustomTypeB,或者'CustomTypeA' <-> CustomTypeA'CustomTypeB' <-> 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子句)将列映射到构造函数参数上。您的构造函数只有一个参数,但您正在从表中选择所有列。很可能,源列和目标列之间也存在不匹配