java.math.BigInteger cannot be cast to java.lang.Long

本文介绍了一种在Java中从数据库查询数据时遇到的类型转换异常,并提供了两种解决方案。异常出现在尝试将BigInteger类型直接转换为Long类型时。通过使用Hibernate的LONG类型指定返回类型或将BigInteger转换为long值来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017-03-24 14:42:27,943 ERROR [edu.ur.ir.web.action.item.GovernmentAction] 系统抛出异常!java.math.BigInteger cannot be cast to java.lang.Long

源代码:

public Long getCollectionId(Long governmentId) {
String sql = "select ir_item.crawler_configure.institutional_collection_id as id from ir_item.government  LEFT JOIN "
+ "ir_item.crawler_configure on ir_item.government.government_crawlerconfigure_id = ir_item.crawler_configure.configure_id "
+ "where ir_item.government.government_id="+governmentId;
Long id  = (Long) hbCrudDAO.getSessionFactory().getCurrentSession().createSQLQuery(sql)
.uniqueResult();
return id;

}

修改后的代码(1):

public Long getCollectionId(Long governmentId) {
String sql = "select ir_item.crawler_configure.institutional_collection_id as id from ir_item.government  LEFT JOIN "
+ "ir_item.crawler_configure on ir_item.government.government_crawlerconfigure_id = ir_item.crawler_configure.configure_id "
+ "where ir_item.government.government_id="+governmentId;
Long id  = (Long) hbCrudDAO.getSessionFactory().getCurrentSession().createSQLQuery(sql)
.addScalar("id",Hibernate.LONG)
.uniqueResult();
return id;

}



修改后的代码(2):
public Long getCollectionId(Long governmentId) {
String sql = "select ir_item.crawler_configure.institutional_collection_id as id from ir_item.government  LEFT JOIN "
+ "ir_item.crawler_configure on ir_item.government.government_crawlerconfigure_id = ir_item.crawler_configure.configure_id "
+ "where ir_item.government.government_id="+governmentId;
BigInteger id  = (BigInteger) hbCrudDAO.getSessionFactory().getCurrentSession().createSQLQuery(sql) .uniqueResult();
return id.longValue();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值