@@ -330,24 +330,32 @@ private void handleExplicitResultSetMapping() {
330330 setTupleTransformerForResultType ( resultType );
331331 }
332332 else {
333- checkResultType ( resultType );
333+ checkResultType ( resultType , resultSetMapping );
334334 }
335335 }
336336 }
337337
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" );
338+ private void checkResultType (Class <R > resultType , ResultSetMapping resultSetMapping ) {
339+ // resultType can be null if any of the deprecated methods were used to create the query
340+ if ( resultType != null && !isResultTypeAlwaysAllowed ( resultType )) {
341+ switch ( resultSetMapping .getNumberOfResultBuilders () ) {
342+ case 0 :
343+ if ( !resultSetMapping .isDynamic () ) {
344+ throw new IllegalArgumentException ( "Named query exists, but did not specify a resultClass" );
345+ }
346+ break ;
347+ case 1 :
348+ final Class <?> actualResultJavaType =
349+ resultSetMapping .getResultBuilders ().get ( 0 ).getJavaType ();
350+ if ( actualResultJavaType != null && !resultType .isAssignableFrom ( actualResultJavaType ) ) {
351+ throw buildIncompatibleException ( resultType , actualResultJavaType );
352+ }
353+ break ;
354+ default :
355+ if ( resultType != Object [].class ) {
356+ throw new IllegalArgumentException ( "The declared return type for a multi-valued result set mapping should be Object[]" );
357+ }
358+ }
351359 }
352360 }
353361
@@ -740,6 +748,7 @@ else if ( !isResultTypeAlwaysAllowed( resultType )
740748 else {
741749 mapping = resultSetMapping ;
742750 }
751+ checkResultType ( resultType , mapping );
743752 return isCacheableQuery ()
744753 ? getInterpretationCache ()
745754 .resolveSelectQueryPlan ( selectInterpretationsKey ( mapping ), () -> createQueryPlan ( mapping ) )
0 commit comments