Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: ClassCastException with SQLQuery and setCacheable(true)
PostPosted: Tue Jul 22, 2008 8:12 am 
Newbie

Joined: Tue Jul 08, 2008 8:02 am
Posts: 10
I have the following code:

Code:
   SQLQuery sqlQuery = s.createSQLQuery("select 1");
   sqlQuery.setCacheable(true);
   Number num = (Number) sqlQuery.uniqueResult();


But I get the following exception:
Code:
Caused by: java.lang.ClassCastException: java.lang.Integer
   at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:83)
   at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2187)
   at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2138)
   at org.hibernate.loader.Loader.list(Loader.java:2096)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1697)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
   at myClass.myMethod(MyClass.java:53)


I think it is a bug, but maybe I'm wrong. The code in StandardQueryCache.put() checks for returnTypes.length, and if it other than 1, it casts to Object[], and this causes the error. The returnTypes.length in this case is 0.

Thanks,
Viliam


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 22, 2008 4:34 pm 
Newbie

Joined: Mon Apr 21, 2008 5:07 pm
Posts: 6
hi Viliam

you got a
Code:
java.lang.ClassCastException: java.lang.Integer
.
Maybe you shouldn't use a Number cast but instead of that an Integer?
If this doesn't help just try to use something in that way

Code:
Object num = (Object) sqlQuery.uniqueResult();
if(num instanceof Integer){
  int value = ((Integer) num).intValue();
  System.out.println("Instance is an Integer and the int representation is "+value);
}
if(num instanceof Integer){
  int value = ((Integer) num).intValue();
  System.out.println("Instance is an Integer and the int representation is "+value);
}
if(num instanceof Double){
  double value = ((Double) num).doubleValue();
  System.out.println("Instance is a Double and the double representation is "+value);
}


Are you using J2SE5.0? If so then Number is an abstract class.

Hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 12:52 am 
Newbie

Joined: Tue Jul 08, 2008 8:02 am
Posts: 10
The exception occured inside of org.hibernate.cache.StandardQueryCache.put(), not inside of my method. There is a cast that fails, on line 83.

I use java.lang.Number because different databases could return different subclasses of Number (e.g. BigInteger, Long, etc.).

I forgot, I use Hibernate 3.2.4.

Viliam


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 6:48 pm 
Newbie

Joined: Mon Apr 21, 2008 5:07 pm
Posts: 6
It seems to be tricky. hope that there is a solution outside. From sure you checked the configuration to be correct? Did you check different cache modes? What happens if you don't user Number but an implementation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 8:29 am 
Newbie

Joined: Thu Jan 15, 2009 8:19 am
Posts: 1
I got the same error and it was solved by calling addScalar(String, Type) on the query. In my case I had to say that what I was selecting was of type String to get the caching to work. I don't know how to handle the particular case in this thread since addScalar takes a name as the first parameter and the "1" in the query at hand does not have a name.

For my purposes this worked:

Code:
session.createSQLQuery("select name from some_table where name like :prefix")
                        .addScalar("name", Hibernate.STRING)
                        .setString("prefix", prefix + "%")
                        .setCacheable(true)
                        .list();


which I want to return all names with a certain prefix. Without the call to addScalar I got a java.lang.ClassCastException: java.lang.String when cacheable was set to true.

I hope this helps someone that Googles to this page.


Top
 Profile  
 
 Post subject: Re: ClassCastException with SQLQuery and setCacheable(true)
PostPosted: Sat Jun 20, 2009 10:24 am 
Newbie

Joined: Tue Jul 08, 2008 8:02 am
Posts: 10
Thanks, this helped in my case as well...
You can give alias to any column, in this case "select 1 as numberone".

Viliam


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 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.