Hibernate returns BigIntegers instead of longs

蹲街弑〆低调 提交于 2019-12-01 03:03:41

The default for ".list()" in hibernate appears to be BigInteger return types for Numeric. Here's one work around:

session.createSQLQuery("select column as num from table")
  .addScalar("num", StandardBasicTypes.LONG).list();

In older versions of Hibernate you can use

  session.createSQLQuery("select column as num from table")
 .addScalar("num", Hibernate.LONG).list();

Adding to #Hedley comment to fix it globally you can add a line in SQLDialect constructor. In my project it was like:

public PostgreSQLDialect() {
        super();
        registerHibernateType(Types.BIGINT, StandardBasicTypes.LONG.getName());
    }
erencan

Object database mapping is wrong. There is a casting exception here saying database field is BigInteger, but object property is long.

BigInteger is a special class to hold unlimited size integer values. Furthermore, BigInteger can not cast to long implicitly.

To avoid this error database field which is BigInteger should be change to long compatible type. Change it to a int type where int can be casted to long implicitly. See BigInteger.

you can check on following link check here for BigInteger identity generator class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!