Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.math.BigInteger cannot be cast to java.math.BigDecimal

We are migrating from Oracle to PostgreSQL. Some queries that were specific to Oracle had to be changed to Postgres equivalents. Following is one such commit:

Initially,

Query query = getEntityManager().createNativeQuery("SELECT PC_SITE_GROUP_ID_SEQ.NEXTVAL from DUAL");
BigDecimal result = (BigDecimal) query.getSingleResult();

was changed to,

Query query = getEntityManager().createNativeQuery("SELECT NEXTVAL('pc_site_group_id_seq')");
BigDecimal result = (BigDecimal) query.getSingleResult();

On running, after the change, it threw error,

java.math.BigInteger cannot be cast to java.math.BigDecimal

My question is why it was working before? Just a reminder, the first change was run on Oracle database while the second one on Postgres database. Please help.

like image 498
Rajat Mishra Avatar asked Jan 23 '18 09:01

Rajat Mishra


1 Answers

Oracle has no "integer" data type in the database so everything is mapped to BigDecimal by the driver.

In Postgres there is a clear distinction between an integer (or bigint) and a decimal, so the driver returns a BigInteger for bigint values.

And a sequence always emits bigint values, so the driver returns a BigInteger for them.

like image 148
a_horse_with_no_name Avatar answered Sep 28 '22 23:09

a_horse_with_no_name