[英]JOOQ choose field Converter by another field
I want to convert between Set of Enum POJO and String[] Database(postgres) column.我想在 Set of Enum POJO 和 String[] Database(postgres) 列之间进行转换。 and the enum class would be changed by another field
type
.并且枚举 class 将被另一个字段
type
更改。 So I can say Enum class which's using in fooSet
is changable and it's up to field type
.所以我可以说在 fooSet 中使用的 Enum
fooSet
是可变的,它取决于字段type
。
I know it's a messy.我知道这很乱。 but I need a help.
但我需要帮助。
Below are models下面是模型
public interface A {
enum B implements A {
step1,
step2,
step3
}
enum C implements A {
step4,
step5,
step6
}
}
public abstract class Foo {
private String type;
}
public abstract class FooA {
private Set<B> fooSet;
}
public abstract class FooB {
private Set<C> fooSet;
}
I want to make a Converter like below.我想制作一个如下所示的转换器。
SetOfEnumConverter<U extends Enum<U> & A> implements Converter<String[], Set<U>> {
@Override
public Set<U> from(String[] databaseObject) {
if (databaseObject == null) {
return null;
}
return Arrays.stream(databaseObject)
.map(x -> U.valueOf(U.class, x)). // here's the problem point
.collect(Collectors.toSet());
}
@Override
public String[] to(Set<U> userObject) {
if (userObject == null || userObject.isEmpty()) {
return null;
}
String[] strings = userObject.stream()
.map(Enum::name)
.toArray(String[]::new);
return ArrayUtils.isEmpty(strings) ? new String[0]: strings;
}
@Override
public Class<String[]> fromType() {
return String[].class;
}
@Override
public Class<Set<U>> toType() {
return (Class) TreeSet.class;
}
}
But the problem is I can't point .class
attribute from a generic type maybe because of Generic Type erasure.但问题是我无法从通用类型指向
.class
属性,这可能是因为通用类型擦除。
So, What I want to do is mapping the setEnum class to be used in the field fooSet according to the field type .所以,我想做的是根据字段类型映射setEnum class 以在字段fooSet中使用。 Because I have to make a single table for Foo and map from FooA, FooB and FooZ.
因为我必须从 FooA、FooB 和 FooZ 为 Foo 和 map创建一个表。
You can't do this without passing an actual Class<U>
reference to your converter, eg like this:如果不将实际的
Class<U>
引用传递给您的转换器,您将无法执行此操作,例如:
SetOfEnumConverter<U extends Enum<U> & A> implements Converter<String[], Set<U>> {
final Class<U> u;
SetOfEnumConverter(Class<U> u) {
this.u = u;
}
// ...
}
And inside of the converter, you can use:在转换器内部,您可以使用:
Enum.valueOf(u, x)
To look up arbitrary enum values by their names.通过名称查找任意枚举值。 Then, instantiate it with eg
然后,用例如实例化它
new SetOfEnumConverter<>(MyEnum.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.