java.lang.Long cannot be cast to java.lang.Integer

Error Information:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer


Error Cause:

1
2
3
4
5
6
7
8
9
10
@SuppressWarnings("unchecked")
public int findCount() {
// here "Customer" is class name
List<Object> list = (List<Object>) this.getHibernateTemplate().find("select count(*) from Customer");
if(list != null && list.size() !=0){
int count = (int) list.get(0); // !!!!!!!error occurs!!!!!!!!!!!
return count;
}
return 0;
}


Error Solution:

1
2
3
4
5
6
7
8
9
10
11
@SuppressWarnings("unchecked")
public int findCount() {
// here "Customer" is class name
List<Object> list = (List<Object>) this.getHibernateTemplate().find("select count(*) from Customer");
if(list != null && list.size() !=0){
Long lcount = (Long) list.get(0);
int count = lcount.intValue();
return count;
}
return 0;
}


Summary:
select count(*) from Customer in Hibernate return a Long number after version 3.2, so List<Object>.
Object to Integer should be Object to Long, then to Integer.