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.  [ 5 posts ] 
Author Message
 Post subject: ClassCastException using count
PostPosted: Tue Jul 17, 2007 5:31 pm 
Newbie

Joined: Tue Jul 17, 2007 5:16 pm
Posts: 3
Hi,

I am trying to get the count using HQL.

This is my code:
Integer id = (Integer)session.createQuery("select count(*) from Orders").uniqueResult();

When i try to cast the return value as Integer i get a ClassCastException
java.lang.ClassCastException: java.lang.Long

It works fine if i cast to a Long. Is there any problem with the code?

Any help is appreciated

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 5:52 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Um, the count is returned as a Long, you're casting to an Integer. Long != Integer so you get the class cast. I fail to see what the real issue is.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 6:03 pm 
Newbie

Joined: Tue Jul 17, 2007 5:16 pm
Posts: 3
Do you mean to say that the count always returns a Long value ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 17, 2007 6:57 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
From the source I am going to say in HQL yes it should by default always return a long.

STANDARD_AGGREGATE_FUNCTIONS.put( "count", new StandardSQLFunction("count") {
public Type getReturnType(Type columnType, Mapping mapping) {
return Hibernate.LONG;
}
} );

However, it would appear the criteria API would return an Integer. Also, the reference docs seem to imply that it would also be an Integer. However, the code will win and you're ending up with a Long.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 10:41 am 
Newbie

Joined: Tue Jul 17, 2007 5:16 pm
Posts: 3
Thanks for your response


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.