8000 HHH-19383 - validation of NativeQuery result mappings · hibernate/hibernate-orm@3769ed9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3769ed9

Browse files
committed
HHH-19383 - validation of NativeQuery result mappings
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
1 parent 341ab5c commit 3769ed9

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package org.hibernate.query.sql.internal;
66

77
import java.io.Serializable;
8+
import java.lang.reflect.Constructor;
89
import java.time.Instant;
910
import java.util.Calendar;
1011
import java.util.Collection;
@@ -125,6 +126,7 @@
125126
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
126127
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
127128
import static org.hibernate.internal.util.collections.CollectionHelper.makeCopy;
129+
import static org.hibernate.internal.util.type.PrimitiveWrapperHelper.getDescriptorByPrimitiveType;
128130
import static org.hibernate.jpa.HibernateHints.HINT_NATIVE_LOCK_MODE;
129131
import static org.hibernate.query.results.internal.Builders.resultClassBuilder;
130132
import static org.hibernate.query.results.ResultSetMapping.resolveResultSetMapping;
@@ -330,25 +332,54 @@ private void handleExplicitResultSetMapping() {
330332
setTupleTransformerForResultType( resultType );
331333
}
332334
else {
333-
checkResultType( resultType );
335+
checkResultType( resultType, resultSetMapping );
334336
}
335337
}
336338
}
337339

338-
private void checkResultType(Class<R> resultType) {
339-
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
340-
case 0:
341-
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
342-
case 1:
343-
final Class<?> actualResultJavaType =
344-
resultSetMapping.getResultBuilders().get( 0 ).getJavaType();
345-
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
346-
throw buildIncompatibleException( resultType, actualResultJavaType );
347-
}
348-
break;
349-
default:
350-
throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
340+
private void checkResultType(Class<R> resultType, ResultSetMapping resultSetMapping) {
341+
// resultType can be null if any of the deprecated methods were used to create the query
342+
if ( resultType != null && !isResultTypeAlwaysAllowed( resultType )) {
343+
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
344+
case 0:
345+
if ( !resultSetMapping.isDynamic() ) {
346+
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
347+
}
348+
break;
349+
case 1:
350+
final Class<?> actualResultJavaType =
351+
resultSetMapping.getResultBuilders().get( 0 ).getJavaType();
352+
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
353+
throw buildIncompatibleException( resultType, actualResultJavaType );
354+
}
355+
break;
356+
default:
357+
// The return type has to be a class with an appropriate constructor, i.e. one whose parameter types match
358+
// the types of the result builders. If none such constructor is found, throw an IAE
359+
if ( !validConstructorFoundForResultType( resultType, resultSetMapping ) ) {
360+
throw new IllegalArgumentException( "The declared return type for a multi-valued result set mapping should be Object[], Map, List, or Tuple" );
361+
}
362+
}
363+
}
364+
}
365+
366+
private boolean validConstructorFoundForResultType(Class<R> resultType, ResultSetMapping resultSetMapping) {
367+
// Only 1 constructor with the right number of parameters is allowed (see NativeQueryConstructorTransformer)
368+
Constructor<?> constructor = resultType.getConstructors()[0];
369+
if ( constructor.getParameterCount() != resultSetMapping.getNumberOfResultBuilders() ) {
370+
return false;
371+
}
372+
final List<ResultBuilder> resultBuilders = resultSetMapping.getResultBuilders();
373+
Class<?>[] paramTypes = constructor.getParameterTypes();
374+
for ( int i = 0; i < resultBuilders.size(); i++ ) {
375+
if (
376+
resultBuilders.get( i ).getJavaType() != ( paramTypes[i].isPrimitive() ?
377+
getDescriptorByPrimitiveType(paramTypes[i] ).getWrapperClass() :
378+
paramTypes[i]) ) {
379+
return false;
380+
}
351381
}
382+
return true;
352383
}
353384

354385
protected <T> void setTupleTransformerForResultType(Class<T> resultClass) {
@@ -740,6 +771,7 @@ else if ( !isResultTypeAlwaysAllowed( resultType )
740771
else {
741772
mapping = resultSetMapping;
742773
}
774+
checkResultType( resultType, mapping );
743775
return isCacheableQuery()
744776
? getInterpretationCache()
745777
.resolveSelectQueryPlan( selectInterpretationsKey( mapping ), () -> createQueryPlan( mapping ) )

hibernate-core/src/test/java/org/hibernate/orm/test/sql/hand/query/NativeSQLQueriesTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.hibernate.query.NativeQuery;
3939
import org.hibernate.query.Query;
4040
import org.hibernate.query.ResultListTransformer;
41+
import org.hibernate.testing.orm.junit.Jira;
42+
import org.hibernate.testing.orm.junit.JiraGroup;
4143
import org.hibernate.transform.ResultTransformer;
4244
import org.hibernate.transform.Transformers;
4345
import org.hibernate.type.StandardBasicTypes;
@@ -59,6 +61,7 @@
5961
import static org.junit.jupiter.api.Assertions.assertEquals;
6062
import static org.junit.jupiter.api.Assertions.assertFalse;
6163
import static org.junit.jupiter.api.Assertions.assertNotNull;
64+
import static org.junit.jupiter.api.Assertions.assertThrows;
6265
import static org.junit.jupiter.api.Assertions.assertTrue;
6366
import static org.junit.jupiter.api.Assertions.fail;
6467

@@ -938,6 +941,32 @@ public void testAliasToBeanMap(SessionFactoryScope scope) {
938941
);
939942
}
940943

944+
@Test
945+
@JiraGroup(
946+
{
947+
@Jira("https://hibernate.atlassian.net/browse/HHH-19376"),
948+
@Jira("https://hibernate.atlassian.net/browse/HHH-19383")
949+
}
950+
)
951+
public void testMutateResultSetMapping(SessionFactoryScope scope) {
952+
scope.inTransaction(
953+
session -> {
954+
String sql = "SELECT p.*, COUNT(*) OVER() AS total_count "
955+
+ "FROM Person p "
956+
+ "WHERE p.name ILIKE :name "
957+
+ "ORDER BY p.id";
958+
// Declare Person as result type
959+
NativeQuery<Person> query = session.createNativeQuery(sql, Person.class);
960+
query.setParameter("name", "Ja%");
961+
query.setMaxResults(2);
962+
query.setFirstResult(0);
963+
// Now mutate the result set mapping and verify an Exception is thrown
964+
assertThrows( IllegalArgumentException.class,
965+
() -> query.addScalar( "total_count", StandardBasicTypes.LONG).list() );
966+
}
967+
);
968+
}
969+
941970
private String buildLongString(int size, char baseChar) {
942971
StringBuilder buff = new StringBuilder();
943972
for( int i = 0; i < size; i++ ) {

0 commit comments

Comments
 (0)
0