gpt4 book ai didi

hibernate - 带有 Java 8 Stream NonUniqueResultException 的 Spring DATA JPA

转载 作者:行者123 更新时间:2023-12-05 00:59:44 24 4
gpt4 key购买 nike

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

结果预计会有多个结果。但是,结果出来是一个异常(exception)如下
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 支持不完整是否安全/我在这里做错了什么?

最佳答案

根据 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 上遇到了同样的异常

关于hibernate - 带有 Java 8 Stream NonUniqueResultException 的 Spring DATA JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com