Java 如何使用jOOQ-gradle插件将postgres中的bigint[]字段转换为class字段
大家好:)我和jOOQ有些问题。 我需要从PostgreSQL表生成jOOQ实体。它有一个类型为Java 如何使用jOOQ-gradle插件将postgres中的bigint[]字段转换为class字段,java,postgresql,gradle,jdbc,jooq,Java,Postgresql,Gradle,Jdbc,Jooq,大家好:)我和jOOQ有些问题。 我需要从PostgreSQL表生成jOOQ实体。它有一个类型为bigint[]的字段。jOOQ Gradle插件(我正在使用这个)不支持数组DateType,所以唯一的方法是使用自定义类型和强制类型 // jooq config in build.gradle customTypes { customType { name = "BigintArrayBinder" type = "Long[]" conv
bigint[]
的字段。jOOQ Gradle插件(我正在使用这个)不支持数组DateType,所以唯一的方法是使用自定义类型和强制类型
// jooq config in build.gradle
customTypes {
customType {
name = "BigintArrayBinder"
type = "Long[]"
converter = "ru.stdev.tskad.yandexnavigator.binder.BigintArrayBinder"
}
}
forcedTypes {
forcedType {
name = "BigintArrayBinder"
includeExpression = '.*'
includeTypes = 'ARRAY'
}
}
这是这个活页夹及其转换器的实现
公共类BiginArrayConverter实现转换器{
@凌驾
公共长[]自(对象[]t){
返回t==null?新的Long[]{}:(Long[])t;
}
@凌驾
公共对象[]到(长[]u){
返回u==null | | u.length==0?新对象[]{}:u;
}
@凌驾
公共类fromType(){
返回对象[]。类;
}
@凌驾
公共类toType(){
返回长[]类;
}
}
import org.jooq.*;
导入org.jooq.conf.ParamType;
导入org.jooq.impl.DSL;
导入org.postgresql.core.BaseConnection;
导入org.postgresql.jdbc.PgArray;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.ObjectOutputStream;
导入java.sql.DriverManager;
导入java.sql.SQLException;
导入java.sql.SQLFeatureNotSupportedException;
导入java.sql.Types;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类BigintArrayBinder实现绑定{
//转换器完成所有工作
@凌驾
公共转换器(){
返回新的BigintArrayConverter();
}
//为绑定上下文的值呈现绑定变量并将其强制转换为json类型
@凌驾
公共void sql(BindingSQLContext ctx)引发SQLException{
//根据生成SQL的方式,可能需要明确区分
//在jOOQ之间生成绑定变量或内联文本。
if(ctx.render().paramType()==paramType.INLINED)
ctx.render().visit(DSL.inline(ctx.convert(converter()).value()).sql(“::bigint[]);
其他的
ctx.render().sql(“::bigint[]”);
}
//为JDBC CallableStatement OUT参数注册数组类型
@凌驾
公共无效寄存器(BindingRegisterContext ctx)引发SQLException{
ctx.statement().registerOutParameter(ctx.index(),Types.ARRAY);
}
//将Long[]转换为bigint[]值并在JDBC PreparedStatement上设置该值
@凌驾
公共无效集(BindingSetStatementContext ctx)引发SQLException{
Object[]value=ctx.convert(converter()).value();
BaseConnection连接=(BaseConnection)DriverManager.getConnection(
“jdbc:postgresql://localhost:5432/m4",
“博士后”,
“博士后”
);
int longOid=20;
系统输出打印(“值->”;
System.out.println(Arrays.toString(value));
PgArray PgArray=新的PgArray(连接、longOid、Arrays.toString(值));
ctx.statement().setArray(ctx.index(),pgArray);
}
//从JDBC结果集获取bigint[]值并将其转换为Long[]
@凌驾
public void get(BindingGetResultSetContext ctx)引发SQLException{
对象[]数组=(对象[])ctx.resultSet().getArray(ctx.index()).getArray();
convert(converter()).value(数组);
}
//从JDBC CallableStatement获取bigint[]值并将其转换为Long[]
@凌驾
public void get(BindingGetStatementContext ctx)引发SQLException{
对象[]数组=(对象[])ctx.statement().getArray(ctx.index()).getArray();
convert(converter()).value(数组);
}
//在JDBC SQLOutput上设置值(对Oracle对象类型有用)
@凌驾
公共无效集(BindingSetSQLOutputContext ctx)引发SQLException{
抛出新的SQLFeatureNotSupportedException();
}
//从JDBCSQLInput获取值(对Oracle对象类型有用)
@凌驾
public void get(BindingGetSQLInputContext ctx)引发SQLException{
抛出新的SQLFeatureNotSupportedException();
}
}
因此,它的编译和工作良好的选择查询。但如果我尝试插入,我有一个例外
org.jooq.exception.DataAccessException: SQL [null]; Error while writing value at JDBC bind index: 7
at org.jooq_3.13.1.POSTGRES.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2751)
at org.jooq.impl.AbstractBindContext.bindValue(AbstractBindContext.java:127)
at org.jooq.impl.Val.accept(Val.java:103)
at org.jooq.impl.AbstractBindContext.bindInternal(AbstractBindContext.java:269)
at org.jooq.impl.AbstractBindContext.visit0(AbstractBindContext.java:88)
at org.jooq.impl.AbstractContext.visit0(AbstractContext.java:457)
at org.jooq.impl.AbstractContext.visit(AbstractContext.java:218)
at org.jooq.impl.QueryPartList.accept(QueryPartList.java:121)
at org.jooq.impl.AbstractBindContext.bindInternal(AbstractBindContext.java:269)
at org.jooq.impl.AbstractBindContext.visit0(AbstractBindContext.java:88)
at org.jooq.impl.AbstractContext.visit0(AbstractContext.java:457)
at org.jooq.impl.AbstractContext.visit(AbstractContext.java:218)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:367)
at org.jooq.impl.AbstractDelegatingQuery.execute(AbstractDelegatingQuery.java:119)
at ru.stdev.tskad.yandexnavigator.repository.NavigatorRepository$testInsert$2.invokeSuspend(NavigatorRepository.kt:37)
at ru.stdev.tskad.yandexnavigator.repository.NavigatorRepository$testInsert$2.invoke(NavigatorRepository.kt)
at ru.stdev.utp.common.jooq.DefaultJooqWrapper$launchOperation$1.invokeSuspend(DefaultJooqWrapper.kt:17)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:330)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:58)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at ru.stdev.utp.common.jooq.DefaultJooqWrapper.launchOperation(DefaultJooqWrapper.kt:16)
at ru.stdev.utp.common.jooq.DefaultJooqWrapper.request(DefaultJooqWrapper.kt:27)
at ru.stdev.tskad.yandexnavigator.repository.NavigatorRepository.testInsert(NavigatorRepository.kt:25)
at ru.stdev.tskad.yandexnavigator.routes.NavigatorRouteKt$navigator$1.invokeSuspend(NavigatorRoute.kt:23)
at ru.stdev.tskad.yandexnavigator.routes.NavigatorRouteKt$navigator$1.invoke(NavigatorRoute.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
at io.ktor.routing.Routing.executeResult(Routing.kt:147)
at io.ktor.routing.Routing.interceptor(Routing.kt:34)
at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:99)
at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:107)
at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.features.StatusPages$interceptCall$2.invokeSuspend(StatusPages.kt:101)
at io.ktor.features.StatusPages$interceptCall$2.invoke(StatusPages.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:194)
at io.ktor.features.StatusPages.interceptCall(StatusPages.kt:100)
at io.ktor.features.StatusPages$Feature$install$2.invokeSuspend(StatusPages.kt:140)
at io.ktor.features.StatusPages$Feature$install$2.invoke(StatusPages.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.features.CallLogging$Feature$install$2.invokeSuspend(CallLogging.kt:139)
at io.ktor.features.CallLogging$Feature$install$2.invoke(CallLogging.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invokeSuspend(DefaultEnginePipeline.kt:120)
at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invoke(DefaultEnginePipeline.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invokeSuspend(NettyApplicationCallHandler.kt:40)
at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invoke(NettyApplicationCallHandler.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:111)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at io.ktor.server.netty.NettyApplicationCallHandler.handleRequest(NettyApplicationCallHandler.kt:30)
at io.ktor.server.netty.NettyApplicationCallHandler.channelRead(NettyApplicationCallHandler.kt:24)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:59)
at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:368)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.sql.SQLException: Error while writing value at JDBC bind index: 7
at org.jooq.impl.DefaultBindContext.bindValue0(DefaultBindContext.java:67)
at org.jooq.impl.AbstractBindContext.bindValue(AbstractBindContext.java:124)
... 81 common frames omitted
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 63 out of bounds for length 63
at org.postgresql.jdbc.PgArray.buildArrayList(PgArray.java:450)
at org.postgresql.jdbc.PgArray.getBaseTypeName(PgArray.java:811)
at org.postgresql.jdbc.PgPreparedStatement.setArray(PgPreparedStatement.java:1098)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setArray(HikariProxyPreparedStatement.java)
at org.jooq.tools.jdbc.DefaultPreparedStatement.setArray(DefaultPreparedStatement.java:239)
at ru.stdev.tskad.yandexnavigator.binder.BigintArrayBinder.set(BigintArrayBinder.java:63)
at org.jooq.impl.DefaultBindContext.bindValue0(DefaultBindContext.java:62)
... 82 common frames omitted
让我困惑的第一件事是必须创建
BaseConnection
。我可以从DslContext获取连接,但它返回一个HikariProxyConnection
。因此,它不适用于PgArray
,它需要BaseConnection
。然后,我需要给它一个oid
,它等于长类型的20。然后我必须以字符串或字节数组的形式给出一个数组。因此,我有了数组索引OutofBoundsException
。我做了很多尝试来找出解决这个问题的办法,但都失败了。我做错了什么?我觉得我做错了什么,但我不明白到底是什么 你为什么要这样做
BaseConnection connection = (BaseConnection) DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/m4",
"postgres",
"postgres"
);
此时您应该已经建立了连接。由于您从未关闭此连接,因此此处可能存在资源泄漏
你也许可以
BaseConnection connection = (BaseConnection) ctx.statement().getConnection()
你应该试试
例如ctx.statement().getConnection().createArrayOf(…)
代码解决方案:
public类BigintArrayBinder实现绑定{
//转换器完成所有工作
@凌驾
公共转换器(){
返回新的BigintArrayConverter();
}
//为绑定上下文的值呈现绑定变量并将其强制转换为bigint数组类型
@凌驾
公共void sql(BindingSQLContext ctx)引发SQLException{
ctx.render().visit(DSL.inline(ctx.convert(converter()).value());
}
//为JDBC CallableStatement OUT参数注册数组类型
@凌驾
公共无效寄存器(BindingRegisterContext ctx)引发SQLException{
ctx.statement().registerOutParameter(ctx.index(),Types.ARRAY);
}
//将Long[]转换为bigint[]值并设置