java.math.BigInteger 不能强制转换为 java.lang.Long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18361558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:52:45  来源:igfitidea点击:

java.math.BigInteger cannot be cast to java.lang.Long

javahibernatecollectionslong-integerbiginteger

提问by Tony

I've got List<Long> dynamics. And I want to get max result using Collections. This is my code:

我有List<Long> dynamics。我想使用Collections. 这是我的代码:

List<Long> dynamics=spyPathService.getDynamics();
        Long max=((Long)Collections.max(dynamics)).longValue(); 

This is my getDynamics:

这是我的getDynamics

public List<Long> getDynamics() {

        Session session = null;

        session = this.sessionFactory.getCurrentSession();
        Query query = session
                .createSQLQuery("SELECT COUNT(*) FROM SpyPath WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) GROUP BY DATE(time) ORDER BY time;");

        List<Long> result = query.list();
        return result;

    }

Now I'm getting java.math.BigInteger cannot be cast to java.lang.Long. What's wrong?

现在我得到了java.math.BigInteger cannot be cast to java.lang.Long。怎么了?

采纳答案by Amin Abu-Taleb

Your error might be in this line:

您的错误可能在这一行:

List<Long> result = query.list();

where query.list() is returning a BigInteger List instead of Long list. Try to change it to.

其中 query.list() 返回 BigInteger 列表而不是 Long 列表。尝试将其更改为。

List<BigInteger> result = query.list();

回答by dkatzel

Are you sure dynamics is a List<Long>and not List<BigInteger>?

你确定 dynamics 是 aList<Long>而不是List<BigInteger>

If dynamics is a List<Long>you don't need to do a cast to (Long)

如果动态是一个List<Long>你不需要做一个演员 (Long)

回答by mike

I'm lacking context, but this is working just fine:

我缺乏上下文,但这工作得很好:

List<BigInteger> nums = new ArrayList<BigInteger>();
Long max = Collections.max(nums).longValue(); // from BigInteger to Long...

回答by Abstract

Try to convert the BigInteger to a long like this

尝试将 BigInteger 转换为这样的 long

Long longNumber= bigIntegerNumber.longValue();

回答by Aniket Kulkarni

Better option is use SQLQuery#addScalarthan casting to Longor BigDecimal.

更好的选择是使用SQLQuery#addScalar 而不是强制转换为Longor BigDecimal

Here is modified query that returns countcolumn as Long

这是返回count列的修改后的查询Long

Query query = session
             .createSQLQuery("SELECT COUNT(*) as count
                             FROM SpyPath 
                             WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) 
                             GROUP BY DATE(time) 
                             ORDER BY time;")
             .addScalar("count", LongType.INSTANCE);

Then

然后

List<Long> result = query.list(); //No ClassCastException here  

Related link

相关链接

回答by Bert Verhees

Imagine d.getId is a Long, then wrap like this:

想象一下 d.getId 是一个 Long,然后像这样包装:

BigInteger l  = BigInteger.valueOf(d.getId());

回答by DevNG

You need to add an alias for the count to your query and then use the addScalar()method as the default for list()method in Hibernate seams to be BigIntegerfor numeric SQL types. Here is an example:

您需要为查询添加计数的别名,然后将该addScalar()方法用作list()Hibernate 接缝中BigInteger用于数字 SQL 类型的方法的默认值。下面是一个例子:

List<Long> sqlResult = session.createSQLQuery("SELECT column AS num FROM table")
    .addScalar("num", StandardBasicTypes.LONG).list();

回答by Jai Aggarwal

It's a very old post, but if it benefits anyone, we can do something like this:

这是一个非常古老的帖子,但如果它对任何人都有好处,我们可以这样做:

Long max=((BigInteger) Collections.max(dynamics)).longValue();