我在迭代从 HQL 准备的 List 时遇到问题。我在映射到非常简单的类的单个表上查询数据库。
在迭代期间迭代相同的列表并将类型转换为相同的类后,我得到了 ClassCastException。
代码 :
import HectorRequest;
import 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());
}
}
我在这里想知道如果我对同一个类进行类型转换,它会给出 ClassCastException。
if(o instanceof HectorRequest) {
HectorRequest h = (HectorRequest) o;
System.out.println("ID: " + h.getId());
}
控件没有进入上述 if 语句。如果我删除上面的 IF 条件,它会抛出
java.lang.ClassCastException: HectorRequest
下面是我的类的休眠映射 xml HectorRequest
。
下面是我的 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</property><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>
下面是输出:
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)
有人可以帮助这里缺少和错误的东西。