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

This commit is contained in:
Vlad Mihalcea 2018-06-07 17:40:06 +03:00
parent 83e4384c3e
commit 9fac6747ef
2 changed files with 89 additions and 18 deletions

View File

@ -10,6 +10,7 @@ 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;
@ -80,6 +81,7 @@ 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;
@ -932,12 +934,38 @@ 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 {
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 ) ) ) {
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,6 +6,7 @@
*/
package org.hibernate.jpa.test.query;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import java.util.List;
@ -20,6 +21,8 @@ import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/**
@ -58,24 +61,64 @@ public class ScalarResultNativeQueryTest extends BaseEntityManagerFunctionalTest
@Test
public void shouldApplyConfiguredTypeForProjectionOfScalarValue() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( new Person( 1, 29 ) );
em.getTransaction().commit();
em.close();
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( new Person( 1, 29 ) );
} );
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();
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();
em.createQuery( "delete from Person" ).executeUpdate();
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] );
} );
}
}