如何从Redis缓存中获取Long(数据类型)值

时间:2019-04-30 08:08:59

标签: java spring-boot redis-cache

我将userId作为Long存储在我的Spring Boot应用程序的Redis缓存中,但是在检索时会返回Integer类型并引发异常

java.lang.Integer cannot be cast to java.lang.Long

下面是我正在使用的代码段。

@Cacheable(value = CacheConstants.GAUTH_CACHE, key = "#root.target.PASSWORD_SALT.concat(#loginTxt.toString())", unless = "#result == null")
    public Long getPasswordSaltFromLoginText(String loginTxt) {
        Long userId = null;
        if(StringUtils.isNotBlank(loginTxt)) {
            userId =  profileRepository.getUserIdForPasswordSalt(loginTxt, "PasswordSalt");
        }
        return userId;
    }

我的Hibernate查询就是这样。其中A.USR_IDLong

@Query(nativeQuery = true, value = "select A.USR_ID from user_tbl A, another_table B WHERE A.USR_ID = B.USR_ID AND "
            + " UPPER(A.loginTxt) = UPPER(:loginTxt) "
            + " AND B.prefName=:prefName ")

    Long getUserId(@Param("loginTxt") String loginTxt, @Param("prefName") String prefName);

实体类为

@Entity
@Table(name="Table1", schema = "Schema_name")
public class Profile {

    @Id
    @Column(name="USR_Id")
    public Long USR_ID;

    @Column(name="other_column")
    public Long other_column;

    @Column(name="other_column2")
    public Long other_column2;

}

1 个答案:

答案 0 :(得分:1)

Redis缓存不支持Long数据类型。因此,我已将整个配置文件对象存储到redis缓存中,并使用getter()能够获得Long值。

@Query(nativeQuery = true, value = "select A.USR_ID,A.other_column,A.other_column2 from user_tbl A, another_table B WHERE A.USR_ID = B.USR_ID AND "
            + " UPPER(A.loginTxt) = UPPER(:loginTxt) "
            + " AND B.prefName=:prefName ")

    Profile getUserId(@Param("loginTxt") String loginTxt, @Param("prefName") String prefName);