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.  [ 2 posts ] 
Author Message
 Post subject: Count do not work java.lang.ClassCastException
PostPosted: Fri May 13, 2011 10:46 pm 
Beginner
Beginner

Joined: Fri Jun 23, 2006 6:40 pm
Posts: 25
Dear Members

I am very confused about the Count in HQL, I receive other values than the expected

I have read 16.16. Tips & Tricks where appear

Quote:
You can count the number of query results without returning them:
( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()


Well I did the follow (compile well)

Quote:
public Integer getCountAllEntidadByNombre(String nombre){
return ((Integer) this.sessionFactory.getCurrentSession().
createQuery("SELECT COUNT(e) FROM Entidad e WHERE e.nombresMaternoEntidad=:nombre").
setParameter("nombre", nombre.trim()).iterate().next()).intValue();
}


In my Test class I have this line

Quote:
Integer count = this.entidadDaoService.getCountAllEntidadByNombre("MANUEL ");
assertTrue("Debo ser igual o mayor que uno", count >=1 )

but I receive this error

Quote:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at com.jordan.manuel.repository.hibernate.EntidadDaoImpl.getCountAllEntidadByNombre(EntidadDaoImpl.java:73)

I am confused, why or from where appear the java.lang.Long ?, according with the API specification intValue() said

Quote:
intValue

public int intValue()

Returns the value of this Integer as an int.

Specified by:
intValue in class Number

Returns:
the numeric value represented by this object after conversion to type int.




What is wrong?

Thanks in advanced

_________________
kill your pride, share your knowledge with all


Top
 Profile  
 
 Post subject: Re: Count do not work java.lang.ClassCastException
PostPosted: Sun May 15, 2011 8:02 am 
Newbie

Joined: Thu May 05, 2011 6:33 am
Posts: 16
I think that this casting is wrong...

((Integer) this.sessionFactory.getCurrentSession().
createQuery("SELECT COUNT(e) FROM Entidad e WHERE e.nombresMaternoEntidad=:nombre").
setParameter("nombre", nombre.trim())

I'm not sure but seem that the count return a Long and not an Integer.

So I will try:

((Long) this.sessionFactory.getCurrentSession().
createQuery("SELECT COUNT(e) FROM Entidad e WHERE e.nombresMaternoEntidad=:nombre").
setParameter("nombre", nombre.trim())

_________________
Luca Preziati


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