[JIRA] (HHH-16650) Loss in precision due usage of Float data type instead of BigDecimal in case Oracle JDBC driver returning -127 for scale and 0 for precision if precision/scale is unknown. Might happen unexpectedly and requires very close inspection of used SQL
by Ralf Grewe (JIRA)
Ralf Grewe ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... ) *commented* on HHH-16650 ( https://hibernate.atlassian.net/browse/HHH-16650?atlOrigin=eyJpIjoiNjFiYW... )
Re: Loss in precision due usage of Float data type instead of BigDecimal in case Oracle JDBC driver returning -127 for scale and 0 for precision if precision/scale is unknown. Might happen unexpectedly and requires very close inspection of used SQL ( https://hibernate.atlassian.net/browse/HHH-16650?atlOrigin=eyJpIjoiNjFiYW... )
CREATE TABLE testTable (
col1 NUMBER(38, 0)
);
INSERT INTO testTable VALUES (012345678901234567890123456789);
COMMIT;
If you run the following query (here within J2EE context, but same result if executed standalone), the returned object will be of type “BigDecimal” (as expected):
@PersistenceContext(unitName = ...)
private EntityManager em;
Query query = em.createNativeQuery("SELECT col1 FROM testTable);
Object queryResult = query.getSingleResult(); // Will be of type BigDecimal as expected
However, if you run the query mentioned in my initial post, the returned object will be of type Float, causing the value from table “testTable” to get truncated in terms of precision:
@PersistenceContext(unitName = ...)
private EntityManager em;
Query query = em.createNativeQuery("SELECT col1 FROM testTable UNION ALL SELECT 43.543 AS col1 FROM DUAL);
List<Object> queryResults = query.getResultList(); // Will be of type Float. This is kind of unexpected
You can also reproduce the issue without using a table, e.g., using the following query:
@PersistenceContext(unitName = ...)
private EntityManager em;
Query query = em.createNativeQuery("SELECT 012345678901234567890123456789 AS col1 FROM DUAL);
Object queryResult = query.getSingleResult(); // Will be of type Float. This is kind of unexpected
If seems that the Oracle JDBC driver generally returns a value of “-127” for method "getScale()" and a value of “0” for method "getPrecision()" (on “OracleResultSetMetaData”) in case the scale and the precision of a returned numeric column is unknown. In such case Hibernate chooses a data type of “Float” which may cause loss of precision.
Sorry that my initial post sound too offensive, that was not my intention. Sorry!
Bye,
Ralf
( https://hibernate.atlassian.net/browse/HHH-16650#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16650#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:9dd72a2 )
2 years, 11 months