java - java.lang.Integer 不能转换为 java.math.BigInteger
问题描述
我是hibernate和Mssql的新手。我正在研究 MySQL 到 Mssql 的集成。
我正面临这个例外:
java.lang.ClassCastException:java.lang.Integer 无法转换为 java.math.BigInteger
我的代码是:
long salesAlertsCount = ((BigInteger)HibernateUtil.getHibernateSession()
.createSQLQuery("SELECT COUNT(a.id) FROM Activity as a,Lead as l WHERE a.what_id=l.id and l.deleted=0 and "
+ (currentUser.isAdmin() ? "a.tenant_id="+currentUser.getTenant_id():" (a.owner_id="+currentUser.getId()
+ " or a.createdBy_id="+currentUser.getId()+")")
+ " and "
+ (currentUser.isAdmin()?"l.tenant_id="+currentUser.getTenant_id():" (l.owner_id="+currentUser.getId()+")")
+ " and a.deleted=0 and a.action="
+ Constants.ACTIVITY_ACTION_SEND_SALES_ALERT
+ ""
+ " and a.viewed=0 AND a.created>='"
+ beginingOfMonth
+ "' ").uniqueResult()).longValue();
这是我的代码,我之前被困在这里,这段代码在 MySQL 中工作,但现在当我集成到 Mssql 中时,它显示了该异常。
先感谢您
解决方案
请尝试使用BigInteger.valueOf()
, 而不是(BigInteger)
.
long salesAlertsCount = BigInteger.valueOf(HibernateUtil.getHibernateSession().createSQLQuery("SELECT COUNT(a.id) FROM Activity as a,Lead as l WHERE a.what_id=l.id and l.deleted=0 and "+(currentUser.isAdmin()?"a.tenant_id="+currentUser.getTenant_id() +" (a.owner_id="+currentUser.getId() +" or a.createdBy_id="+currentUser.getId()+")")+" and "+(currentUser.isAdmin()?"l.tenant_id="+currentUser.getTenant_id():" (l.owner_id="+currentUser.getId()+")")+" and a.deleted=0 and a.action="+Constants.ACTIVITY_ACTION_SEND_SALES_ALERT+"" + " and a.viewed=0 AND a.created>='"+beginingOfMonth+"' ").uniqueResult()).longValue();
此外,您应该更改在 HQL 中设置参数的方式。这可能代表严重的安全风险,允许用户注入恶意 sql 查询。相反,您应该使用如下所示的 hql 参数。
Query query = HibernateUtil.getHibernateSession().createQuery("SELECT count(a.id) FROM Activity a, Lead l where a.what_id = l.id and a.owner_id = :owner_id"); \\ Add the rest of the code
query.setParameter("owner_id", currentUser.getId());
您可以在以下链接中阅读更多内容。
推荐阅读
- angular - 通信 - 具有多个父组件的子组件
- python - 如何使用 Flask 将数据库记录呈现为 HTML?
- reactjs - 在“Connect(App)”的上下文中找不到“store”
- java - 使用多模块 Maven 项目时 Eclipse 的类路径问题
- java - 将 JTable 中的数据导出为 PDF 格式
- google-apps-script - 我需要一个优雅的解决方案来格式化比赛时间
- c# - 如果与 Task.WhenAll() 一起使用,键入的 http 客户端会引发套接字异常
- batch-file - 从文本文件中提取部分句子
- ansible - 我可以在 ansible 中设置一个任务以仅在文件夹存在的情况下运行吗?
- cygwin - 关于 Cygwin Windows 编译问题的 Wine