[英]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 而不是转换为Long
或BigDecimal
。
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相关链接
Hibernate.LONG
, remember it has been deprecated since Hibernate version 3.6.X Hibernate.LONG
,请记住它自 Hibernate version 3.6.X 以来已被弃用LongType.INSTANCE
这是已弃用的文档,因此您必须使用LongType.INSTANCE
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.