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

后端 未结 4 854
不知归路
不知归路 2020-12-31 10:19

When run against and Oracle database, what is the runtime type of the object that the following Spring Hibernate Template (Spring 2.5 and Hibernate 3.3.2GA) code returns whe

4条回答
  •  既然无缘
    2020-12-31 11:11

    The class type of the object that hibernateTemplate.execute() returns is indeed BigDecimal. It turns out that the cause of the ClassCastException is the cast of the return value of method doInHibernate():

    (BigDecimal) query.uniqueResult();
    

    Corrected code:

     BigDecimal count = (BigDecimal) hibernateTemplate.execute(
       new HibernateCallback() { 
        public Object doInHibernate(Session session) throws HibernateException {
         SQLQuery query = session.createSQLQuery(sql);
         return query.uniqueResult();
        }});
     return count;
    

提交回复
热议问题