-->
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: ClassCastException
PostPosted: Mon May 06, 2013 9:16 am 
Newbie

Joined: Mon May 06, 2013 9:06 am
Posts: 1
Hi All,

I got a problem with iterating List which got prepared from HQL.

I am querying DB on a single table mapped to very simple class.

After iterating the same list and type casting to same class during iteration I am getting ClassCastException.

Code :

import classes.HectorRequest;
import classes.EDIMigrateData;

SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();

Query qry = session.createQuery("select hr from HectorRequest hr");
List result = qry.list();

for (Iterator it = result.iterator(); it.hasNext();) {
Object o = it.next();
if(o instanceof HectorRequest)
{
HectorRequest h = (HectorRequest) o;
System.out.println("ID: " + h.getId());
}

}




I wonder here If I am typecasting to the same class it is giving ClassCastException.

if(o instanceof HectorRequest)
{
HectorRequest h = (HectorRequest) o;
System.out.println("ID: " + h.getId());
}


The control is not coming into the above if statement.

If I remove the above IF condition it is throwing java.lang.ClassCastException: HectorRequest

Below is my hibernate mapping xml for HectorRequest class.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="HectorRequest" table="hector_request">

<id name="Id" type="int">
<column name="ID" precision="5" scale="0" />
<generator class="assigned" />
</id>

<property name="DN" type="string">
<column name="ROUTER_TEL" length="25" not-null="true" />
</property>
<property name="FlagValue" type="integer">
<column name="FLAGVALUE" length="3" not-null="true" />
</property>
<property name="FlagPos" type="integer">
<column name="FLAGPOS" length="3" not-null="true" />
</property>

<property name="AccountNo" type="string">
<column name="ACCOUNTNO" length="16" not-null="true" />
</property>
<property name="CustomerIdentity" type="string">
<column name="CUSTOMERIDENTITY" length="30" not-null="true" />
</property>
<property name="CrmSource" type="string">
<column name="CRMSOURCE" length="5" not-null="true" />
</property>
<property name="DataSource" type="string">
<column name="DATASOURCE" length="5" not-null="true" />
</property>
</class>
</hibernate-mapping>


Below is my Hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@//apludc01clu20-scan-oravip.dci.bt.com:61901/C2BM2_ANY</propert
y>
<property name="hibernate.connection.username">s3</property>
<property name="hibernate.connection.password">**</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">s3</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="resources/config/hector_request.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>



Below is the output:

[Main Thread] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: select hectorrequ0_.ID as ID0_, hectorrequ0_.ROUTER_TEL as ROUTER2_0_, hectorrequ0_.FLAGVALUE as FLAGVALUE0_, hectorrequ0_.FLAGPOS as FLAGPOS0_, hectorrequ0_.ACCOUNTNO as ACCOUNTNO0_, hectorrequ0_.CUSTOMERIDENTITY as CUSTOMER6_0_, hectorrequ0_.CRMSOURCE as CRMSOURCE0_, hectorrequ0_.DATASOURCE as DATASOURCE0_ from s3.hector_request hectorrequ0_
java.lang.ClassCastException: HectorRequest
at NotifyMain1.main(NotifyMain1.java:37)



Can someone help what is missing and wrong here.


Thanks!
Krishna


Top
 Profile  
 
 Post subject: Re: ClassCastException
PostPosted: Fri May 10, 2013 3:29 am 
Beginner
Beginner

Joined: Thu May 17, 2007 9:56 am
Posts: 21
Location: India
Instead of select hr from HectorRequest hr you can try the query as from HectorRequest

You can check the below hibernate reference document section "14.6. The select clause" to understand the return types of the HQL queries.

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

_________________
Kuzhali


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.