Skip to content

HHH-19383 - validation of NativeQuery result mappings #10085

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jrenaat
Copy link
Member

@jrenaat jrenaat commented Apr 25, 2025


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-19383

@jrenaat jrenaat requested a review from gavinking April 25, 2025 20:25
Comment on lines +353 to +359
for ( ResultBuilder resultBuilder : resultSetMapping.getResultBuilders() ) {
final Class rbJavaType = resultBuilder.getJavaType();
if ( !resultType.isAssignableFrom( rbJavaType ) ) {
throw new IllegalArgumentException( "The result set mapping of the typed query doesn't match the declared return type" );
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple resultBuilders, shouldn't the resultType be Object[]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily, at least that is what I thought?
If the return type is Object[], there's really not much to check since that flag covers everything.
For that reason I initially surrounded that for loop with if ( resultType != Object[].class ) , but it should already be covered by the outer if.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this should be an error:

s.createNativeQuery("select title, isbn from books", String.class)
     .addScalar("title", String.class)
     .addScalar("isbn", String.class)
     .getResultList();

The result type of that query has to be Object[], not String.

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
@jrenaat jrenaat force-pushed the HHH-19383_checkResultType branch from 5869a45 to 53675e6 Compare April 25, 2025 21:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants