jOOQ子查询使用DEFAULT方言而非配置的INFORMIX方言问题求助
jOOQ 3.15.3子查询中BindingSQLContext方言为DEFAULT的问题解决
问题背景
使用jOOQ 3.15.3编写了包含子查询的UPDATE语句,代码如下:
myDslContext.update(TICKET) .set(TICKET.TICKET_STATUS, BillingStatus.POSTED) .set(TICKET.INVOICE, myDslContext.select(AR_INVOICE.INVOICE) .from(AR_INVOICE) .where(AR_INVOICE.CUSTID.eq(TICKET.CUSTID)) .and(AR_INVOICE.STATUS.eq(BillingStatus.PRINTED))) .where(TICKET.INVOICE.isNull()) .and(TICKET.TICKET_STATUS.eq(BillingStatus.PRINTED)) .andExists(myDslContext.select(AR_INVOICE.INVOICE) .from(AR_INVOICE) .where(AR_INVOICE.CUSTID.eq(TICKET.CUSTID)) .and(AR_INVOICE.STATUS.eq(BillingStatus.PRINTED))) .execute();
其中BillingStatus枚举使用了自定义Binding,实现时会检查SQL方言,仅支持INFORMIX:
@Override public void sql(BindingSQLContext<T> ctx) throws SQLException { Short converted = converter().to(ctx.value()); if (ctx.family() == SQLDialect.INFORMIX) { if (ctx.render().paramType() == ParamType.INLINED) { if (converted == null) { ctx.render().sql("NULL"); } else { ctx.render().sql(String.valueOf(converted)); } } else { ctx.render().sql(ctx.variable()); } } else { throw new SQLFeatureNotSupportedException(); } }
执行时发现子查询中的BindingSQLContext方言为DEFAULT而非预期的INFORMIX,使用静态DSL.select()也存在同样问题。目前因Spring Boot 2.x限制无法升级jOOQ版本,需确认这是写法错误还是bug,若为bug则寻求可行的临时解决办法。
补充:集成测试失败堆栈信息
org.jooq.exception.DataAccessException: Error while generating SQL for Binding at org.jooq.impl.Val.accept(Val.java:185) at org.jooq.impl.DefaultRenderContext.visit0(DefaultRenderContext.java:720) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:296) at org.jooq.impl.AbstractContext.lambda$visit$2(AbstractContext.java:832) at org.jooq.impl.AbstractContext.toggle(AbstractContext.java:333) at org.jooq.impl.AbstractContext.paramType(AbstractContext.java:845) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:832) at org.jooq.impl.CompareCondition.accept0(CompareCondition.java:214) at org.jooq.impl.CompareCondition.accept(CompareCondition.java:159) at org.jooq.impl.DefaultRenderContext.visit0(DefaultRenderContext.java:720) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:296) at org.jooq.impl.CombinedCondition.accept(CombinedCondition.java:231) at org.jooq.impl.DefaultRenderContext.visit0(DefaultRenderContext.java:720) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:296) at org.jooq.impl.ConditionProviderImpl.accept(ConditionProviderImpl.java:127) at org.jooq.impl.DefaultRenderContext.visit0(DefaultRenderContext.java:720) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:296) at org.jooq.impl.SelectQueryImpl.toSQLReference0(SelectQueryImpl.java:2349) at org.jooq.impl.SelectQueryImpl.lambda$toSQLReferenceLimitDefault$21(SelectQueryImpl.java:1865) at org.jooq.impl.AbstractContext.toggle(AbstractContext.java:333) at org.jooq.impl.AbstractContext.data(AbstractContext.java:344) at org.jooq.impl.SelectQueryImpl.toSQLReferenceLimitDefault(SelectQueryImpl.java:1865) at org.jooq.impl.SelectQueryImpl.accept0(SelectQueryImpl.java:1779) at org.jooq.impl.SelectQueryImpl.accept(SelectQueryImpl.java:1435) at org.jooq.impl.DefaultRenderContext.visit0(DefaultRenderContext.java:720) at org.jooq.impl.AbstractContext.visit(AbstractContext.java:296) at org.jooq.impl.DefaultDSLContext.render(DefaultDSLContext.java:709) at org.jooq.impl.Tools.hash(Tools.java:3256) at org.jooq.impl.Tools.autoAlias(Tools.java:3233) at org.jooq.impl.SelectQueryImpl.asTable(SelectQueryImpl.java:668) at org.jooq.impl.SelectQueryImpl.fieldsRow(SelectQueryImpl.java:661) at org.jooq.impl.FieldsTrait.fields(FieldsTrait.java:57) at org.jooq.impl.Tools.dataTypes(Tools.java:3439) at org.jooq.impl.Tools.scalarType(Tools.java:3443) at org.jooq.impl.SelectQueryImpl.asField(SelectQueryImpl.java:646) at org.jooq.impl.SelectImpl.asField(SelectImpl.java:2921) at org.jooq.impl.UpdateImpl.set(UpdateImpl.java:154) at org.jooq.impl.UpdateImpl.set(UpdateImpl.java:122) at com.mycompany.api.service.impl.CreateInvoiceRecordsHelperServiceImpl.postInvoices(CreateInvoiceRecordsHelperServiceImpl.java:61) at com.mycompany.api.service.impl.CreateInvoiceRecordsHelperServiceImpl.createInvoiceRecords(CreateInvoiceRecordsHelperServiceImpl.java:54) at com.mycompany.api.service.impl.CreateInvoiceRecordsHelperServiceImpl$$FastClassBySpringCGLIB$$73261fc1.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) at com.mycompany.api.service.impl.CreateInvoiceRecordsHelperServiceImpl$$EnhancerBySpringCGLIB$$378b940a.createInvoiceRecords(<generated>) at com.mycompany.api.service.impl.ArInvoiceServiceImpl.createInvoiceRecords(ArInvoiceServiceImpl.java:137) at com.mycompany.api.service.impl.ArInvoiceServiceImplIT.testCreateInvoiceRecords(ArInvoiceServiceImplIT.java:185) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55) Caused by: java.sql.SQLFeatureNotSupportedException at com.mycompany.jooq.binding.AbstractSmallintEnumBinding.sql(AbstractSmallintEnumBinding.java:42) at com.mycompany.jooq.binding.BillingStatusBinding.sql(BillingStatusBinding.java:7) at org.jooq.impl.Val.accept(Val.java:182) ... 122 more
问题分析与解决
这是bug还是写法错误?
这是jOOQ 3.15.x版本的已知bug:在子查询或嵌套查询的SQL渲染阶段,上下文的SQL方言没有正确传递给Binding,导致ctx.family()返回DEFAULT而非实际配置的INFORMIX。该问题在后续jOOQ版本中已修复,但受限于Spring Boot 2.x无法升级时,可通过以下临时方案绕过。
可行的临时解决办法
方案1:修改Binding的方言检查逻辑
由于主查询的方言是正确的,子查询场景下的DEFAULT属于错误传递的异常情况,可放宽Binding的检查条件,允许DEFAULT作为合法情况:
@Override public void sql(BindingSQLContext<T> ctx) throws SQLException { Short converted = converter().to(ctx.value()); SQLDialect family = ctx.family(); // 允许DEFAULT作为合法方言,适配子查询场景下的错误传递 if (family == SQLDialect.INFORMIX || family == SQLDialect.DEFAULT) { if (ctx.render().paramType() == ParamType.INLINED) { if (converted == null) { ctx.render().sql("NULL"); } else { ctx.render().sql(String.valueOf(converted)); } } else { ctx.render().sql(ctx.variable()); } } else { throw new SQLFeatureNotSupportedException(); } }
该方案
相关产品推荐
相关产品推荐

