-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
different approach to type checking arguments to query parameters #11406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gavinking
merged 5 commits into
hibernate:main
from
gavinking:query-parameter-types-rebase
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e43afc5
first phase of strict checking of arguments to query parameters
gavinking ce2a540
fix a user-written test that asserted some unreasonable things
gavinking 34b28d2
simplify the exception model for query arguments/parameters
gavinking 17bd413
very minor code refactoring
gavinking 74d0b27
introduce QueryArguments
gavinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
hibernate-core/src/main/java/org/hibernate/query/internal/QueryArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.query.internal; | ||
|
|
||
| import jakarta.persistence.metamodel.Type; | ||
| import org.hibernate.HibernateException; | ||
| import org.hibernate.type.BindingContext; | ||
| import org.hibernate.type.descriptor.java.JavaType; | ||
| import org.hibernate.type.descriptor.java.spi.EntityJavaType; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; | ||
|
|
||
| /** | ||
| * @since 7.3 | ||
| * | ||
| * @author Gavin King | ||
| */ | ||
| public class QueryArguments { | ||
|
|
||
| private static boolean isInstance(Object value, JavaType<?> javaType) { | ||
| try { | ||
| // special handling for entity arguments due to | ||
| // the possibility of an uninitialized proxy | ||
| // (which we don't want or need to fetch) | ||
| if ( javaType instanceof EntityJavaType<?> ) { | ||
| final var javaTypeClass = javaType.getJavaTypeClass(); | ||
| final var initializer = extractLazyInitializer( value ); | ||
| final var valueEntityClass = | ||
| initializer != null | ||
| ? initializer.getPersistentClass() | ||
| : value.getClass(); | ||
| // accept assignability in either direction | ||
| return javaTypeClass.isAssignableFrom( valueEntityClass ) | ||
| || valueEntityClass.isAssignableFrom( javaTypeClass ); | ||
| } | ||
| else { | ||
| // require that the argument be assignable to the parameter | ||
| return javaType.isInstance( javaType.coerce( value ) ); | ||
| } | ||
| } | ||
| catch (HibernateException ce) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean isInstance( | ||
| Type<?> parameterType, Object value, | ||
| BindingContext bindingContext) { | ||
| if ( value == null ) { | ||
| return true; | ||
| } | ||
| final var sqmExpressible = bindingContext.resolveExpressible( parameterType ); | ||
| assert sqmExpressible != null; | ||
| final var javaType = sqmExpressible.getExpressibleJavaType(); | ||
| return isInstance( value, javaType ); | ||
| } | ||
|
|
||
| public static boolean areInstances( | ||
| Type<?> parameterType, Collection<?> values, | ||
| BindingContext bindingContext) { | ||
| if ( values.isEmpty() ) { | ||
| return true; | ||
| } | ||
| final var sqmExpressible = bindingContext.resolveExpressible( parameterType ); | ||
| assert sqmExpressible != null; | ||
| final var javaType = sqmExpressible.getExpressibleJavaType(); | ||
| for ( Object value : values ) { | ||
| if ( !isInstance( value, javaType ) ) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public static boolean areInstances( | ||
| Type<?> parameterType, Object[] values, | ||
| BindingContext bindingContext) { | ||
| if ( values.length == 0 ) { | ||
| return true; | ||
| } | ||
| if ( parameterType.getJavaType() | ||
| .isAssignableFrom( values.getClass().getComponentType() ) ) { | ||
| return true; | ||
| } | ||
| final var sqmExpressible = bindingContext.resolveExpressible( parameterType ); | ||
| assert sqmExpressible != null; | ||
| final var javaType = sqmExpressible.getExpressibleJavaType(); | ||
| for ( Object value : values ) { | ||
| if ( !isInstance( value, javaType ) ) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.