使用spring-boot-starter-data-jpa(版本2.0.4.RELEASE)和PostgreSQL(版本9.5.10),我一直得到 PSQLException :查询未返回任何结果,尽管我确实按照许多其他人的建议使用了@Modifying
。有人建议添加@Transactional
也没有什么不同。
分析一下StackTrace,可以看到org.postgresql.jdbc.PgPreparedStatement.executeQuery()
被调用,而org.postgresql.jdbc.PgPreparedStatement.executeUpdate()
应该被调用。
我该怎么办?
@Repository
public interface Rank extends JpaRepository<RankArticle, String> {
@Modifying
@Query(value = "UPDATE rank SET id=calc.id, shop_id=calc.shop_id, " +
"score=calc.score, rank = calc.rank FROM " +
"(SELECT id, shop_id, SUM(sales) AS score, CAST(NULL AS INT) AS rank " +
"FROM " +
"(SELECT id, sales, shop_id FROM daily_pod " +
"WHERE shop_id = :shopId " +
"UNION " +
"SELECT id, sales, shop_id FROM weekly_pod " +
"WHERE shop_id = :shopId " +
"UNION " +
"SELECT id, sales, shop_id FROM end_pod " +
"WHERE shop_id = :shopId) " +
"AS pods " +
"GROUP BY id, shop_id ORDER BY score DESC limit :limit) as calc;"
, nativeQuery = true)
void calcScore(@Param("shopId") Integer shopId,
@Param("limit") Integer limit);
}
编辑:添加完整堆栈跟踪:
[ERROR] 2018-09-20 17:49:10,503 [SimpleAsyncTaskExecutor-1] [d.t.p.v.backend.VerkaufsrangConfig] - Konnte AMQP-Nachricht nicht
verarbeiten! org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: 侦听器方法'public void de.thalia.pim.verkaufsrang.backend.application.rabbitmq.SalesHandler.receive(org.springframework.amqp.core.Message)' 抛出异常 在org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:190)处 在org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:120)处 在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1414)处 在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1337)处 在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1324) 在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1303)处 在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:817) 在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:801) 在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access $ 700(SimpleMessageListenerContainer.java:77) 在org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer $ AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1042) 在java.base / java.lang.Thread.run(Thread.java:844) 由以下原因引起:org.springframework.orm.jpa.JpaSystemException:无法提取ResultSet。嵌套异常为 org.hibernate.exception.GenericJDBCException:无法提取 结果集 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:314) 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225) 在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) 在org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) 在org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)处 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) com.sun.proxy。$ Proxy123.calcScore(未知来源) 在de.thalia.pim.verkaufsrang.backend.business.SalesRankService.calculateRank(SalesRankService.java:28) 在de.thalia.pim.verkaufsrang.backend.application.rabbitmq.SalesHandler.receive(SalesHandler.java:60) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181) 在org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114) 在org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:51) 在org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:182) ...省略了10个通用框架 原因:org.hibernate.exception.GenericJDBCException:无法提取ResultSet 在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) 在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) 在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69) 在org.hibernate.loader.Loader.getResultSet(Loader.java:2168) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1931) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1893) 在org.hibernate.loader.Loader.doQuery(Loader.java:938) 在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) 在org.hibernate.loader.Loader.doList(Loader.java:2692) 在org.hibernate.loader.Loader.doList(Loader.java:2675) 在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) 在org.hibernate.loader.Loader.list(Loader.java:2502) 在org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335) 在org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2200) 在org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016) 在org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152) 在org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) 在org.hibernate.query.Query.getResultList(Query.java:146) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机 方法) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在org.springframework.orm.jpa.SharedEntityManagerCreator $ DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:380) com.sun.proxy。$ Proxy141.getResultList(未知来源) 在org.springframework.data.jpa.repository.query.JpaQueryExecution $ CollectionExecution.doExecute(JpaQueryExecution.java:129) 在org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)处 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136) 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590) 在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) 在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ...省略了29个通用框架 原因:org.postgresql.util.PSQLException:查询未返回任何结果。 在org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114) 在com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) 在com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) 在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60) ...省略了63个共同的框架
答案 0 :(得分:0)
设法通过将语句更改为INSERT INTO ... ON CONFLICT(...)做一个更新来使它起作用:
IServiceProvider
}