Spring DATA JPA with Java 8 Stream NonUniqueResultException

时间:2015-05-18 20:32:42

标签: hibernate java-8 spring-data spring-data-jpa

我定义了一个基于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$1.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);

可以安全地假设在Spring Data JPA中没有完成Java 8支持/我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

根据Spring数据JPA documentation

  

并非所有Spring Data模块当前都支持Stream作为返回类型。

尝试更改PagingAndSortingRepository - &gt; JpaRepository并查看它是否有效。

<强>更新

我进一步调试了它,发现Spring Data JPA 1.8.0支持Streaming,如果你使用的是其他版本,则升级到1.8.0。我在1.7.2上得到了同样的例外