博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List(泛型)类型转换陷阱,hibernate 原生查询BigInteger 转 Long 出错问题
阅读量:7102 次
发布时间:2019-06-28

本文共 1195 字,大约阅读时间需要 3 分钟。

hot3.png

在使用hibernate 调用原生sql 渴望查询出List<Long>类型数据,查询复制没报错,在使用List<Long>时,却报java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long 错误;

原来hq1查询出的Long 类型数据,sql查询出都是BigInteger 类型;

List a = new ArrayList<BigIneteger> ,在a 没用使用类型限定的情况下,可以赋值给任意类型的List,

如List<Long>,而把它当Long类型使用时,会出错

 

    public void testc(){
         List<Long> LongList = new ArrayList<Long>();
         List bigIntList = new ArrayList<BigInteger>();
         bigIntList.add(new BigInteger("1"));
         LongList = bigIntList;
         for(int i = 0; i < bigIntList.size();i ++ ){
             if( bigIntList.get(i) instanceof Long){
                 System.err.println("is Long type");
             }else{
                 System.err.println("is not Long type");
             }
            
             if( LongList.get(i) instanceof Long){
                 System.err.println("is Long type");
             }else{
                 System.err.println("is not Long type");
             }
            
            
             if( bigIntList.get(i) instanceof BigInteger){
                 System.err.println("is BigInteger type");
             }else{
                 System.err.println("is not BigInteger type");
             }
            
             try{
                 for(Long l : LongList){
                     System.err.println(l);
                 }
             }catch(Throwable t){
                t.printStackTrace();
             }
            
            
         }
    }

 

 

//输出结果

is not Long type

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

 

转载于:https://my.oschina.net/yukong/blog/842547

你可能感兴趣的文章
第1章 安装Exchange2010
查看>>
重写CMFCRibbonStatusBarPane类
查看>>
linux环境下安装mysql
查看>>
2015-10-19高项作业
查看>>
cisco对某一IP的限速
查看>>
忠告17:本田宗一郎:成功出现在九十九次失败后
查看>>
长途猫的归属
查看>>
查看硬件信息命令
查看>>
【翻译】为Ext JS和Sencha Touch开发人员准备的应用程序监测(App Inspector)
查看>>
Hibernate(2)
查看>>
制作WinCE 5.0的ARM模拟器
查看>>
PHP字符串(1)-初始化
查看>>
一个笔试题
查看>>
我的友情链接
查看>>
1.5-LVS的NAT模式
查看>>
我的友情链接
查看>>
IPTABLES学习笔记
查看>>
sqlserver 2008 R2 故障恢复移动文件
查看>>
varnish description
查看>>
Centos 6.5 安装 Atlassiana Crowd+JIRA+Confluence(Wiki)之一 数据库篇(MySQL5.1)
查看>>