Skip to content

Conversation

@gavinking
Copy link
Member

@gavinking gavinking commented May 21, 2025

[Please describe here what your change is about]


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

gavinking added 2 commits May 21, 2025 23:40
NativeQueryConstructorTransformer does not prevent the result
type from having multiple constructors, it just rejects a
result type with multiple constructors with matching arity
@gavinking gavinking merged commit 3b3ee64 into hibernate:main May 22, 2025
25 checks passed
paramTypes[i]) ) {
return false;
// TODO: Only one constructor with the right number of parameters is allowed
// (see NativeQueryConstructorTransformer) so we should validate that
Copy link
Member

@jrenaat jrenaat May 22, 2025

Choose a reason for hiding this comment

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

Wasn't I doing just that in my original code?
I don't quite understand why you would instead iterate all constructors, if clearly only exactly one with the right amt. of params is allowed ... ?
I removed the constructor iteration I originally also had when I saw there should only be one ...

Copy link
Member Author

Choose a reason for hiding this comment

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

Well we have to skip over all the constructors with the wrong number of parameters until we get to one with the right number of parameters.

Copy link
Member

Choose a reason for hiding this comment

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

IINM, that is not true; NativeQueryConstructorTransformershould allow only a constructor with the same number of params, else an InstantiationExceptionwould be thrown, so no need to iterate?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's just not correct. Look at the code in NativeQueryConstructorTransformer. It iterates over the constructors, looking for one with the right number of parameters, and ignores all the rest:

for ( final Constructor<?> candidate : resultClass.getDeclaredConstructors() ) {
	final Class<?>[] parameterTypes = candidate.getParameterTypes();
	if ( parameterTypes.length == elements.length ) {
		// found a candidate with the right number
		// of parameters

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I was mislead by the exception message ...
Maybe it's somewhat confusing since it's possible for a resultclass to have several constructors with the same number of params as needed, but with different param types, and then you'd still get that message?
I'm just annoyed by the fact I initially had a perfectly good implementation including constructor iteration, and then I took that out thinking it was not needed, only for the lead Hibernate guy to come in after me and slap me on the wrist by rewriting my code. Fucks up my self-esteem in so many ways ...

Copy link
Member Author

Choose a reason for hiding this comment

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

it's possible for a resultclass to have several constructors with the same number of params as needed, but with different param types, and then you'd still get that message?

Correct. The potentially-ambiguous case is where you have multiple constructors with the same number of parameters but different types.

Fucks up my self-esteem in so many ways

I make mistakes all the time. I don't let it bother me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants