简体   繁体   English

Spring 数据 JPA - NonUniqueResultException:查询未返回唯一结果:2

[英]Spring Data JPA - NonUniqueResultException: query did not return a unique result: 2

I have a problem when consulting the database in my RestAPI made with SpringBoot - Spring Data JPA.我在使用 SpringBoot 制作的 RestAPI 中查询数据库时遇到问题 - Spring 数据 JPA。

When I try to get one credential by its IdUserFK only works when there is ONLY ONE user.当我尝试通过其 IdUserFK 获取一个凭证时,只有在只有一个用户时才有效。 If there is more than one credential querying by the IdUferFK the server, I get the following error:如果 IdUferFK 服务器查询多个凭据,我会收到以下错误:

javax.persistence.NonUniqueResultException: query did not return a unique result: 2 javax.persistence.NonUniqueResultException:查询未返回唯一结果:2

I've read that the problem is that uniqueResult is true in hibernate but I don't know how to set it to false to return more than one credential if needed我已经读到问题是 uniqueResult 在 hibernate 中为 true 但我不知道如何将其设置为 false 以在需要时返回多个凭证

MySQL database MySQL 数据库

CREATE DATABASE credentialManager CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE credentialManager;

CREATE TABLE users 
(
    id_user INT AUTO_INCREMENT PRIMARY KEY,
    name_user VARCHAR(256),
    password_user VARCHAR(256)
);
    
CREATE TABLE credentials 
(
    id_credential INT AUTO_INCREMENT PRIMARY KEY,
    name_credential VARCHAR(256),
    user_credential VARCHAR(256),
    password_credential VARCHAR(256),
    id_user_fk INT,
    FOREIGN KEY (id_user_fk)
        REFERENCES users (id_user)
        ON DELETE CASCADE ON UPDATE CASCADE
);

Interface ICredentialDAO接口 ICredentialDAO

public interface ICredentialDAO extends CrudRepository<Credential, Long> {

    Credential findAllByIdUserFK(Long idUserFK);
}

Interface ICredentialService接口 ICredentialService

public interface ICredentialService {

    List<Credential> findAll();

    List<Credential> findAllByIdUserFK(Long id);
}

Class CredentialServiceImpl Class CredentialServiceImpl

@Service
public class CredentialServiceImpl implements ICredentialService {

    @Autowired
    private ICredentialDAO credentialDAO;

    @Override
    @Transactional(readOnly = true)
    public List<Credential> findAll() {
        return (List<Credential>) credentialDAO.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Credential> findAllByIdUserFK(Long id) {
        return (List<Credential>) credentialDAO.findAllByIdUserFK(id);
    }
}

Controller CredentialRestController Controller CredentialRestController

@RestController
@RequestMapping("/api")
public class CredentialRestController {

    @Autowired
    private ICredentialService credentialService;

    @GetMapping("/credentials")
    public List<Credential> index() {
        return credentialService.findAll();
    }

    @GetMapping("/credentials/user/{idUser}")
    public List<Credential> showCredential(@PathVariable Long idUser) {
        return this.credentialService.findAllByIdUserFK(idUser);
    }
}

The full error when I try to do a get to localhost:8080/api/credentials/user/1 in Postman is this当我尝试在 Postman 中访问localhost:8080/api/credentials/user/1时的完整错误是这样的

"timestamp": "2021-04-30T11:53:56.691+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "trace": "org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2\r\n\tat org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:385)\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)\r\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)\r\n\tat org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)\r\n\tat org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)\r\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)\r\n\tat com.sun.proxy.$Proxy88.findAllByIdUserFK(Unknown Source)\r\n\tat com.fortun.credmanback.models.services.CredentialServiceImpl.findAllByIdUserFK(CredentialServiceImpl.java:38)\r\n\tat com.fortun.credmanback.models.services.CredentialServiceImpl$$FastClassBySpringCGLIB$$cd2fa77a.invoke(<generated>)\r\n\tat org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)\r\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\r\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)\r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)\r\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)\r\n\tat com.fortun.credmanback.models.services.CredentialServiceImpl$$EnhancerBySpringCGLIB$$2e44cbb0.findAllByIdUserFK(<generated>)\r\n\tat com.fortun.credmanback.controllers.CredentialRestController.showCredential(CredentialRestController.java:31)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:626)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:733)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.base/java.lang.Thread.run(Thread.java:836)\r\nCaused by: javax.persistence.NonUniqueResultException: query did not return a unique result: 2\r\n\tat org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:128)\r\n\tat org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1648)\r\n\tat org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:111)\r\n\tat org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:196)\r\n\tat org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88)\r\n\tat org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155)\r\n\tat org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)\r\n\tat org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryQueryMethodInvoker$$Lambda$1086/0x0000000000000000.invoke(Unknown Source)\r\n\tat org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)\r\n\tat org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)\r\n\tat org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)\r\n\tat org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)\r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)\r\n\t... 71 more\r\n",
    "message": "query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2",
    "path": "/api/credentials/user/1"

Thanks!谢谢!

I think there are some constraints (like @Unique field) in your model that there should be a single entity for a single id.我认为在您的 model 中存在一些限制(例如 @Unique 字段),单个 id 应该有一个实体。 But somehow db has 2 rows for this same id.但不知何故,db 有 2 行用于相同的 id。 That's why it causes an error when mapping the results.这就是为什么它在映射结果时会导致错误。

Write

List<Credential> findAllByIdUserFK(Long idUserFK);

instead of代替

Credential findAllByIdUserFK(Long idUserFK);

in your ICredentialDAO在您的ICredentialDAO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询没有返回唯一结果:3; 嵌套异常是 javax.persistence.NonUniqueResultException: (Spring JPA Project) - query did not return a unique result: 3; nested exception is javax.persistence.NonUniqueResultException: (Spring JPA Project) org.hibernate.NonUniqueResultException:查询未返回唯一结果:462 - org.hibernate.NonUniqueResultException: query did not return a unique result: 462 javax.persistence.NonUniqueResultException:查询未返回唯一结果 - javax.persistence.NonUniqueResultException: query did not return a unique result org.hibernate.NonUniqueResultException:查询未返回唯一结果:2? - org.hibernate.NonUniqueResultException: query did not return a unique result: 2? 查询未返回唯一结果 - Query did not return a unique result 如何在 Spring 引导时避免此错误:(查询未返回唯一结果:4) - How to avoid this error on Spring Boot: (query did not return a unique result: 4) NonUniqueResultException Spring JPA - NonUniqueResultException Spring JPA JPA @Query 给出 NonUniqueResultException - JPA @Query gives NonUniqueResultException JPA存储过程结果集映射和NonUniqueResultException - JPA Stored Procedure result set mappings and NonUniqueResultException 使用spring jpa“findtop”时抛出NonUniqueResultException - NonUniqueResultException thrown when using spring jpa “findtop”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM