代码之家  ›  专栏  ›  技术社区  ›  Stephane Grenier

尝试使用Maven在JOOQ中实现EnumConverter时出错

  •  0
  • Stephane Grenier  · 技术社区  · 6 年前

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

    我的 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);
    }
    
    0 回复  |  直到 6 年前
        1
  •  2
  •   Lukas Eder    6 年前

    这里有几个可能的问题:

    枚举转换器不正确

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

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

    我假设你的实际枚举不是都命名的 CustomType ,这在Java中是不可能的,所以我添加了 A B 后缀。有了这些枚举,jOOQ的 枚举转换器 现在可以映射 0 <-> CustomTypeA 1 <-> CustomTypeB 'CustomTypeA' <-> CustomTypeA 'CustomTypeB' <-> CustomTypeB

    我想您应该希望jOOQ将您的数据库值映射到 id value 字符串,但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 子句)转换为构造函数参数。构造函数只有一个参数,但您正在从表中选择所有列。很可能,源列和目标列之间也存在不匹配。