java - 类型转换异常

标签 java hibernate collections

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

当我迭代 Set 时遇到强制转换异常。

异常(exception)是:org.hibernate.collection.PersistentSet 无法转换为 java.util.Iterator。我做错了什么?

最佳答案

您不会将集合强制转换为Iterator。您获得一个:cust.iterator():

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

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

关于java - 类型转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4312389/

相关文章:

java - 为什么 TreeMap 中不允许空键?

java - Java 的泛型是如何工作的?

java - RecyclerView项目的图像错误

java - android java Map与自定义BiFunction合并

java - 当你可以只使用 object.variable 名称来获取或设置时,为什么还要使用 set 和 get 方法?

java - 如何在Hibernate中将主键从主表复制到子表

mysql - 插入日期字段mysql、struts2 spring hibernate

java - 简单的 GUI Java 计算器

java - findAll() 在 SpringBoot Rest MySql CRUD 操作应用程序中不起作用

java - 从java 8流中获取N(3)个列表(基于ENUM性别(男,女,其他))并基于ENUM过滤条件