hibernate returning BigDecimal datatype instead of long

*爱你&永不变心* 提交于 2019-12-05 04:44:28

Oracle NUMBER maps to BigDecimal in Hibernate by default. Try setting the type to BigDecimal.

I don't know what the issue is with your hibernate configuration but as a workaround, one trick that allows you not to care what java Number type a Hibernate query returns is to cast the returned value to Number and call .longValue():

long id = ((Number) em.createNativeQuery("select my_seq.nextVal from dual")
              .getSingleResult())
          .longValue();

That way you don't care if the query returns Long, BigDecimal, BigInteger, Short as long as it fits into a java long.

Yes, Hibernate Maps Oracle Number Type to BigDecimal in Java. Since BigDecimal computations are costly in Java, once You get the BigDecimal in Java, write a Utility to convert the BigDecimal Collection to Integer or Long collection, based on Your data size.

Since Jpa/Hibernate by default maps the Number datatype to BigDecimal, you can not change this behavior.

You can handle the conversion in the service methods you have. Convert BigDecimal to int/long based on what the business case needs using bigDecimal.intValue() or bigDeciml.intValueExact() or bigDecimal.longValue()

I had the same problem. This fixes the issue neatly and returns a List of Long

List<Long> orgTypeIds = session.createSQLQuery("SELECT typeId FROM org_type_cd")
                               .addScalar("typeId", StandardBasicTypes.LONG)
                               .list();

ref: https://matthewbusche.com/2016/06/08/hibernate-returning-bigdecimal-instead-of-long/

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