引起:org.springframework.dao.IncorrectResultSizeDataAccessException:查询没有返回唯一结果:3

Posted

技术标签:

【中文标题】引起:org.springframework.dao.IncorrectResultSizeDataAccessException:查询没有返回唯一结果:3【英文标题】:Caused by: org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 3 【发布时间】:2020-06-05 17:40:24 【问题描述】:

我是 spring security 的新手,我正在使用 spring-security 和 Msql 构建应用程序 AuthSystems 我在 JpaRepository 中使用了额外的查询方法,该方法不返回结果,并显示 IncorrectResultSizeDataAccessException。

这是我的代码

用户存储库

package com.ganesh.repository;


import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.ganesh.model.User;


public interface UserRepository extends JpaRepository<User, Integer> 

    User findByUsername(String username);
        


CustomUserDetailsS​​ervice

@Service
public class CustomUserDetailsService implements UserDetailsService 
    
    @Autowired
    private UserRepository userRepo;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException 
        
        User user = userRepo.findByUsername(username);
        
        
        CustomeUserDetail userdetails = null;
        
        if(user != null) 
            
            userdetails = new CustomeUserDetail();
            
            userdetails.setUser(user);
        
            
        else 
            throw new UsernameNotFoundException("User not fond this username"+ username);
        
        return userdetails;
        
        
    


CustomeUserDetail


@Getter
@Setter
public class CustomeUserDetail implements UserDetails 
    
    /**
     * 
     */
    private static final long serialVersionUID = -8354447536649796292L;
    
    
    @Autowired
    private User user;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() 
        
        return user.getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_"+ role)).collect(Collectors.toList());
    

    @Override
    public String getPassword() 
        
        return user.getPassword();
    

    @Override
    public String getUsername() 
        
        return user.getUsername();
    

    @Override
    public boolean isAccountNonExpired() 
        
        return true;
    

    @Override
    public boolean isAccountNonLocked() 
        
        return true;
    

    @Override
    public boolean isCredentialsNonExpired() 
        
        return true;
    

    @Override
    public boolean isEnabled() 
        
        return true;
    


#properties
spring.datasource.url=jdbc:mysql://$MYSQL_HOST:localhost:3306/spring_auth
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.propertirs.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.format_sql=true

请帮帮我...

【问题讨论】:

【参考方案1】:

您的 DB-Table 中似乎有多个用户具有相同的用户名。所以User findByUsername(String username); 会返回多个结果。

您可以执行以下操作之一:

    使数据库中的用户名列独一无二。 将存储库方法更改为List&lt;User&gt; findByUsername(String username); 以获取所有用户 用户名。 将您的存储库方法更改为 User findFirstByUsername(String username); 以仅获取一个(随机)用户。

【讨论】:

以上是关于引起:org.springframework.dao.IncorrectResultSizeDataAccessException:查询没有返回唯一结果:3的主要内容,如果未能解决你的问题,请参考以下文章

查询如何引起gc

杂牌设备网络接入引起交换机引起端口翻转的故障

csharp 可能会引起问题的类的继承问题,父类后来增加的方法可能会引起子类的函数重载错误

两则dhcp设置引起的故障

大开测试:性能-如何解决由于设置引起的运行失败问题(连载22)

查找由智能指针引起的内存泄漏