此方法在 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/