Spring数据redis总是返回相同的结果

时间:2016-05-26 10:05:51

标签: java spring redis spring-data spring-data-redis

我使用Spring Data Redis和JPA风格的查询方法查询Redis:

public interface UserRepository extends CrudRepository<User, String> {
   User findByUsernameAndPassword(String username, String password);
}

这个方法完美地返回了用户,但是一旦我创建了第二个用户,我意识到一旦调用了这个方法,它就会不断返回相同的结果,无论参数的值如何。

我还没有启用任何缓存,即便如此,这应该基于参数。

实体用户:

@RedisHash("users")
public class User {
    private @Id Long id;
    private @Indexed String username;
    private @Indexed String password;
    private UserRole role;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserRole getRole() {
        return role;
    }

    public void setRole(UserRole role) {
        this.role = role;
    }
}

Spring Data Redis中是否存在已知问题?我做错了吗?

依赖关系:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.data</groupId>
                    <artifactId>spring-data-redis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>

我强制使用最新版本的Spring数据redis,因此我可以获得新的@RedisHash@TimeToLive注释

修改

示例案例:

    User userA = new User();
    userA.setUsername("admin");
    userA.setPassword("admin");
    userA.setRole(UserRole.ADMIN);

    userRepository.save(userA);

    User userB = new User();
    userB.setUsername("aaa");
    userB.setPassword("aaa");

    userRepository.save(userB);

    userRepository.findByUsernameAndPassword("aaa","aaa"); //Returns user B (aaa,aaa)
    userRepository.findByUsernameAndPassword("admin","admin"); //Returns user B again, instead of A        
    userRepository.findByUsernameAndPassword("222","222"); //Returns user B again again! even though it doesn't exist.

如果我们通过Id获得用户,一切正常

0 个答案:

没有答案