【发布时间】:2023-03-14 03:28:02
【问题描述】:
我正在使用一个名为 AudienceInfo 的 bean 对象从数据库中检索数据。 AudienceInfo 有两个属性:
public String messageBody;
public BigDecimal audienceNo;
在数据库中,messageBody 是 varchar2,audienceNo 是数字。 对于在 DAO 类中获取数据,一切正常,数据已被选中。 但是当我将对象(客户)从 Dao 类传递给我的主类时,会发生错误。这是我在主类中的代码:
Iterator<AudienceInfo> iterator = customer.iterator();
int i = 0;
while (iterator.hasNext()) {
BigDecimal com = (customer.get(i).getAudienceno());
String msgBody = customer.get(i).getMessageBody();
iterator.next();
++i;
}
错误是:
Exception in thread "main" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to com.htsc.sms.model.bo.AudienceInfo
当我在 jdbc 中使用此代码时,一切正常,我可以看到结果集,但在 hibernate 中我遇到了这个问题。
【问题讨论】:
-
旁注:为什么要使用迭代器 and 索引?这很奇怪,而且容易出错。
-
试试 BigDecimal com=(customer.get(i).getAudienceno()).getId();也许你还需要看一下 AudiencelInfo 的 ID 的类型
-
@Thomas 因为我需要显示所有列表元素
-
增强的 for 循环和 toString 应该可以解决您的问题。
-
@gefei 正如我之前在 oracle 数据库中写的那样,它是数字,在 bean 类中它是 BigDecimal。我试过你的建议,没有任何反应
标签: java hibernate jakarta-ee hibernate-mapping