简体   繁体   English

java.math.BigInteger 无法转换为 java.lang.Long

[英]java.math.BigInteger cannot be cast to java.lang.Long

I've got List<Long> dynamics .我有List<Long> dynamics And I want to get max result using Collections .我想使用Collections获得最大结果。 This is my code:这是我的代码:

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 .现在我得到java.math.BigInteger cannot be cast to java.lang.Long What's wrong?怎么了?

Better option is use SQLQuery#addScalar than casting to Long or BigDecimal .更好的选择是使用SQLQuery#addScalar 而不是转换为LongBigDecimal

Here is modified query that returns count column 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相关链接

Your error might be in this line:您的错误可能在这一行:

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

where query.list() is returning a BigInteger List instead of Long list.其中 query.list() 返回 BigInteger 列表而不是 Long 列表。 Try to change it to.尝试将其更改为。

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

尝试将 BigInteger 转换为这样的 long

Long longNumber= bigIntegerNumber.longValue();

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...

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

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

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 BigInteger for numeric SQL types.您需要为您的查询添加计数的别名,然后使用addScalar()方法作为 Hibernate 接缝中的list()方法的默认值,以便为数字 SQL 类型设置BigInteger Here is an example:下面是一个例子:

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

Are you sure dynamics is a List<Long> and not List<BigInteger> ?您确定 dynamics 是List<Long>而不是List<BigInteger>吗?

If dynamics is a List<Long> you don't need to do a cast to (Long)如果动态是一个List<Long>你不需要做一个演员 (Long)

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

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

You need to retrieve the result as List of BigInteger then convert it to a Long collection.您需要将结果检索为 BigInteger 列表,然后将其转换为Long集合。

List<BigInteger> ids = query.getResultList();
Set<Long> collect = ids.stream().map(id -> id.longValue()).collect(Collectors.toSet());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long 尝试连接后,我一直收到 java.math.bigInteger 无法转换为 java.lang.Long 的问题 - I keept getting a java.math.bigInteger cannot be cast to java.lang.Long iafter trying to connect ClassCastException: java.math.BigInteger 在连接到 MySQL 时不能转换为 java.lang.Long - ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL java.math.BigInteger与java.lang.Long不兼容 - java.math.BigInteger incompatible with java.lang.Long 当我尝试建立与MySQL的连接时,得到以下信息。 java.math.BigInteger不能强制转换为java.lang.Long - I get the following when I try to establish a connection to MySQL. java.math.BigInteger cannot be cast to java.lang.Long 无法将java.math.BigInteger强制转换为java.lang.Long - java.math.BigInteger can't be cast to java.lang.Long Sqoop2无法从mysql导入到hdfs,原因是:“引起原因:java.lang.ClassCastException:java.math.BigInteger无法转换为java.lang.Long” - Sqoop2 failed import from mysql to hdfs with “Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long” 我无法使用JSP文件从MySQL数据库检索数据并收到java.math.BigInteger不能转换为java.lang.Long错误 - I can't retrieve data from MySQL database using a JSP file and receive a java.math.BigInteger cannot be cast to java.lang.Long error java.lang.Integer不能强制转换为java.math.BigInteger - java.lang.Integer cannot be cast to java.math.BigInteger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM