gpt4 book ai didi

java - Jooq 数组作为集合

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:29 25 4
gpt4 key购买 nike

我想在 java 中使用集合而不是数组来序列化 postgreSQL 数组。如int[]、varchar(256)[]到java Collection和Collection。

我使用 jooq 3.6.2、java 8 和 postgresql 9.2.9。

我尝试实现自定义绑定(bind):

public class PostgresArrayBinding implements Binding<Object, Collection> {

@Override
public Converter<Object, Collection> converter() {
return new Converter<Object, Collection>() {
@Override
public Collection from(final Object dbObj) {
return dbObj == null ? null : new ArrayList<>(Arrays.asList((Object[])dbObj));
}

@Override
public Object to(final Collection userObject) {
return userObject == null ? null : "ARRAY[" + Joiner.on(',').join(userObject) + "]";
}

@Override
public Class<Object> fromType() {
return Object.class;
}

@Override
public Class<Collection> toType() {
return Collection.class;
}
};
}

@Override
public void sql(final BindingSQLContext<Collection> ctx) throws SQLException {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::ARRAY");
}

@Override
public void register(final BindingRegisterContext<Collection> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.ARRAY);
}

@Override
public void set(final BindingSetStatementContext<Collection> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}

@Override
public void set(final BindingSetSQLOutputContext<Collection> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public void get(final BindingGetResultSetContext<Collection> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}

@Override
public void get(final BindingGetStatementContext<Collection> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}

@Override
public void get(final BindingGetSQLInputContext<Collection> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}

jooq-config.xml:

<configuration>
...
<generator>
<database>
<name>org.jooq.util.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes>schema_version</excludes>
<inputSchema>public</inputSchema>
<customTypes>
...
<customType>
<name>ARRAY</name>
<type>java.util.Collection</type>
<binding>ru.fabit.contingent.configuration.persistence.PostgresArrayBinding</binding>
</customType>
</customTypes>

<forcedTypes>
...
<forcedType>
<name>ARRAY</name>
<expression>.*_ARRAY</expression>
<types>.*</types>
</forcedType>
</forcedTypes>
</database>
<generate>
<records>true</records>
<interfaces>true</interfaces>
<relations>true</relations>
<validationAnnotations>true</validationAnnotations>
</generate>
<target>
<packageName>ru.fabit.contingent.models.generated</packageName>
<directory>src/main/java/</directory>
</target>
</generator>
</configuration>

SQL:

创建表 array_tests(string_array varchar(256)[]);

我在生成的类中有错误:

public final TableField<ArrayTestsRecord, Collection[]> STRING_ARRAY = createField("string_array", org.jooq.impl.DefaultDataType.getDefaultDataType("java.util.Collection").getArrayDataType(), this, "", new PostgresArrayBinding());

找不到适合 createField(String,DataType,ArrayTests,String,PostgresArrayBinding) 的方法

有什么想法吗?

最佳答案

这是由于代码生成器中的错误,jOOQ 3.8 已修复: https://github.com/jOOQ/jOOQ/issues/4388

恐怕 jOOQ 3.7 或更低版本没有解决方法,除非手动修补错误生成的代码。

关于java - Jooq 数组作为集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35476939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com