List< Long>对具有BigInteger值的ArrayList的引用 [英] How can a List<Long> references to an ArrayList which has BigInteger values

查看:232
本文介绍了List< Long>对具有BigInteger值的ArrayList的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法在JpaRepository中定义,并运行本机PostgreSQL查询。

This method is defined in a JpaRepository and runs a native PostgreSQL query.

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

它无例外运行,并且在运行时,我在返回的distributorIds ArrayList中看到BigInteger值,而不是Long值

与以下问题相同: Spring Data JPA中的错误:Spring Data返回List< BigInteger>而不是List< Long>

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

So how can this bug occur? I mean how JAVA allows this? If Java doesn't check this kind of type errors isn't it a problem with generics in JAVA.

注意:我还检查了Long和BigInteger的类型层次结构,

Note: I also checked the type hierarchy for Long and BigInteger and there is no sub/super class relation.

推荐答案

泛型类型检查是编译时的功能。在运行时,所有类型信息都会丢失。请参阅类型擦除。例如,如果将使用非泛型集合的旧版API映射到使用泛型且需要强制转换集合的API,则您看到的行为很容易发生。不幸的是,如果该集合包含意外类型的对象,您将只能在运行时找到它。

Generic type checks are a compile time feature. At runtime all type information is lost. See "type erasure". The behavior you see can easily happen if, for example, a legacy API that uses non-generic collections is mapped to an API that uses generics and requires a cast of the collection. If that collection happens to contain objects of an unexpected type you will, sadly, only find out at runtime.

这篇关于List&lt; Long&gt;对具有BigInteger值的ArrayList的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆