hibernate returning BigDecimal datatype instead of long
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.