HHH-16697 Fix auto type discovery for aggregate functions on Oracle

This commit is contained in:
Christian Beikov 2023-06-12 18:49:45 +02:00
parent 1a9732a5c2
commit 2fb7cdd08b
5 changed files with 29 additions and 6 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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 );
}
);
}

View File

@ -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 );
} );
}
}

View File

@ -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<Object> 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();
}
}