我有一个使用 DAO 的 java 应用程序,它包装NamedParameterJdbcTemplate
了数据库的表名和列名,这些表名和列名需要转义[]
才能使查询正常工作,这很好。但是,我的一个专栏有一个?作为列名的一部分。因此,如果我尝试运行以下查询:
INSERT INTO [Supervisory Description]([Supervisory?], Description)
VALUES ( :id, :description );
我得到以下异常,列在这个问题的底部。重要的部分是:
InvalidDataAccessApiUsageException: You can't mix named and traditional ? placeholders. You have 2 named parameter(s) and 1 traditonal placeholder(s)`
所以它理解:id
并且:description
很好。但由于某种原因,它认为[Supervisory?]
包含一个位置(传统)占位符,而不仅仅是一个列名。
所以当我打电话时,我getJddbcTemplate().batchUpdate(myInsertQuery, queryParams);
该如何表示?字符应该被忽略?
例外:
org.springframework.dao.InvalidDataAccessApiUsageException: You can't mix named and traditional ? placeholders. You have 2 named parameter(s) and 1 traditonal placeholder(s) in [INSERT INTO [Supervisory Description]([Supervisory?], Description) VALUES ( :id, :description )]
at org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray(NamedParameterUtils.java:316)
at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils$1.setValues(NamedParameterBatchUpdateUtils.java:45)
at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:899)
at org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:890)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
at org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:890)
at org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(NamedParameterBatchUpdateUtils.java:40)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(NamedParameterJdbcTemplate.java:324)
at us.ak.state.revenue.cssd.AccessImport.dao.BaseHibernateDAO.batchInsert(BaseHibernateDAO.java:163)
at WICKET_us.ak.state.revenue.cssd.AccessImport.dao.BaseHibernateDAO$$FastClassByCGLIB$$9a8099e8.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:333)
at WICKET_us.ak.state.revenue.cssd.AccessImport.dao.importers.SupervisoryDescriptionImporterDAO$$EnhancerByCGLIB$$36bf57cd.batchInsert(<generated>)
at us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter.typeProcessor(AccessExporter.java:88)
at us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter.process(AccessExporter.java:61)
at us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter$$FastClassByCGLIB$$9f5e6d3c.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter$$EnhancerByCGLIB$$2ad77846.process(<generated>)
at WICKET_us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter$$FastClassByCGLIB$$9f5e6d3c.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:333)
at WICKET_us.ak.state.revenue.cssd.AccessImport.dao.AccessExporter$$EnhancerByCGLIB$$5edf35b8.process(<generated>)
at us.ak.state.revenue.cssd.AccessImport.dao.AccessExporterTest.testProcess(AccessExporterTest.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
编辑:
感谢@tieTYT 提供的信息,我尝试添加{espace \\}
到我的字符串末尾并将我的列名更改为 [Superverisory\?] 在查询中。它没有用。原来问题是特定于 Spring 的。Spring *引发了异常NamedParameterUtils.buildValueArray
它试图将带有命名参数的查询转换为仅使用传统参数的查询,这让我感到窒息?并且不明白它不是传统参数。