I'm using a native query like this:
public Long count() {
String sql = "SELECT COUNT(t.task_id_t100) from T100_TASK t ";
Query query = entityManager.createNativeQuery(sql);
return (Long) query.getSingleResult();
}
This always worked. Today I got a ClassCastException because query.getSingleResult() returned a BigDecimal. I can't remember to have changed anything.
The hint in this answer tells Note that in some cases result type may depend on the database, i.e. it can be something like BigDecimal
In which cases?
you can simply ask the object which class it is...
public Long count() {
String sql = "SELECT COUNT(t.task_id_t100) from T100_TASK t ";
Query query = entityManager.createNativeQuery(sql);
Object o = query.getSingleResult();
//for BigDecimal
if (o.getClass().equals(BigDecimal.class) ){
BigDecimal big = (BigDecimal) o;
return handleBigDecimal(big); //TODO
}
//other types might follow
return (Long) o;
}
but remember - a bigDecimal can be much bigger than a long can be... (can be.. not guaranteed)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With