-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: ClassCastException while using count(*) in HQL query
PostPosted: Thu Mar 12, 2009 10:26 am 
Newbie

Joined: Thu Mar 12, 2009 9:13 am
Posts: 3
Hi,

I am using Hibernate 3.3.1 GA version for one of my project. I am getting following exception while using "count()" agreegate function.
"java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer"

Following is the sample code that I have written for testing count() function:

Code in TestDAO:
=====================================
String strQuery = "Select count(empId) as empCount from TbTestEmp where deptNo = ?";
Query query = session.createQuery(strQuery);
query.setInteger(0, 20);
return query.list();
=====================================

I am using the following code to find the employees by depart no. The TestDAO.findEmpCountByDept is called as follows:

=========================================
TestDAO testDAO = new TestDAO();
List empList = empDAO.findEmpCountByDept();

if(empList.size() > 0) {
Integer empCount = (Integer) empList.get(0); // Exception at this line.
System.out.println("=============== empCount " + empCount);
}
========================================

I am getting the above exception while retriving value from the list.

However, the only solution to this problem is to use Long class instead of Integer class to retrive the value.

Is there any solution other this?

Any help will be appreciated.

Thanks in advance,

Avinash Deshmukh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 10:51 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
What is happining here? findEmpCountByDept();
Please paste your class or mapping files wich belong to this error!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 12:26 pm 
Newbie

Joined: Thu Mar 12, 2009 9:13 am
Posts: 3
Hi,

Following is the class :

public class TestDAO {

public List findEmpCountByDept() {
Session session = HibernateSessionFactory.getSession();

try {
String strQuery = "Select count(empId) as empCount from TbTestEmp where deptNo = ?";
Query query = session.createQuery(strQuery);
query.setInteger(0, 20);
return query.list();
} catch (RuntimeException re) {
throw re;
}
}
}

The following code calls the "findEmpCountByDept()" method:
TestDAO testDAO = new TestDAO();
List empList = testDAO.findEmpCountByDept();

if(empList.size() > 0) {
Integer empCount = (Integer) empList.get(0); // Exception at this line
System.out.println("=============== empCount " + empCount);
}


Following is the mapping file:

<hibernate-mapping>
<class name="com.test.om.TestEmp" table="TEST_EMP">
<id name="empId" type="java.lang.Integer">
<column name="EMP_ID"/>
<generator class="identity"/>
</id>
<property name="empName" type="java.lang.String">
<column name="EMP_NAME" not-null="true" length="50"/>
</property>
<property name="deptNo" type="java.lang.Integer">
<column name="DEPT_NO" not-null="false"/>
</property>
</class>
</hibernate-mapping>

Thanks,

Avinash Deshmukh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 2:59 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The count() function always return Long values. I think it is in some specification (JPA?) that it should do that.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 6:44 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
it's safer if you use Number to handle the return value.

Code:
if(empList.size() > 0) {
    Number empCount = (Number) empList.get(0);
    System.out.println("=============== empCount " + empCount);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 6:14 am 
Newbie

Joined: Thu Mar 12, 2009 9:13 am
Posts: 3
nordborg wrote:
The count() function always return Long values. I think it is in some specification (JPA?) that it should do that.


If you see the FAQ Tip and Tricks of Hibernate (http://www.hibernate.org/118.html#A2) :

How can I count the number of query results without actually returning them?
Integer count = (Integer) session.createQuery("select count(*) from ....").uniqueResult();

From this FAQ:Tip and Trick, it should return Integer instead of Long.

I tried to get result by using above statement, but I am getting the same ClassCastException.

Thanks,

Avinash Deshmukh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 6:21 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I guess it is a bug in the FAQ. The count() method used to return an int, but it was changed several of years ago. I don't know in which version but I recall that I got the ClassCastException after installing a new Hibernate version and that I had to change my code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 10:39 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
that's why it's better if with use Number to handle the numeric return instead of using Long or Integer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.