java - List<Long> 如何引用具有 BigInteger 值的 ArrayList

标签 java spring-boot casting spring-data generic-collections

此方法在 JpaRepository 中定义并运行 native PostgreSQL 查询。

List<Long> distributorIds = distributorRepository
.findDistributorIdsWithChildren(distributorId)

它运行时没有异常,在运行时我在返回的distributorIds ArrayList中看到BigInteger值而不是Long值

与此问题相同:Bug in Spring Data JPA: Spring Data returns List<BigInteger> instead of List<Long>

那么这个错误怎么会发生呢?我的意思是JAVA 如何允许这样做?如果Java不检查这种类型错误,那是不是JAVA中泛型的问题。

注意:我还检查了 Long 和 BigInteger 的类型层次结构,并且没有子/父类(super class)关系。

最佳答案

通用类型检查是编译时功能。在运行时,所有类型信息都会丢失。请参阅“类型删除”。例如,如果使用非泛型集合的旧版 API 映射到使用泛型并需要对集合进行强制转换的 API,则您看到的行为很容易发生。如果该集合碰巧包含意外类型的对象,遗憾的是,您只能在运行时发现。

关于java - List<Long> 如何引用具有 BigInteger 值的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948445/

相关文章:

java - "Error: ' .class ' expected"

spring-boot - Swagger UI 试试吧!不适用于 Kubernetes 入口

c# - 删除 DateTime.UctNow.TimeOfDay 的毫秒部分的最简单方法是什么?

java - 检查 java List<object> 对象类型

java - 使用 JTA 隔离分布式(全局)事务

java - Play Framework : Why the cookie isn't showing

javascript - AJAX调用SpringBoot Controller 导致CORS错误

java - Spring Boot 组件扫描无法识别不同 bean 的限定符注释

c++ - 尝试将 static_cast<> const char[] 转换为 unsigned char * 时出错

java - 关于java片段输出的问题