Spring 具有 Java 8 Stream NonUniqueResultException 的 DATA JPA
Spring DATA JPA with Java 8 Stream NonUniqueResultException
我定义了一个基于 PagingAndSortingRepository 的存储库以实现以下查询
@Query(" SELECT me FROM CustomerModelEntity me WHERE me.planNum = :planNum AND me.customerModelStatusCode IN ('AVL','PHO')" +
" AND me.modelCode IN (SELECT e.modelCode FROM ModelEntity e WHERE e.modelStatusCode IN ('AVL','PHO'))")
Stream<CustomerModelEntity> findByPlanNum(@Param("planNum") String planNum);
结果预计会有多个结果。但是,结果出现异常如下
Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:505) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:202) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:395) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
... 125 more
如果我使用 List 而不是 Stream,则相同的 @Query 实现效果很好
@Query(" SELECT me FROM CustomerModelEntity me WHERE me.planNum = :planNum AND me.customerModelStatusCode IN ('AVL','PHO')" +
" AND me.modelCode IN (SELECT e.modelCode FROM ModelEntity e WHERE e.modelStatusCode IN ('AVL','PHO'))")
List<CustomerModelEntity> findByPlanNum(@Param("planNum") String planNum);
假设 Java 8 支持在 Spring Data JPA 中不完整是否安全/我在这里做错了吗?
根据 Spring 数据 JPA documentation:
Not all Spring Data modules currently support Stream as a return type.
尝试更改 PagingAndSortingRepository
-> JpaRepository
看看是否有效。
更新:
我进一步调试,发现 Spring Data JPA 1.8.0 支持 Streaming,如果您使用的是其他版本,请升级到 1.8.0。我在 1.7.2
上遇到了同样的异常
我定义了一个基于 PagingAndSortingRepository 的存储库以实现以下查询
@Query(" SELECT me FROM CustomerModelEntity me WHERE me.planNum = :planNum AND me.customerModelStatusCode IN ('AVL','PHO')" +
" AND me.modelCode IN (SELECT e.modelCode FROM ModelEntity e WHERE e.modelStatusCode IN ('AVL','PHO'))")
Stream<CustomerModelEntity> findByPlanNum(@Param("planNum") String planNum);
结果预计会有多个结果。但是,结果出现异常如下
Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:505) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:202) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:97) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:88) [spring-data-jpa-1.7.2.RELEASE.jar:]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:395) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486) [spring-data-commons-1.9.2.RELEASE.jar:]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) [spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
... 125 more
如果我使用 List 而不是 Stream,则相同的 @Query 实现效果很好
@Query(" SELECT me FROM CustomerModelEntity me WHERE me.planNum = :planNum AND me.customerModelStatusCode IN ('AVL','PHO')" +
" AND me.modelCode IN (SELECT e.modelCode FROM ModelEntity e WHERE e.modelStatusCode IN ('AVL','PHO'))")
List<CustomerModelEntity> findByPlanNum(@Param("planNum") String planNum);
假设 Java 8 支持在 Spring Data JPA 中不完整是否安全/我在这里做错了吗?
根据 Spring 数据 JPA documentation:
Not all Spring Data modules currently support Stream as a return type.
尝试更改 PagingAndSortingRepository
-> JpaRepository
看看是否有效。
更新:
我进一步调试,发现 Spring Data JPA 1.8.0 支持 Streaming,如果您使用的是其他版本,请升级到 1.8.0。我在 1.7.2
上遇到了同样的异常