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
|