【发布时间】: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