Spring Hibernate模板执行方法返回什么对象类型以便在Oracle上进行计数查询? [英] What object type does Spring Hibernate Template execute method return for a counting query on Oracle?

查看:180
本文介绍了Spring Hibernate模板执行方法返回什么对象类型以便在Oracle上进行计数查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当针对Oracle数据库运行时,以下 Spring Hibernate Template(Spring 2.5和Hibernate 3.3.2GA)代码返回SQL查询是一个计数查询,如 select count(*)from table

  String sql =select count(*)from表; 
BigDecimal count =(BigDecimal)hibernateTemplate.execute(
new HibernateCallback(){$ b $ public Object doInHibernate(Session session)throws HibernateException {
SQLQuery query = session.createSQLQuery(sql) ;
return(BigDecimal)query.uniqueResult();
}});
返回计数;

这段代码抛出以下异常:

  javax.ejb.EJBException:EJB Exception::java.lang.ClassCastException:java.math.BigDecimal不能转换为[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)在org.hibernate.loader.Loader.list(Loader.java:2087)处
在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)


原来, ClassCastException 可能是由于Hibernate标准查询缓存中的一个错误e。

解决方案是为查询添加标量:

  String sql =select count(*)as table from table; 
BigDecimal count =(BigDecimal)ht.execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException {
SQLQuery query = session.createSQLQuery(sql) ;
//添加标量以避免Hibernate查询缓存中的错误
query.addScalar(result,Hibernate.BIG_DECIMAL);
return query.uniqueResult();
}
});

参考文献:


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 where the SQL query is a counting query like select count(*) from table?

 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;

This code throws the following exception:

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)

解决方案

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:

这篇关于Spring Hibernate模板执行方法返回什么对象类型以便在Oracle上进行计数查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆