From 2fb7cdd08bc220b726de6fbe882787bbf04441a5 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Mon, 12 Jun 2023 18:49:45 +0200 Subject: [PATCH] HHH-16697 Fix auto type discovery for aggregate functions on Oracle --- .../dialect/OracleLegacyDialect.java | 2 +- .../org/hibernate/dialect/OracleDialect.java | 2 +- .../procedure/OracleStoredProcedureTest.java | 5 +++-- .../test/sql/OracleStoredProcedureTest.java | 5 +++-- .../sql/autodiscovery/AutoDiscoveryTest.java | 21 +++++++++++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java index b8b16e085c..7669c482ae 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java @@ -715,7 +715,7 @@ public class OracleLegacyDialect extends Dialect { } //intentional fall-through: case Types.DECIMAL: - if ( scale == 0 ) { + if ( scale == 0 && precision != 0 ) { // Don't infer TINYINT or SMALLINT on Oracle, since the // range of values of a NUMBER(3,0) or NUMBER(5,0) just // doesn't really match naturally. diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java index 97e0d1a0e9..56fa913516 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java @@ -767,7 +767,7 @@ public class OracleDialect extends Dialect { } //intentional fall-through: case DECIMAL: - if ( scale == 0 ) { + if ( scale == 0 && precision != 0 ) { // Don't infer TINYINT or SMALLINT on Oracle, since the // range of values of a NUMBER(3,0) or NUMBER(5,0) just // doesn't really match naturally. diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/OracleStoredProcedureTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/OracleStoredProcedureTest.java index 92f1f2baf1..bde629832a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/OracleStoredProcedureTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/procedure/OracleStoredProcedureTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.test.procedure; +import java.math.BigDecimal; import java.sql.CallableStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -187,11 +188,11 @@ public class OracleStoredProcedureTest { public void testStoredProcedureReturnValue(EntityManagerFactoryScope scope) { scope.inTransaction( entityManager -> { - Integer phoneCount = (Integer) entityManager + BigDecimal phoneCount = (BigDecimal) entityManager .createNativeQuery( "SELECT fn_count_phones(:personId) FROM DUAL" ) .setParameter( "personId", person1.getId() ) .getSingleResult(); - assertEquals( 2, phoneCount ); + assertEquals( BigDecimal.valueOf( 2 ), phoneCount ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/OracleStoredProcedureTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/OracleStoredProcedureTest.java index f241707ecd..b353225f63 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/OracleStoredProcedureTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/OracleStoredProcedureTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.test.sql; +import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDateTime; @@ -227,11 +228,11 @@ public class OracleStoredProcedureTest { @Test public void testStoredProcedureReturnValue(EntityManagerFactoryScope scope) { scope.inTransaction( entityManager -> { - Integer phoneCount = (Integer) entityManager + BigDecimal phoneCount = (BigDecimal) entityManager .createNativeQuery( "SELECT fn_count_phones(:personId) FROM DUAL" ) .setParameter( "personId", 1 ) .getSingleResult(); - assertEquals( 2, phoneCount ); + assertEquals( BigDecimal.valueOf( 2L ), phoneCount ); } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/autodiscovery/AutoDiscoveryTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/autodiscovery/AutoDiscoveryTest.java index d6af4c981d..8c8706d4a3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/autodiscovery/AutoDiscoveryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/autodiscovery/AutoDiscoveryTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.test.sql.autodiscovery; +import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -20,6 +21,7 @@ import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jdbc.Work; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.testing.orm.junit.JiraKey; import org.junit.Assert; import org.junit.Test; @@ -137,4 +139,23 @@ public class AutoDiscoveryTest extends BaseCoreFunctionalTestCase { session.getTransaction().commit(); session.close(); } + + @Test + @JiraKey( "HHH-16697" ) + public void testAggregateQueryAutoDiscovery() { + Session session = openSession(); + session.beginTransaction(); + User u = new User( "steve" ); + session.persist( u ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.beginTransaction(); + List result = session.createNativeQuery( "select sum(39.74) from t_user u" ).list(); + Assert.assertEquals( new BigDecimal( "39.74" ), result.get( 0 ) ); + session.remove( u ); + session.getTransaction().commit(); + session.close(); + } }