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?
Try to convert the BigInteger to a long like this
You need to add an alias for the count to your query and then use the
addScalar()
method as the default forlist()
method in Hibernate seams to beBigInteger
for numeric SQL types. Here is an example:Better option is use SQLQuery#addScalar than casting to
Long
orBigDecimal
.Here is modified query that returns
count
column asLong
Then
Related link
Hibernate.LONG
, remember it has been deprecated since Hibernate version 3.6.Xhere is the deprecated document, so you have to use
LongType.INSTANCE
Your error might be in this line:
where query.list() is returning a BigInteger List instead of Long list. Try to change it to.
Imagine d.getId is a Long, then wrap like this:
Are you sure dynamics is a
List<Long>
and notList<BigInteger>
?If dynamics is a
List<Long>
you don't need to do a cast to (Long)