Rajat Mishra
Rajat Mishra

Reputation: 384

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.

Upvotes: 7

Views: 5292

Answers (1)

user330315
user330315

Reputation:

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.

Upvotes: 4

Related Questions