原因:
从Hibernate 3.0.x/3.1.x升级到最新的3.2版之后,3.2版的很多sql函数如count(), sum()的唯一返回值已经从Integer变为Long,如果不升级代码,会得到一个ClassCastException。
这个变化主要是为了兼容JPA,可以在hibernate.org的最新文档中找到说明。
轻松解决如下:
public int getTotalCount(){
Query q=getSession().createQuery("select count(*) from Student");
List cc=q.list();
Integer a=((Number)cc.get(0)).intValue();
return a.intValue();
}
注意其中Integer a=((Number)cc.get(0)).intValue(); cc.get(0)是你相应代码,你只主要按照这段代码加上Number 和 intValue()即可
---sean