32

I've got List<Long> dynamics. And I want to get max result using Collections. 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?

Marcelo
  • 11,218
  • 1
  • 37
  • 51
Tony
  • 485
  • 2
  • 7
  • 13
  • 4
    I'd say you're trying to cast a `BigInteger` to a `Long`. – Brian Roach Aug 21 '13 at 15:29
  • 1
    Because a `java.math.BigInteger` class instance **is not** an instance of `java.lang.Long` class. – Luiggi Mendoza Aug 21 '13 at 15:30
  • You'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? – Eric Stein Aug 21 '13 at 15:30
  • 2
    you have your answer in your question, i assume this `Collections.max(dynamics))` is `BigInteger`, and you are trying cast it to long, try to cast it to `BigInteger`, and then use `longValue()`method – user902383 Aug 21 '13 at 15:32
  • 2
    Ok people, he's clearly labeled his dynamics list as a `List`. @Tony Check the return type of `spyPathService.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 some `BigInteger`s – StormeHawke Aug 21 '13 at 15:34

9 Answers9

35

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

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
26

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();
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
  • As @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. – mike Aug 21 '13 at 15:41
  • 1
    This answer lies on wrong assumption about internal Hibernate wrapper types! The only **Aniket Kulkarni** answer *SQLQuery#addScalar* is right! – gavenkoa Apr 04 '14 at 15:53
  • I also type cast the list – Bills Sep 17 '17 at 11:28
16

Try to convert the BigInteger to a long like this

Long longNumber= bigIntegerNumber.longValue();
Abstract
  • 664
  • 1
  • 5
  • 15
3

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...
mike
  • 4,929
  • 4
  • 40
  • 80
3

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

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

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

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

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
dkatzel
  • 31,188
  • 3
  • 63
  • 67
1

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. Here is an example:

List<Long> sqlResult = session.createSQLQuery("SELECT column AS num FROM table")
    .addScalar("num", StandardBasicTypes.LONG).list();
DevNG
  • 5,875
  • 3
  • 18
  • 14
0

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

BigInteger l  = BigInteger.valueOf(d.getId());
Bert Verhees
  • 1,057
  • 3
  • 14
  • 25
0

You need to retrieve the result as List of BigInteger then convert it to a Long collection.

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