Revert "HHH-12670 - Allows native SQL queries that take a given resultClass to map the result set to the required type"

This reverts commit 9fac6747ef.
This commit is contained in:
Guillaume Smet 2018-07-09 13:27:55 +02:00
parent 8814d4753f
commit 1d68b1a2d0
2 changed files with 18 additions and 89 deletions

View File

@ -10,7 +10,6 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Array;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.TimeZone; 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.backend.jta.internal.JtaTransactionCoordinatorImpl;
import org.hibernate.resource.transaction.spi.TransactionCoordinator; import org.hibernate.resource.transaction.spi.TransactionCoordinator;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.transform.BasicTransformerAdapter;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
@ -934,38 +932,12 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
private void handleNativeQueryResult(NativeQueryImplementor query, Class resultClass) { private void handleNativeQueryResult(NativeQueryImplementor query, Class resultClass) {
boolean isObjectArray = Object[].class.equals( resultClass );
if ( Tuple.class.equals( resultClass ) ) { if ( Tuple.class.equals( resultClass ) ) {
query.setResultTransformer( new NativeQueryTupleTransformer() ); query.setResultTransformer( new NativeQueryTupleTransformer() );
} }
else if ( resultClass.isArray() && !isObjectArray ) { else {
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 ) ) ) {
query.addEntity( "alias1", resultClass.getName(), LockMode.READ ); 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 @Override

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.jpa.test.query; package org.hibernate.jpa.test.query;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.List; import java.util.List;
@ -22,9 +21,7 @@ import javax.persistence.Table;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test; import org.junit.Test;
/** /**
@ -64,64 +61,24 @@ public class ScalarResultNativeQueryTest extends BaseEntityManagerFunctionalTest
@Test @Test
public void shouldApplyConfiguredTypeForProjectionOfScalarValue() { public void shouldApplyConfiguredTypeForProjectionOfScalarValue() {
doInJPA( this::entityManagerFactory, entityManager -> { EntityManager em = getOrCreateEntityManager();
entityManager.persist( new Person( 1, 29 ) ); em.getTransaction().begin();
} ); em.persist( new Person( 1, 29 ) );
em.getTransaction().commit();
em.close();
doInJPA( this::entityManagerFactory, entityManager -> { em = getOrCreateEntityManager();
List<String> results = entityManager.createNamedQuery( "personAge", String.class ).getResultList(); em.getTransaction().begin();
assertEquals( 1, results.size() ); List<String> results = em.createNamedQuery( "personAge", String.class ).getResultList();
assertEquals( "29", results.get( 0 ) ); assertEquals( 1, results.size() );
} ); assertEquals( "29", results.get( 0 ) );
} em.getTransaction().commit();
em.close();
@Test em = getOrCreateEntityManager();
@TestForIssue( jiraKey = "HHH-12670" ) em.getTransaction().begin();
public void testNativeSQLWithExplicitScalarMapping() { em.createQuery( "delete from Person" ).executeUpdate();
doInJPA( this::entityManagerFactory, entityManager -> { em.getTransaction().commit();
entityManager.persist( new Person( 1, 29 ) ); em.close();
} );
doInJPA( this::entityManagerFactory, entityManager -> {
List<Integer> 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<Integer[]> 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<Object[]> 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] );
} );
} }
} }