JOOQ在另一个字段中选择字段转换器
我想在枚举POJO和String []数据库(Postgres)列之间转换。 枚举类将通过另一个字段type
更改。 因此,我可以说在fooset
中使用的枚举类是可更改的,并且可以通过字段type
。
我知道这很混乱。但是我需要帮助。
以下是
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;
}
我想制作一个类似于下面的转换器的型号。
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;
}
}
但是问题是我不能指向.class
属性,可能是由于通用类型的擦除而属性。
因此,我想做的是根据 fooset setEnum 类 根据 type 。 因为我必须为Foo制作一个表,并从Fooa,Foob和Fooz制作映射。
I want to convert between Set of Enum POJO and String[] Database(postgres) column.
and the enum class would be changed by another field type
.
So I can say Enum class which's using in fooSet
is changable and it's up to field 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.
So, What I want to do is mapping the setEnum class to be used in the field fooSet according to the field type.
Because I have to make a single table for Foo and map from FooA, FooB and FooZ.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不传递实际
class&lt; u&gt;
对转换器的引用,例如,例如这样:在转换器的内部,您可以使用:您可以使用:您可以使用:
查找任意枚举值。然后,用例如
You can't do this without passing an actual
Class<U>
reference to your converter, e.g. like this:And inside of the converter, you can use:
To look up arbitrary enum values by their names. Then, instantiate it with e.g.