Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用Maven在JOOQ中实现EnumConverter时出错_Java_Maven_Enums_Type Conversion_Jooq - Fatal编程技术网

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

Java 尝试使用Maven在JOOQ中实现EnumConverter时出错,java,maven,enums,type-conversion,jooq,Java,Maven,Enums,Type Conversion,Jooq,我还没有找到一个完整的例子,因此我假设我在某个地方遗漏了一个部分。我收到一条映射错误消息,特别是“将记录映射到类时出错…” 我的枚举: public enum CustomType { CustomType(1, "some text"), CustomType(2, "another"); private int id; private String value; private CustomType(int id, String value) {

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

我的
枚举

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);
    }
}
据我所知,转换器链接可以在配置文件、maven和编程中完成。我更喜欢在maven做这件事

My
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>
...
。。。
org.jooq
jooq codegen maven
生成源
生成
${db.url}
${db.username}
.*
迈斯切玛
com.myPackage.enum.CustomType
com.myPackage.converters.CustomTypeConverter
com.myPackage.data.MyPojo
.*\自定义\u类型
.*
com.myPackage
目标/生成源/jooq
...
在这种情况下,数据库列的数据库更改日志为:

<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);
}
公共列表getPojos(长id){
返回上下文
.从(MY_POJO)中选择
.where(MY_POJO.ID.eq(ID))
.fetchInto(MyPojo.class);
}

这里有几个可能的问题:

您的枚举转换器不正确 jOOQ的内置只能在“序号”枚举或“名称”枚举之间转换,序号对应于和名称对应于。在您的示例中,向枚举添加了类似“标签”的内容:

CustomTypeA(1,“一些文本”),
客户类型B(2,“另一”);
我假设您的实际枚举不是同时命名为
CustomType
,这在Java中是不可能的,所以我添加了
A
B
后缀。通过这些枚举,jOOQ现在可以映射
0 CustomTypeA
1 CustomTypeB
,或者
'CustomTypeA'CustomTypeA
'CustomTypeB'CustomTypeB

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

代码生成器配置不正确 您的
MyPojo
类不是要应用于特定列的类,它是用于包装记录的包装类型,而不是单个值

您可能想要配置的是:


com.myPackage.enums.CustomType
com.myPackage.converters.CustomTypeConverter
.*\自定义\u类型
.*
您正在选择所有列,并尝试将它们映射到POJO 最后,您的POJO没有默认构造函数,因此jOOQ将其视为不可变的POJO。这意味着jOOQ将按投影顺序(您的
SELECT
子句)将列映射到构造函数参数上。您的构造函数只有一个参数,但您正在从表中选择所有列。很可能,源列和目标列之间也存在不匹配

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