2010-11-30 145 views
0
Set cust = customer.getCustomerBills(); 
Iterator<Customer> seriter = (Iterator)cust; 

我在Set上迭代時遇到了鑄造異常。鑄造異常

例外是:org.hibernate.collection.PersistentSet cannot be cast to java.util.Iterator。我究竟做錯了什麼?

回答

10

您不會將集合投射到Iterator。你獲得一個:cust.iterator()

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

(甲CollectionIterable,它定義了iterator()方法)

0

迭代seriter =(迭代)CUST; 不是一個適當的投射,因此拋出異常。

use Iterator seriter = cust.iterator();