sql - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long -
i want return number of rows using native sql. console says me java.math.biginteger cannot cast java.lang.long
. what's wrong? method:
public long getnumrows(integer id){ session session = null; session = this.sessionfactory.getcurrentsession(); query query = session .createsqlquery("select count(*) controllnews news_id=" + id + ";"); list firstresult = query.list(); return (long) firstresult.get(0); }
use biginteger#longvalue()
method, instead of casting long
:
return firstresult.get(0).longvalue();
seems firstresult.get(0)
returns object
. 1 option typecast biginteger
:
return ((biginteger)firstresult.get(0)).longvalue();
but don't this. instead use way provided nambari in comments. i'm no user of hibernate, can't provide hibernate specific solution.
Comments
Post a Comment