java.math.BigInteger cannot be cast to java.lang.Long错误的解决方案

场景

mysql数据库中

字段为`authority_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '权限ID',

问题


public interface BmRoleAuthorityRepository extends JpaRepository<BmRoleAuthority, Long>{

@Query(value = "SELECT authority_id FROM bm_role_authority where role_id = ?1", nativeQuery = true)
List<Long> getAllAuthorityIdByRoleId(Long roleId);

直接执行,获取到的List<Long>是

之后我想循环authorityIdList

for (Long authorityId : authorityIdList) {

 

结果报错:

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

解决方案

没有看到有什么特别好的方法,只能将BigInterger的List先转换为String,然后再转换为Long List


List<Long> authorityIdList = bmRoleAuthorityRepository.getAllAuthorityIdByRoleId(roleId);

if(authorityIdList == null || authorityIdList.isEmpty()) {
   authorityIdList = new ArrayList<>();
}
String idArray = JSONArray.toJSONString(authorityIdList);

之后再处理这个String,将其转换为Long list


// [1,2,3] convert to String[]
String[] split = StringUtils.split(StringUtils.substring(allAuthorityIdByRoleId, 1,
        allAuthorityIdByRoleId.length() - 1), ",");
// guava 将String[] convert to Long[]
authorityIdList.addAll(Lists.transform(Arrays.asList(split), Longs.stringConverter()));

参考:

关于java.math.BigInteger cannot be cast to java.lang.Integer小方法

Decompose a String into Array of Long or List of Long without Loop in JAVA

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值