铸造例外

sin*_*ngh 0 java collections hibernate

Set cust = customer.getCustomerBills();
Iterator<Customer> seriter = (Iterator)cust;
Run Code Online (Sandbox Code Playgroud)

当我迭代Set时,我正面临一个转换异常.

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

Boz*_*zho 10

您没有将集合转换为Iterator.你得到一个cust.iterator()::

Set<Customer> cust = customer.getCustomerBills();
Iterator<Customer> seriter = cust.iterator();
Run Code Online (Sandbox Code Playgroud)

(A CollectionIterable,定义iterator()方法.)