-->
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.  [ 13 posts ] 
Author Message
 Post subject: class cast exception while iterating hql query result
PostPosted: Mon Aug 31, 2009 2:03 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
Code:
List idList=getHibernateTemplate.find(select co.englishname,fo.englishname from Company co,Founder fo where co.companyid=fo.companyid);

while iterateing the result it shows class cast exception
please help me to get this resolved



Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Mon Aug 31, 2009 7:21 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
A little little little bit of code would be really great.


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Mon Aug 31, 2009 2:01 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Log the class type and find out what the incoming class is.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 3:53 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
result are coming from two pojo and we are not able to get where should we place the return type


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 3:59 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
It's obvious that the results are coming from your two POJOs Company and Founder but you only get two properties. So I think you get an array with two Strings.
To check that log the type of each element that is in your idList. If it is an array iterate the array and log the type of each field. Then check your casts if you expect another type that...


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 4:37 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
below is the code , please advice me where i am doing wrong

List getrecordsList4=new ArrayList();
List mylist=new ArrayList();

getrecordsList4 = getHibernateTemplate().find("select co.chinesename fo.englishname from Company as co , founder as fo where co.companyid=fo.companyid);


if (getrecordsList4 != null && getrecordsList4.size() > 0)
{

Iterator itr = getrecordsList4.iterator(); // this line shows class cast exception
while (itr.hasNext())
{
Company company = (Company)itr.next();
CompanyDTO companyDTO = new CompanyDTO();
companyDTO.setChinesename(company.getChinesename());
mylist.add(companyDTO);

}

return mylist;
}


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 5:06 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
it is showing the same error in the below (this time i am using one pojo)

getrecordsList4 = getHibernateTemplate().find("select co.chinesename from Company as co where co.companyid=2"");


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 5:26 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
I cannot believe that the line you placed the comment in throws the exception. The post the stacktrace of the exception and the complete class so we can compare the linenumbers.

This line is wrong: Company company = (Company)itr.next();
The next element from the iterator is not a company. Even in your last post you do NOT receive an instance of Company. You get the type of the field "chinesename" (I think a String.)


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 10:24 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
thanks CDillinger

as i changed the this code String ss = (String)itr.next();

getHibernateTemplate().find("select co.chinesename from Company as co where co.companyid=2"); this query is running fine now

but when i use two pojos it gives me below exception

HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: [Ljava.lang.Object; cannot be cast to java.lang.String
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


root cause

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
com.uhc.add.actions.CompanyAction.getrecordsList4(CompanyAction.java:473)
com.uhc.add.actions.CompanyAction.execute(CompanyAction.java:288)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Tue Sep 01, 2009 12:03 pm 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
i have tried below , it is not showing an exception but there is no output

List getrecordsList4=new ArrayList();
getrecordsList4 =getHibernateTemplate().find("select co.chinesename,fo.fenglishname from Company as co,Founder as fo where co.companyid=fo.companyid and co.companyid=1");

if (getrecordsList4 != null && getrecordsList4.size() > 0)
{
Iterator itr = getrecordsList4.iterator();
while (itr.hasNext())
{
Object object= itr.next();
if (object instanceof Company)
{
Company company=(Company)object;
company.getChinesename();
}

}


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Wed Sep 02, 2009 12:48 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
i have log the class type also it is rsulting the array of Object []


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Wed Sep 02, 2009 2:43 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
correct me if i m wrong

"select co.englishname,fo.englishname from Company co,Founder fo where co.companyid=fo.companyid"

"select new NewClass(co.englishname,fo.englishname) from Company co,Founder fo where co.companyid=fo.companyid"

now retrieve the object of NewClass.
NewClass must have same constuctor.

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: class cast exception while iterating hql query result
PostPosted: Wed Sep 02, 2009 10:33 am 
Newbie

Joined: Mon Aug 31, 2009 1:45 am
Posts: 12
Great thanks Tarun

my qyery is running absolutely fine with your suggestions.


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