java.math.BigInteger cannot be cast to java.lang.Long
133,348
Solution 1
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.
List<BigInteger> result = query.list();
Solution 2
Better option is use SQLQuery#addScalar than casting to Long
or BigDecimal
.
Here is modified query that returns count
column as 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 javadocs
- Scalar queries
-
Hibernate.LONG
, remember it has been deprecated since Hibernate version 3.6.X
here is the deprecated document, so you have to useLongType.INSTANCE
- My previous answer
Solution 3
Try to convert the BigInteger to a long like this
Long longNumber= bigIntegerNumber.longValue();
Solution 4
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...
Solution 5
It's a very old post, but if it benefits anyone, we can do something like this:
Long max=((BigInteger) Collections.max(dynamics)).longValue();
Related videos on Youtube
Author by
Tony
My favorite programming language is Java with Spring Framework.
Updated on March 07, 2022Comments
-
Tony about 2 years
I've got
List<Long> dynamics
. And I want to get max result usingCollections
. This is my code:List<Long> dynamics=spyPathService.getDynamics(); Long max=((Long)Collections.max(dynamics)).longValue();
This is my
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?-
Brian Roach over 10 yearsI'd say you're trying to cast a
BigInteger
to aLong
. -
Luiggi Mendoza over 10 yearsBecause a
java.math.BigInteger
class instance is not an instance ofjava.lang.Long
class. -
Eric Stein over 10 yearsYou're going to need to provide more code if you want more help. Maybe the line the error is pointing to? And what type does getDynamics() return?
-
user902383 over 10 yearsyou have your answer in your question, i assume this
Collections.max(dynamics))
isBigInteger
, and you are trying cast it to long, try to cast it toBigInteger
, and then uselongValue()
method -
StormeHawke over 10 yearsOk people, he's clearly labeled his dynamics list as a
List<Long>
. @Tony Check the return type ofspyPathService.getDynamics()
. Assuming your error is actually coming from these lines of code, I would guess from this code that spyPathService.getDynamics() is actually returning a List that at the very least includes someBigInteger
s
-
-
Tony over 10 yearsI heard that
COUNT(*)
statement returnsLong
. Doesn't it? -
mike over 10 yearsAs @StormeHawke said '...is actually returning a List that at the very least includes some BigIntegers'. So you better find out exactly, before you run into the next problem of this kind.
-
gavenkoa about 10 yearsThis answer lies on wrong assumption about internal Hibernate wrapper types! The only Aniket Kulkarni answer SQLQuery#addScalar is right!
-
gavenkoa about 10 yearsYou are hero! +1, but really +100!
-
Bills over 6 yearsI also type cast the list