public interface ServiceTransactionRepository extends CrudRepository<ServiceTransaction, Long>, JpaSpecificationExecutor<ServiceTransaction> {
@Query(value = "SELECT t.userName, COUNT(t.sid) as number_of_total_transactions, COALESCE(SUM(t.transaction_amount)) as total_transaction_sum, count(z.id) number_of_commission_transactions, COALESCE(SUM(z.amount),0) as commission_amount, t.authority FROM (SELECT u.userName, u.authority, st.id as sid, st.amount as transaction_amount FROM User u JOIN ServiceTransaction st ON u.id=st.identity WHERE (st.transactorType=0 or st.transactorType=1) and st.created>=?2 and st.created<=?3 and (st.transactionStatus=0 OR st.transactionStatus=1) and st.identity IN (?1)) AS t LEFT JOIN ZoneCommission z ON t.sid=z.serviceTransaction_id GROUP BY t.userName", nativeQuery = true)
List<Object> getAgentChildTransactionDetails(List<Long> userIds, Date fromDate, Date toDate);
}
@Test
public void testGetAgentChildTransaction() {
List<Long> userIds = getUserIds();
Date fromDate = new Date();
Date toDate = new Date();
List<Object> transactions = serviceTransactionRepository.getAgentChildTransactionDetails(userIds, fromDate, toDate);
//check
}
public List<Long> getUserIds() {
List<Long> list = new ArrayList<Long>();
list.add(childAgentUser.getId());
System.out.println("Size of list : " + list.size());//I get 1 here
return list;
}
在上面的代码中,我正确地获得了userIds
。在正常情况下,代码再次完美,但从测试用例中我面临异常
java.sql.SQLException: Unexpected token in statement [SELECT t.userName, COUNT(t.sid) as number_of_total_transactions, COALESCE(SUM(t.transaction_amount)) as total_transaction_sum, count(z.id) number_of_commission_transactions, COALESCE(SUM(z.amount),0) as commission_amount, t.authority FROM (SELECT u.userName, u.authority, st.id as sid, st.amount as transaction_amount FROM User u JOIN ServiceTransaction st ON u.id=st.identity WHERE (st.transactorType=0 or st.transactorType=1) and st.created>=? and st.created<=? and (st.transactionStatus=0 OR st.transactionStatus=1) and st.identity IN (?)) AS t LEFT JOIN ZoneCommission z ON t.sid=z.serviceTransaction_id GROUP BY t.userName]
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT t.userName, COUNT(t.sid) as number_of_total_transactions, COALESCE(SUM(t.transaction_amount)) as total_transaction_sum, count(z.id) number_of_commission_transactions, COALESCE(SUM(z.amount),0) as commission_amount, t.authority FROM (SELECT u.userName, u.authority, st.id as sid, st.amount as transaction_amount FROM User u JOIN ServiceTransaction st ON u.id=st.identity WHERE (st.transactorType=0 or st.transactorType=1) and st.created>=? and st.created<=? and (st.transactionStatus=0 OR st.transactionStatus=1) and st.identity IN (?)) AS t LEFT JOIN ZoneCommission z ON t.sid=z.serviceTransaction_id GROUP BY t.userName]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:635)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy80.getAgentChildTransactionDetails(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy81.getAgentChildTransactionDetails(Unknown Source)
at com.eccount.server.repositories.ServiceTransactionRepositoryTest.testGetAgentChildTransaction(ServiceTransactionRepositoryTest.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
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.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
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:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at $Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2536)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:77)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:55)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:95)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:85)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:313)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
... 67 more
Caused by: java.sql.SQLException: Unexpected token in statement [SELECT t.userName, COUNT(t.sid) as number_of_total_transactions, COALESCE(SUM(t.transaction_amount)) as total_transaction_sum, count(z.id) number_of_commission_transactions, COALESCE(SUM(z.amount),0) as commission_amount, t.authority FROM (SELECT u.userName, u.authority, st.id as sid, st.amount as transaction_amount FROM User u JOIN ServiceTransaction st ON u.id=st.identity WHERE (st.transactorType=0 or st.transactorType=1) and st.created>=? and st.created<=? and (st.transactionStatus=0 OR st.transactionStatus=1) and st.identity IN (?)) AS t LEFT JOIN ZoneCommission z ON t.sid=z.serviceTransaction_id GROUP BY t.userName]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask.run(GooGooStatementCache.java:525)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
15:28:52.678 [Test worker] ERROR o.h.util.JDBCExceptionReporter - Unexpected token in statement [SELECT t.userName, COUNT(t.sid) as number_of_total_transactions, COALESCE(SUM(t.transaction_amount)) as total_transaction_sum, count(z.id) number_of_commission_transactions, COALESCE(SUM(z.amount),0) as commission_amount, t.authority FROM (SELECT u.userName, u.authority, st.id as sid, st.amount as transaction_amount FROM User u JOIN ServiceTransaction st ON u.id=st.identity WHERE (st.transactorType=0 or st.transactorType=1) and st.created>=? and st.created<=? and (st.transactionStatus=0 OR st.transactionStatus=1) and st.identity IN (?)) AS t LEFT JOIN ZoneCommission z ON t.sid=z.serviceTransaction_id GROUP BY t.userName]
答案 0 :(得分:0)
子查询是问题所在。我面临着hsqldb似乎无法解析的子查询的类似问题。 Insert ignore into ...语句可能会遇到类似的错误。 但我不确定最新的hsqldb版本是否支持此功能。