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

前端 未结 3 1145
鱼传尺愫
鱼传尺愫 2021-01-06 07:00

I want to return number of rows using native sql. But console says me java.math.BigInteger cannot be cast to java.lang.Long. What\'s wrong? This is my method:

3条回答
  •  独厮守ぢ
    2021-01-06 07:23

    Use BigInteger#longValue() method, instead of casting it to Long:

    return firstResult.get(0).longValue();
    

    Seems like firstResult.get(0) returns an Object. One option is to typecast to BigInteger:

    return ((BigInteger)firstResult.get(0)).longValue();
    

    But don't do this. Instead use the way provided by Nambari in comments. I'm no user of Hibernate, so I can't provide Hibernate specific solution.

提交回复
热议问题