java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long [英] java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

查看:4863
本文介绍了java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用本机sql返回行数。但控制台说我 java.math.BigInteger无法强制转换为java.lang.Long
怎么了?这是我的方法:

I want to return number of rows using native sql. But console says me java.math.BigInteger cannot be cast to java.lang.Long. What's wrong? This is my method:

public Long getNumRows(Integer id){

        Session session = null;

        session = this.sessionFactory.getCurrentSession();
        Query query = session
                .createSQLQuery("SELECT COUNT(*) FROM controllnews WHERE news_id="
                        + id + ";");
        List firstResult = query.list();

        return (Long) firstResult.get(0);


    }


推荐答案

使用 BigInteger #longValue() 方法,而不是将其强制转换为 Long

return firstResult.get(0).longValue();

似乎 firstResult.get(0)返回对象。一种选择是强制转换为 BigInteger

Seems like firstResult.get(0) returns an Object. One option is to typecast to BigInteger:

return ((BigInteger)firstResult.get(0)).longValue();

但不要这样做。而是使用Nambari在评论中提供的方式。我不是Hibernate的用户,所以我不能提供Hibernate特定的解决方案。

But don't do this. Instead use the way provided by Nambari in comments. I'm no user of Hibernate, so I can't provide Hibernate specific solution.

这篇关于java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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