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 f708a75c4f
commit daef4b7f08
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.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

View File

@ -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<String> results = entityManager.createNamedQuery( "personAge", String.class ).getResultList();
assertEquals( 1, results.size() );
assertEquals( "29", results.get( 0 ) );
} );
}
em = getOrCreateEntityManager();
em.getTransaction().begin();
List<String> 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<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] );
} );
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery( "delete from Person" ).executeUpdate();
em.getTransaction().commit();
em.close();
}
}