java – 什么对象类型的Spring Hibernate Template执行方法返回给Oracle上的计数查询?

当运行反对和Oracle数据库时,以下Spring Hibernate Template(Spring 2.5和Hibernate 3.3.2GA)代码返回的对象的运行时类型是什么,SQL查询是来自表的select count(*)的计数查询?

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

此代码抛出以下异常:

javax.ejb.EJBException: EJB Exception: : java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;
    at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:83)
    at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2185)
    at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2087)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)
    at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:804)
    at com.db.abstrack.dao.hibernate.RfqCdoUsDaoHibernate$1.doInHibernate(RfqCdoUsDaoHibernate.java:124)
怎么样

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

这将适用于Number的所有子类,如Long,Double,Biginteger或BigDecimal.

翻译自:https://stackoverflow.com/questions/3504751/what-object-type-does-spring-hibernate-template-execute-method-return-for-a-coun

转载注明原文:java – 什么对象类型的Spring Hibernate Template执行方法返回给Oracle上的计数查询?