What object type does Spring Hibernate Template execute method return for a counting query on Oracle? What object type does Spring Hibernate Template execute method return for a counting query on Oracle? oracle oracle

What object type does Spring Hibernate Template execute method return for a counting query on Oracle?


How about

long value = ((Number)query.uniqueResult()).longValue();return Long.valueOf(value);

This would work for all subclasses of Number like Long, Double, Biginteger or BigDecimal.


Turns out that the ClassCastException may be due to a bug in the Hibernate standard query cache.

Solution is to add a scalar to the query:

String sql = "select count(*) as result from table";BigDecimal count = (BigDecimal) ht.execute(new HibernateCallback() {    public Object doInHibernate(Session session)            throws HibernateException {        SQLQuery query = session.createSQLQuery(sql);        // Add scalar to avoid bug in Hibernate query cache.        query.addScalar("result", Hibernate.BIG_DECIMAL);        return query.uniqueResult();    }});

References:


I think it returns type Long. I have used Long in my code. But if your BigDecimal works then even I want to know what is the return type :)