对于 Oracle 上的计数查询,Spring Hibernate 模板执行方法返回什么对象类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3504751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What object type does Spring Hibernate Template execute method return for a counting query on Oracle?
提问by Derek Mahar
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
?
当针对 Oracle 数据库运行时,以下Spring Hibernate 模板(Spring 2.5 和 Hibernate 3.3.2GA)代码返回的对象的运行时类型是什么,其中 SQL 查询是一个计数查询,例如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.doInHibernate(RfqCdoUsDaoHibernate.java:124)
采纳答案by Derek Mahar
Turns out that the ClassCastException
may be due to a bug in the Hibernate standard query cache.
原来这ClassCastException
可能是由于 Hibernate 标准查询缓存中的错误。
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:
参考:
- ClassCastException with SQLQuery and setCacheable(true)
- Martin Schaaf's Blog: ClassCastException with SQLQuery and setCacheable(true)
- Caching a raw sql count with Hibernate and EhCache
- HHH-5163 Bug when applying a ResultTransformer on a cacheable projection based criteria
- ClassCastException when Hibernate tries to cache results using ResultTransformer
回答by J?rn Horstmann
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.
这适用于 Number 的所有子类,如 Long、Double、Biginteger 或 BigDecimal。
回答by Ankit Bansal
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 :)
我认为它返回 Long 类型。我在我的代码中使用了 Long。但是,如果您的 BigDecimal 有效,那么即使我想知道返回类型是什么:)
回答by Derek Mahar
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()
:
hibernateTemplate.execute()
返回的对象的类类型确实是BigDecimal
. 事实证明,原因ClassCastException
是方法的返回值的强制转换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;