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:
parent
83e4384c3e
commit
9fac6747ef
|
@ -10,6 +10,7 @@ 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;
|
||||||
|
@ -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.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;
|
||||||
|
|
||||||
|
@ -932,12 +934,38 @@ 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 {
|
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 );
|
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
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
@ -20,6 +21,8 @@ import javax.persistence.SqlResultSetMapping;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,24 +61,64 @@ public class ScalarResultNativeQueryTest extends BaseEntityManagerFunctionalTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldApplyConfiguredTypeForProjectionOfScalarValue() {
|
public void shouldApplyConfiguredTypeForProjectionOfScalarValue() {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||||
em.getTransaction().begin();
|
entityManager.persist( new Person( 1, 29 ) );
|
||||||
em.persist( new Person( 1, 29 ) );
|
} );
|
||||||
em.getTransaction().commit();
|
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||||
em.getTransaction().begin();
|
List<String> results = entityManager.createNamedQuery( "personAge", String.class ).getResultList();
|
||||||
List<String> results = em.createNamedQuery( "personAge", String.class ).getResultList();
|
assertEquals( 1, results.size() );
|
||||||
assertEquals( 1, results.size() );
|
assertEquals( "29", results.get( 0 ) );
|
||||||
assertEquals( "29", results.get( 0 ) );
|
} );
|
||||||
em.getTransaction().commit();
|
}
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
@Test
|
||||||
em.getTransaction().begin();
|
@TestForIssue( jiraKey = "HHH-12670" )
|
||||||
em.createQuery( "delete from Person" ).executeUpdate();
|
public void testNativeSQLWithExplicitScalarMapping() {
|
||||||
em.getTransaction().commit();
|
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||||
em.close();
|
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] );
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue