Skip to content

Commit 53675e6

Browse files
committed
HHH-19383 - validation of NativeQuery result mappings
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 6dc2e7b commit 53675e6

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,35 @@ private void handleExplicitResultSetMapping() {
328328
setTupleTransformerForResultType( resultType );
329329
}
330330
else {
331-
checkResultType( resultType );
331+
checkResultType( resultType, resultSetMapping );
332332
}
333333
}
334334
}
335335

336-
private void checkResultType(Class<R> resultType) {
337-
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
338-
case 0:
339-
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
340-
case 1:
341-
final Class<?> actualResultJavaType =
342-
resultSetMapping.getResultBuilders().get( 0 ).getJavaType();
343-
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
344-
throw buildIncompatibleException( resultType, actualResultJavaType );
345-
}
346-
break;
347-
default:
348-
throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
336+
private void checkResultType(Class<R> resultType, ResultSetMapping resultSetMapping) {
337+
// resultType can be null if any of the deprecated methods were used to create the query
338+
if ( resultType != null && !isResultTypeAlwaysAllowed( resultType )) {
339+
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
340+
case 0:
341+
if ( !resultSetMapping.isDynamic() ) {
342+
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
343+
}
344+
break;
345+
case 1:
346+
final Class<?> actualResultJavaType =
347+
resultSetMapping.getResultBuilders().get( 0 ).getJavaType();
348+
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
349+
throw buildIncompatibleException( resultType, actualResultJavaType );
350+
}
351+
break;
352+
default:
353+
for ( ResultBuilder resultBuilder : resultSetMapping.getResultBuilders() ) {
354+
final Class rbJavaType = resultBuilder.getJavaType();
355+
if ( !resultType.isAssignableFrom( rbJavaType ) ) {
356+
throw new IllegalArgumentException( "The result set mapping of the typed query doesn't match the declared return type" );
357+
}
358+
}
359+
}
349360
}
350361
}
351362

@@ -716,6 +727,7 @@ else if ( !isResultTypeAlwaysAllowed( resultType )
716727
else {
717728
mapping = resultSetMapping;
718729
}
730+
checkResultType( resultType, mapping );
719731
return isCacheableQuery()
720732
? getInterpretationCache()
721733
.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)