2

I am new in hibernate and Mssql. I am working on MySQL to Mssql integration.

I am facing this exception:

java.lang.ClassCastException: java.lang.Integer cannot be cast to 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.

Thank you in advance

2
  • get rid of the cast. Commented Dec 6, 2018 at 6:08
  • i already tried to cast long but it's not working Commented Dec 6, 2018 at 6:15

2 Answers 2

0

Please, try using BigInteger.valueOf(), instead of (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. This could represent a severe security risk, allowing users to inject malicious sql queries. Instead, you should use hql parameters like the one below.

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.

Sign up to request clarification or add additional context in comments.

Comments

-2

this is because the query is returning an integer and you are casting to BigInteger. the BigInteger cannot be cast to Integer because the main class for BigInteger is of type Number. if you could cast the output to Number and from Number you can cast to BigInteger.

1 Comment

No, this would not work. A type cast of an object does not change its dynamic type. The Integer is still an Integer after the cast to Number and cannot be cast to a BigInteger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.