【问题标题】:casting exception铸造异常
【发布时间】:2010-11-30 09:56:42
【问题描述】:
Set cust = customer.getCustomerBills();
Iterator<Customer> seriter = (Iterator)cust;

当我在 Set 上迭代时,我面临一个强制转换异常。

例外是:org.hibernate.collection.PersistentSet cannot be cast to java.util.Iterator。我做错了什么?

【问题讨论】:

    标签: java hibernate collections


    【解决方案1】:

    您不会将集合投射到Iterator。你得到一个:cust.iterator():

    Set<Customer> cust = customer.getCustomerBills();
    Iterator<Customer> seriter = cust.iterator();
    

    CollectionIterable,它定义了iterator() 方法。)

    【讨论】:

      【解决方案2】:

      迭代器序列 = (迭代器)cust; 不是正确的强制转换,因此会引发异常。

      使用迭代器seriter = cust.iterator();

      【讨论】:

        猜你喜欢
        • 2015-11-29
        • 2015-01-03
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多