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