简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM