Hibernate 类转换异常 [英] Hibernate Class Cast Exception

查看:26
本文介绍了Hibernate 类转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将结果集强制转换为映射类时,我遇到了一个带有休眠的类强制转换异常......我能够看到返回的结果集中的数据......但是它作为一个对象返回[] 并且我能够将对象 [] 设置为列表...我是否正确地进行了休眠映射?我从查询中得到了正确的数据,但它没有正确映射...

I am getting a class cast exception with hibernate when trying to cast the result set to the mapping class... I am able to see the data in the result set that is returned...however it comes back as an Object [] and I am able to set the Object [] to the a List...Am I doing my hibernate mappings correctly? I get the correct data back from the query but its not mapping correctly...

映射

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
        <property name="hibernate.connection.url">jdbc:db2://****</property>
        <property name="hibernate.connection.username">*****</property>
        <property name="hibernate.connection.password">*****</property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">NONE</property>
        <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <mapping package="db2"/>
        <mapping class="db2.EQP_UT"/>
        <mapping class="db2.EQP_COMP_PRIMARY"/>
    </session-factory>
</hibernate-configuration>

映射类

@Entity
@Table(name = "EQP_UT")
public class EQP_UT implements Serializable {

    @Id
    private String INITIAL;

    @Id
    private String NUMBER;
    private Integer AXL_CNT;
    private String EQP_TYP_CD;
    private String EIN;
    private Integer TRUK_CNT;

    @OneToOne
    @JoinTable(name = "EQP_COMP_PRIMARY",
        joinColumns = {@JoinColumn(name = "INITIAL"), @JoinColumn(name = "NUMBER")},
         inverseJoinColumns = {
            @JoinColumn(name = "INITIAL"), @JoinColumn(name="NUMBER")
            }  
    )
    public EQP_COMP_PRIMARY eqp_comp;

    public EQP_UT(){
        this.INITIAL = "";
        this.NUMBER = " ";
        int a = this.retrieve();
        System.out.println("This is the data is here..." + a);
    }

    public String getNumber()
    {
        return NUMBER;
    }

    public void setNumber(String num)
    {
        this.NUMBER = num;
    }

    public String getInitial()
    {
        return INITIAL;
    }

    public void setInitial(String init)
    {
        this.INITIAL = init;
    }

    public int retrieve()
    {
        Session session = null;
        Transaction transaction = null;

        try{
            session = HibernateUtil.getDB2SessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            String init = "AARE";
            String number = String.format("%010d", 9350);
            Integer num = Integer.parseInt(number);

            String queryString = "SELECT A.INITIAL, A.NUMBER, " + 
                    "A.TRUK_CNT, A.EQP_TYP_CD, A.AXL_CNT, " +
                    "B.TRUK_AXL_CNT, A.EIN FROM EQP_UT AS A " +
                    "LEFT JOIN A.eqp_comp AS B " +
                    "WHERE A.INITIAL||A.NUMBER IN (:carList) AND A.INITIAL IN (:initList) AND A.NUMBER IN (:numberList) " + 
                    "AND B.TRUK_AXL_CNT > 0";

            Query query = session.createQuery(queryString);
            query.setParameterList("carList", Globals.returnCarList());
            query.setParameterList("initList", Globals.returnCarListCarInits());
            query.setParameterList("numberList", Globals.returnCarListCarNumbers());

            @SuppressWarnings("unchecked")
            List obj =  query.list();

            System.err.println("CLASS NAME IS " + obj.get(0).getClass().getClass().getName());

            Globals.eqpList = obj; //Globals.eqpList is List<EQP_UT>

            session.getTransaction().commit(); 

            return 10;      
        }
        catch(HibernateException ex)
        {
            ex.printStackTrace();
            transaction.rollback();

        }
        catch(Exception ex)
        {
            System.err.println(ex.getMessage());
        }

        return -1;  
    }   
}

加入班级

@Entity
@Table(name = "EQP_COMP_PRIMARY")
public class EQP_COMP_PRIMARY implements Serializable{

        private Integer TRUK_AXL_CNT;

        @Id
        private String INITIAL;
        @Id
        private String NUMBER;
}

主要

//tons more code...
for(int i = 0; i < Globals.eqpList.size(); i++)
{
     EQP_UT e = Globals.eqpList.get(i); //class cast exception - and Globals.eqpList is List<EQP_UT>
}
//tons more code...

推荐答案

对于测试,我建议您在产生类强制转换异常的语句周围放置一个 try-catch 子句,在 catch 块中设置一个断点并查看哪个将第 i 个元素归类.

For tests I recommend you to put a try-catch clause around the statement which is producing the class cast exception, set a breakpoint in the catch block and look which class the i-th element really is.

针对您的问题:

您正在使用 HQL SELECT 语句.带有该语句的查询返回一个列表,但该列表的元素不一定是 EQP_TU 的实例;它们也可以是一个对象数组.

You are using the HQL SELECT statement. The query with this statement returns a list, but the elements of the list are not necessarily instances of EQP_TU; they also can be an array of object.

适合您的解决方案:

使用FROM 语句代替SELECT 语句.在您的代码中:

Use the FROM statement instead of the SELECT statement. In your code:

String queryString = "FROM EQP_UT AS A " +
                "LEFT JOIN A.eqp_comp AS B " +
                "WHERE A.INITIAL||A.NUMBER IN (:carList) AND A.INITIAL IN (:initList) AND A.NUMBER IN (:numberList) " + 
                "AND B.TRUK_AXL_CNT > 0";

然后您可以确保获得一个包含您在 FROM 之后提到的类的实例的列表(代码中的 EQP_UT).

Then you can be sure to get a list with instances of the class you mentioned after FROM (EQP_UT in your code).

这篇关于Hibernate 类转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆