3

我将 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;
    }

我的休眠查询是这样的。其中 A.USR_ID是类型Long

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

}
4

1 回答 1

2

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);
于 2019-05-02T08:13:31.840 回答