【问题标题】:Caused by: org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 3引起:org.springframework.dao.IncorrectResultSizeDataAccessException:查询没有返回唯一结果: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

请帮帮我...

【问题讨论】:

    标签: mysql spring-boot spring-security spring-data-jpa spring-data


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2020-09-09
      • 2020-07-14
      • 2016-08-22
      相关资源
      最近更新 更多