简体   繁体   English

java.lang.Integer不能强制转换为java.math.BigInteger

[英]java.lang.Integer cannot be cast to java.math.BigInteger

I am new in hibernate and Mssql. 我是休眠和Mssql的新手。 I am working on MySQL to Mssql integration. 我正在将MySQL与Mssql集成。

I am facing this exception: 我面临这个例外:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger

My code is: 我的代码是:

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();

This is my code I am stuck here previously this code working in MySQL but now when I integrate in Mssql it shows that exception. 这是我的代码,以前我在这里停留在MySQL上,但是现在当我集成到Mssql中时,它显示了该异常。

Thank you in advance 先感谢您

this is because the query is returning an integer and you are casting to BigInteger. 这是因为查询返回一个整数,而您正在转换为BigInteger。 the BigInteger cannot be cast to Integer because the main class for BigInteger is of type Number. BigInteger不能转换为Integer,因为BigInteger的主类是Number类型。 if you could cast the output to Number and from Number you can cast to BigInteger. 如果您可以将输出转换为Number,然后从Number转换为BigInteger。

Please, try using BigInteger.valueOf() , instead of (BigInteger) . 请尝试使用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();

Also, you should change they way you are setting parameters in your HQL. 另外,您应该更改它们的方式,以便在HQL中设置参数。 This could represent a severe security risk, allowing users to inject malicious sql queries. 这可能会带来严重的安全风险,从而使用户可以注入恶意sql查询。 Instead, you should use hql parameters like the one below. 相反,您应该使用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());

You can read more in the following link . 您可以在以下链接中阅读更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.math.BigInteger 不能转换为 java.lang.Integer - java.math.BigInteger cannot be cast to java.lang.Integer 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer? - How to work around java.math.BigInteger cannot be cast to java.lang.Integer? 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer - How to work around java.math.BigInteger cannot be cast to java.lang.Integer Playorm java.lang.Integer无法强制转换为java.math.BigInteger - Playorm java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能强制转换为 java.lang.Integer,ZAC5C24B64B4BAC83 - java.math.BigInteger cannot be cast to java.lang.Integer , sql Dialect is not configured java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger HTTP状态500 - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger HTTP Status 500 java.math.BigInteger 无法转换为 java.lang.Long - java.math.BigInteger cannot be cast to java.lang.Long java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long java.math.BigInteger无法强制转换为java.math.BigDecimal - java.math.BigInteger cannot be cast to java.math.BigDecimal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM