简体   繁体   English

如何从redis缓存中取回Long(数据类型)值

[英]How to get Long (data type) value back from redis cache

I am storing userId as Long in redis cache in my spring boot application but while retrieving it returns Integer type and throws exception我在 Spring Boot 应用程序的 redis 缓存中将 userId 存储为 Long,但在检索它时返回 Integer 类型并抛出异常

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

Below is the code snippet I am using.下面是我正在使用的代码片段。

@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;
    }

My Hibernate query is something like that.我的 Hibernate 查询就是这样。 in which A.USR_ID is type Long其中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 class is实体类是

@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;

}

Redis cache doesn't support the Long data type. Redis 缓存不支持 Long 数据类型。 So I have stored whole profile object into redis cache and using getter() , I was able to get the Long value.所以我已经将整个配置文件对象存储到 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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM