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

    xiaoxiao2021-03-25  128

    I’m getting the following exception.

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

    with the following code:

    @Query(value = "SELECT id FROM "+ GlobalConsts.INTELLIF_STATIC + "." +GlobalConsts.T_NAME_OTHER_DETAIL +" WHERE indexed < 0 and from_image_id = :from_image_id ", nativeQuery = true) List<Long> findFailedId(@Param("from_image_id")long from_image_id); List<Long> failedIds = otherDetailDao.findFailedId(key); List<String> failedList = new ArrayList<String>(); for (Long id : failedIds) { String url = ResourceHelper.generateFileName(id); String idStr = ResourceHelper.decodeResourceId(url); failedList.add(idStr); }

    解决方法: 在mysql中,long类型的字段都用bigint来表示,所以在进行hibernate进行解析的时候,会变成BigInteger,而这时我们却用Long类型来进行解析,会直接报异常:

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

    在这里需要修改如下代码:(如果我们需要用到long类型的字段,可以将BigInteger类型转化为long型,xxx.intValue())

    @Query(value = "SELECT id FROM "+ GlobalConsts.INTELLIF_STATIC + "." +GlobalConsts.T_NAME_OTHER_DETAIL +" WHERE indexed < 0 and from_image_id = :from_image_id ", nativeQuery = true) List<BigInteger> findFailedId(@Param("from_image_id")long from_image_id); List<BigInteger> failedIds = otherDetailDao.findFailedId(key); List<String> failedList = new ArrayList<String>(); for (BigInteger id : failedIds) { long faiedId = id.intValue(); String url = ResourceHelper.generateFileName(failedId); String idStr = ResourceHelper.decodeResourceId(url); failedList.add(idStr); }
    转载请注明原文地址: https://ju.6miu.com/read-10639.html

    最新回复(0)