From daef4b7f08f518b075deea61b6a0761da44fb414 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Mon, 9 Jul 2018 13:27:55 +0200 Subject: [PATCH] Revert "HHH-12670 - Allows native SQL queries that take a given resultClass to map the result set to the required type" This reverts commit 9fac6747ef22acff9c7350efca33286452a2c967. --- .../AbstractSharedSessionContract.java | 30 +------- .../query/ScalarResultNativeQueryTest.java | 77 ++++--------------- 2 files changed, 18 insertions(+), 89 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index ea3c5925e2..ac3d5da8ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; -import java.lang.reflect.Array; import java.sql.SQLException; import java.util.List; import java.util.TimeZone; @@ -81,7 +80,6 @@ import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl; import org.hibernate.resource.transaction.spi.TransactionCoordinator; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; -import org.hibernate.transform.BasicTransformerAdapter; import org.hibernate.type.Type; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; @@ -934,38 +932,12 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont } private void handleNativeQueryResult(NativeQueryImplementor query, Class resultClass) { - boolean isObjectArray = Object[].class.equals( resultClass ); - if ( Tuple.class.equals( resultClass ) ) { query.setResultTransformer( new NativeQueryTupleTransformer() ); } - else if ( resultClass.isArray() && !isObjectArray ) { - Class elementClass = resultClass.getComponentType(); - - query.setResultTransformer( new BasicTransformerAdapter() { - @Override - public Object transformTuple(Object[] tuple, String[] aliases) { - Object[] result = (Object[]) Array.newInstance( elementClass, tuple.length ); - for ( int i = 0; i < tuple.length; i++ ) { - result[i] = elementClass.cast( tuple[i] ); - } - return result; - } - } ); - } - else if ( this.getFactory().getMetamodel().getEntities() - .stream() - .anyMatch( entityType -> entityType.getJavaType().isAssignableFrom( resultClass ) ) ) { + else { query.addEntity( "alias1", resultClass.getName(), LockMode.READ ); } - else if ( !isObjectArray ) { - query.setResultTransformer( new BasicTransformerAdapter() { - @Override - public Object transformTuple(Object[] tuple, String[] aliases) { - return resultClass.cast( tuple[0] ); - } - } ); - } } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ScalarResultNativeQueryTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ScalarResultNativeQueryTest.java index 8f4fb20fc9..6e7e18432a 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ScalarResultNativeQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/ScalarResultNativeQueryTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.jpa.test.query; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertEquals; import java.util.List; @@ -22,9 +21,7 @@ import javax.persistence.Table; import org.hibernate.dialect.H2Dialect; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.TestForIssue; import org.junit.Test; /** @@ -64,64 +61,24 @@ public class ScalarResultNativeQueryTest extends BaseEntityManagerFunctionalTest @Test public void shouldApplyConfiguredTypeForProjectionOfScalarValue() { - doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.persist( new Person( 1, 29 ) ); - } ); + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.persist( new Person( 1, 29 ) ); + em.getTransaction().commit(); + em.close(); - doInJPA( this::entityManagerFactory, entityManager -> { - List results = entityManager.createNamedQuery( "personAge", String.class ).getResultList(); - assertEquals( 1, results.size() ); - assertEquals( "29", results.get( 0 ) ); - } ); - } + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + List results = em.createNamedQuery( "personAge", String.class ).getResultList(); + assertEquals( 1, results.size() ); + assertEquals( "29", results.get( 0 ) ); + em.getTransaction().commit(); + em.close(); - @Test - @TestForIssue( jiraKey = "HHH-12670" ) - public void testNativeSQLWithExplicitScalarMapping() { - doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.persist( new Person( 1, 29 ) ); - } ); - - doInJPA( this::entityManagerFactory, entityManager -> { - List results = entityManager.createNativeQuery( - "select p.age from person p", Integer.class ) - .getResultList(); - assertEquals( 1, results.size() ); - assertEquals( Integer.valueOf( 29 ), results.get( 0 ) ); - } ); - } - - @Test - @TestForIssue( jiraKey = "HHH-12670" ) - public void testNativeSQLWithExplicitTypedArrayMapping() { - doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.persist( new Person( 1, 29 ) ); - } ); - - doInJPA( this::entityManagerFactory, entityManager -> { - List results = entityManager.createNativeQuery( - "select p.id, p.age from person p", Integer[].class ) - .getResultList(); - assertEquals( 1, results.size() ); - assertEquals( Integer.valueOf( 1 ), results.get( 0 )[0] ); - assertEquals( Integer.valueOf( 29 ), results.get( 0 )[1] ); - } ); - } - - @Test - @TestForIssue( jiraKey = "HHH-12670" ) - public void testNativeSQLWithObjectArrayMapping() { - doInJPA( this::entityManagerFactory, entityManager -> { - entityManager.persist( new Person( 1, 29 ) ); - } ); - - doInJPA( this::entityManagerFactory, entityManager -> { - List results = entityManager.createNativeQuery( - "select p.id, p.age from person p", Object[].class ) - .getResultList(); - assertEquals( 1, results.size() ); - assertEquals( Integer.valueOf( 1 ), results.get( 0 )[0] ); - assertEquals( Integer.valueOf( 29 ), results.get( 0 )[1] ); - } ); + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.createQuery( "delete from Person" ).executeUpdate(); + em.getTransaction().commit(); + em.close(); } }