-->
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: ClassCast Exception
PostPosted: Wed May 31, 2006 10:31 am 
Newbie

Joined: Wed May 31, 2006 9:49 am
Posts: 3
Location: Bangalore
Hi All,
I am getting ClassCastException while Iterating the List.
I am retrieving the data from two tables(Parent and Child) using Primary key. Can anyone tell me that after iterating list in which object i should cast. Thanx in Advance.

I am sending the details:

Hibernate version:3.0

Mapping documents:Parent Table
<hibernate-mapping>
<class name="com.coassectt.dak.business.vo.CreateDakVO" table="dakdetails">
<id name="dakid" column="DAK_ID" type="integer" >
<generator class="native" />
</id>
<property name="subject" type="string">
<column name="DAK_SUBJECT" length="100"/>
</property>
<property name="dateDueBy" type="date">
<column name="DAK__DATE_DUEBY" length="20"/>
</property>
<set name="dakEdit" inverse="true" cascade="all" >
<key>
<column name="DAK_ID" not-null="true" />
</key>
<one-to-many class="com.coassectt.dak.business.vo.EditDakVO" />
</set>
</class>


</hibernate-mapping>

Child Table:
<hibernate-mapping>
<class name="com.coassectt.dak.business.vo.EditDakVO" table="EDITDAK">
<id name="dakEditId" column="DAK_EDIT_ID" type="integer" >
<generator class="native" />
</id>
<property name="department" type="string">
<column name="DE_FORWARDED_TO" length="40"/>
</property>
<property name="fwdDate" type="date">
<column name="DE_FORWARDED_DATE" length="20"/>
</property>
<property name="remarks" type="string">
<column name="DE_COAS_REMARKS" length="255"/>
</property>
<property name="dateDueBy" type="date">
<column name="DE_DATE_DUEBY" length="20"/>
</property>
<many-to-one name="createDak" class="com.coassectt.dak.business.vo.CreateDakVO" >
<column name="DAK_ID" not-null="true" />
</many-to-one>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
List dayendReptList=null;
Date today=new Date();
today.setDate(today.getDate()+5);
StringBuffer query = new StringBuffer();
query.append("select editvo.department,editvo.remarks,createVO.subject from com.coassectt.dak.business.vo.CreateDakVO createVO, com.coassectt.dak.business.vo.EditDakVO editvo where createVO.dakid = editvo.createDak.dakid").append(" and ").append("editvo.dateDueBy = '").append(DateUtil.convertDate(DateUtil.formatDate(today))).append("'");
dayendReptList = session.createQuery(query.toString()).list();
Iterator itr = dayendReptList.iterator();
while(itr.hasNext()){
// System.out.println("Class name"+itr.next().getClass().getName());
EditDakVO editDakVO=(EditDakVO)itr.next();//Here it is giving ClasscastException because hql is using two VOs.

Exception :java.lang.ClassCastException
at com.coassectt.dak.webcontent.tags.SearchDayEndReportTag.doStartTag(Unknown
at org.apache.jsp.Manage_005fDAK_005fDayEndRept_005fSearch_jsp._jspx_meth_dept
at org.apache.jsp.Manage_005fDAK_005fDayEndRept_005fSearch_jsp._jspx_meth_html
at org.apache.jsp.Manage_005fDAK_005fDayEndRept_005fSearch_jsp._jspService(org
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)


Name and version of the database you are using:My SQL

The generated SQL (show_sql=true): select editdakvo1_.DE_FORWARDED_DATE as col_0_0_, editdakvo1_.DE_FORWARDED_TO as col_1_0_, editdakvo1_.DE_COAS_REMARKS as col_2_0_, createdakv0_.DAK_SUBJECT as col_3_0_ from dakdetails createdakv0_, EDITDAK editdakvo1_ where (createdakv0_.DAK_ID=editdakvo1_.DAK_ID and editdakvo1_.DE_DATE_DUEBY='2006-06-05')

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 11:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you do "select a, b, c from ..." in HQL, you get back a list of Object[3]. So try this:
Code:
Iterator itr = dayendReptList.iterator();
while(itr.hasNext()){
  Object[] ret = (Object[]) itr.next();
  String department = (String) ret[0];
  String remarks = (String) ret[1];
  String subject = (String) ret[2];
  ...
}
You might consider using parameters instead of building that HQL string. Also, posting with code tags is good. Switching to java5 wouldn't hurt either, though I suppose you could hold off for java6 at this point.. it's pretty close to release.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 1:38 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
I'm having a similar issue. Is there a way to get a transient instance of the object, or just an array of it's fields?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:47 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's a separate issue. There are two ways to get a transient instance of an entity you've loaded from the DB. You can either session.evict() it (making it detached) then set its ID to whatever the mapping's unsaved-value is, or you can use HQL's "select new" syntax (see the refdocs, section 14.5, "The select clause").

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 5:59 pm 
Beginner
Beginner

Joined: Wed Jun 07, 2006 12:28 pm
Posts: 22
I realized this was a different issue so I created a different thread. As you can see over there, the fundamental issue is that the proxy (com.monkeyden.pipeline.poc.bo.User$$EnhancerByCGLIB$$71e1d5b6) isn't castable to the true type (com.monkeyden.pipeline.poc.bo.User). Thanks for responding.

http://forum.hibernate.org/viewtopic.php?t=960477


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.