简体   繁体   中英

Hibernate Query convert count to int?

I'm getting this error:

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer

My idea is to check the count value of a select, but I'm having trouble to convert it.

attaching the code below.

Any ideas?

public List<Map<String, Object>> contaQuery(String sqlCount) throws MensagemException {

    Session session = HibernateUtil.getSession();
    try {

        String sql = sqlCount;
        int query = ((Integer) session.createSQLQuery("select count(*) from (" + sqlCount + ") as subquery")
                .uniqueResult());

        if (query <= 50000) {

            return rodarQuery(sql);
        } else {

            throw new MensagemException("Too many Registers");

        }

    } finally {
        session.close();

    }

}

As stated in the Farlan comment, convert the BigInteger return value from the uniqueResult to an Integer by calling intValue()

final Query query;
final BigInteger bigResult;
final int result;

query = session.createSQLQuery("select count(*) from (" + sqlCount + ") as subquery");
bigResult = (BigInteger)query.uniqueResult();

if (bigResult != null)
{
    result = bigResult.intValue();
}
else
{
    result = ...some value of your choosing, perhaps 0.
)

if (result < 50000)
... stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM