• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classcast exception

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using a tble named guage table in my DB.

I am defined a class guagetable in as a model class.
I create a list in the type of guagetable. Ande I am fetching one field from the guage table, that contain values in the type Long.

While executing the particular step I am fetching the data from DB and storing to the List: it's throwing exception given below.

******************************************
java.lang.ClassCastException: java.lang.Long
com.videocon.dao.hibernate.SealReportDAOHibernate.getSealReportData(SealReportDAOHibernate.java:312)
com.videocon.service.impl.SealReportManagerImpl.getSealReportData(SealReportManagerImpl.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
**********************************************

Can anybody tell me why this exception is coming?

-Arun
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More info would be needed to locate the problem.
Can you please post the code where you are trying to fetch the data?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
***********************************************
if(pointlist != null){
recordpoints = pointlist.get(0).getTotalrecordpoints();
}
************************************************
where "pointlist" is the list which is storing the db returned values.
Now I changed the code in this way. "recordpoints" is declared as Long and the selecting value using the code pointlist.get(0).getTotalrecordpoints() is also a Long value.
But now also tha same exception is throwing.

Previosly the code was:
------------------------------------
List<Funnelguagetable> pointlist = new ArrayList<Funnelguagetable>();

pointlist = getHibernateTemplate().find("select fgt.totalrecordpoints from Funnelguagetable fgt where fgt.guagetypenumber=?",new Object[] {new Long(type)});

Iterator iter1 = pointlist.iterator();
Funnelguagetable rd = (Funnelguagetable) iter.next();

-------------------------------------

Actually only one value will be get from DB. Thats why I changed the code.
But both the above code is throwing the classcastexception same time...

Now can you please tell me what the mistake I made?

-Thanks
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reehan,

Please replay me..

-Arun
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method getHibernateTemplate(), what API does that belong to? Is it one you have written yourself, or is it Spring?

Normally, if you run an HQL query where you specify values in the select clause the return type would be an Object array. Does getHibernateTemplate().find() convert the return type of the HQL query into a List?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------
The method getHibernateTemplate(), what API does that belong to
---------------------------

In hibernate framework I am using this query, and returning a list and I was trying to store in a list.
But It was not working that's why I changed the code to take the value in the list and tried to store in a variable..

-Arun
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun R. S. Chandran:
--------------------------------
The method getHibernateTemplate(), what API does that belong to
---------------------------

In hibernate framework I am using this query, and returning a list and I was trying to store in a list.
But It was not working that's why I changed the code to take the value in the list and tried to store in a variable..

-Arun



This method is not part of the Hibernate API. Which API is it part of?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using MyEclipse as IDE...

As I am beginner , please pardan me for any mistakes made..

-Thanks,
ARUN
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't worry about making mistakes here - we're here to help after all, and everyone has been a beginner at one time or another.

We still need to workout where this getHibernateTemplate() method comes from and what it does. There is a method in the Spring framework with this signature. There is also I believe an example class in the Hibernate Wiki with this method. Are you using either of these?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your kind replay Paul.

I am using it from Hibernate.

Actually I am working in an end stage project. So I am copy paste same code from previous hibernate codes.

In my first post I mentioned which are the model classes I am using. And this --------getHibernateTemplate().find()----------
is using for fetching data from DataBase.

-Arun
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your kind replay Paul.

I am using it from Hibernate.

Actually I am working in an end stage project. So I am copy paste same code from previous hibernate codes.

In my first post I mentioned which are the model classes I am using. And this --------getHibernateTemplate().find()----------
is using for fetching data from DataBase.

-Arun
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Paul,


It is property taken from Spring itself. You are right.
But in my project I am using Struts, Spring and Hibernate. In my Hibernate class file I am using the method.

-thanks
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Spring's HibernateTemplate class has a find method that takes an HQL String and an Object array of bind parameters. The method signature shows it doesn't support generics; its an old style List.

So this:

is unsafe. Try it without the parameterized List.
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I've to check with that parameter.
any other way without passing that value?
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Arun, I have very limited knowledge about Spring framework.

Try this:

I guess the guagetypenumber should be the key element.

Also

Does this give error?
Can you not just pass the type as string instead of converting it to long?
Also are you sure that type is a number?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun R. S. Chandran:
But I've to check with that parameter.
any other way without passing that value?



Not sure what you mean by this. What I meant is this:
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

I am selecting the data from DB according to that variable "gyagetype". I meant I've to check with the guagetype to the values in my db.

Actually the table will have only one value as same as the guagetype. That I have to use for futher purpose.


 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not sure I follow what you mean.

Spring framework aside, this bit of HQL:

selects a single property from a mapped Hibernate object called Funnelguagetable where the property Funnelguagetable.guagetypenumber == the value passed. Is Funnelguagetable.guagetypenumber mapped to be of the type Long? Can we see your mapping file?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:

------------------------------------
select fgt.totalrecordpoints from Funnelguagetable fgt where fgt.guagetypenumber=?

--------------------------------------

I am selecting the the field values (Field name: totalRecordPoints) from the table( Table Name: funnelguagetable )
and that table has another field (Field name: guagetypenumber).
I wish to select totalrecordpoints(values in this field) where it's guagetypenumber=.....

This guagetypenumber is I am passing to the query:

code:
-------------------
List pointlist = getHibernateTemplate().find("select fgt.totalrecordpoints from Funnelguagetable fgt where fgt.guagetypenumber=?",new Object[] {new Long(type)})
_________________________


Curresponding SQL query in oracle and the result is:
________________________________________


SQL> select fgt.totalrecordpoints from funnelguagetable fgt where fgt.guagetypenumber=4;

TOTALRECORDPOINTS
-----------------
23

SQL>
______________________________________

Hope now you'll get what I meant.

I wish to take the value "23" for further calculations.
Thats why I used such a code.
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:

------------------------------------
select fgt.totalrecordpoints from Funnelguagetable fgt where fgt.guagetypenumber=?

--------------------------------------

I am selecting the the field values (Field name: totalRecordPoints) from the table( Table Name: funnelguagetable )
and that table has another field (Field name: guagetypenumber).
I wish to select totalrecordpoints(values in this field) where it's guagetypenumber=.....

This guagetypenumber is I am passing to the query:

code:
-------------------
List pointlist = getHibernateTemplate().find("select fgt.totalrecordpoints from Funnelguagetable fgt where fgt.guagetypenumber=?",new Object[] {new Long(type)})
_________________________


Curresponding SQL query in oracle and the result is:
________________________________________


SQL> select fgt.totalrecordpoints from funnelguagetable fgt where fgt.guagetypenumber=4;

TOTALRECORDPOINTS
-----------------
23

SQL>
______________________________________

Hope now you'll get what I meant.

I wish to take the value "23" for further calculations.
Thats why I used such a code.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And can we see your mapping file?
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:



This is the mapping file
[ December 03, 2008: Message edited by: Paul Sturrock ]
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I changed the code in this way..
Now it,s working.....
But I didn't get why the previouse code didn't work.

Code:
___________________________________

pointlist = getHibernateTemplate().find("select fgt from Funnelguagetable fgt where fgt.guagetypenumber=?",new Object[] {new Long(type)});

if(pointlist != null)
recordpoints = pointlist.get(0).getTotalrecordpoints();

____________________________________


-Thanks
ARUN




 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's because, as Paul pointed out, that the query fetches only a single field.
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Reehan,

Me also think so.

 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Hibernate terms, the original query returns an array of Objects, not a single value. Spring wraps this up into a List. Running the original query, I'd expect a List of Longs. The change Arun has made will mean this returns a List of Funnelguagetable objects. Either will work, and there is no real benefit returning only some properties of the mapped object.

Personally, I'd recommend not defining a select clause. This:

Will return the same as this:
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
_____________________

Personally, I'd recommend not defining a select clause. This:

code:


from Funnelguagetable fgt where fgt.guagetypenumber=?


Will return the same as this:

code:


select fgt from Funnelguagetable fgt where fgt.guagetypenumber=?

__________________________________________

Paul both the code you quoted here are same or not?
I think you didn't notice the code you pasted, first one is ncomplete.
Am I right?

-ARUN
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the first one is complete. I would recommend you have a read of the Hibernate documentation. It has a chapter on HQL. You should get a better understanding of the syntax after that.
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
I got it..
Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic