106106import org .hibernate .query .sqm .tree .domain .SqmPluralValuedSimplePath ;
107107import org .hibernate .query .sqm .tree .domain .SqmSetJoin ;
108108import org .hibernate .query .sqm .tree .domain .SqmSingularJoin ;
109- import org .hibernate .query .sqm .tree .domain .SqmSingularPersistentAttribute ;
110109import org .hibernate .query .sqm .tree .domain .SqmTreatedRoot ;
111110import org .hibernate .query .sqm .tree .domain .SqmTreatedSingularJoin ;
112111import org .hibernate .query .sqm .tree .expression .*;
@@ -2095,7 +2094,11 @@ public <T> SqmExpression<T> value(T value, SqmExpression<? extends T> typeInfere
20952094 //noinspection unchecked
20962095 return (SqmExpression <T >) value ;
20972096 }
2098- return inlineValue ( value ) ? literal ( value , typeInferenceSource ) : valueParameter ( value , typeInferenceSource );
2097+ else {
2098+ return inlineValue ( value )
2099+ ? literal ( value , typeInferenceSource )
2100+ : valueParameter ( value , typeInferenceSource );
2101+ }
20992102 }
21002103
21012104 private <E > SqmExpression <? extends Collection <?>> collectionValue (Collection <E > value , SqmExpression <E > typeInferenceSource ) {
@@ -2120,7 +2123,7 @@ else if ( value instanceof SqmExpression<?> ) {
21202123 }
21212124 }
21222125
2123- private <T > boolean isInstance (BindableType <T > bindableType , T value ) {
2126+ private <T > boolean isInstance (BindableType <? extends T > bindableType , T value ) {
21242127 if ( bindableType instanceof SqmExpressible <?> expressible ) {
21252128 return expressible .getExpressibleJavaType ().isInstance ( value );
21262129 }
@@ -2130,50 +2133,49 @@ private <T> boolean isInstance(BindableType<T> bindableType, T value) {
21302133 }
21312134 }
21322135
2133- @ SuppressWarnings ("unchecked" )
2134- private static <T > BindableType <T > resolveInferredParameterType (
2135- T value ,
2136- SqmExpression <? extends T > typeInferenceSource ,
2136+ private static <X , T extends X > BindableType <? extends X > resolveInferredParameterType (
2137+ X value ,
2138+ SqmExpression <T > typeInferenceSource ,
21372139 TypeConfiguration typeConfiguration ) {
21382140
21392141 if ( typeInferenceSource != null ) {
21402142 if ( typeInferenceSource instanceof BindableType ) {
2143+ //noinspection unchecked
21412144 return (BindableType <T >) typeInferenceSource ;
21422145 }
2143- final SqmExpressible <? > nodeType = getNodeType ( typeInferenceSource );
2146+ final SqmExpressible <T > nodeType = typeInferenceSource . getExpressible ( );
21442147 if ( nodeType != null ) {
2145- return ( BindableType < T >) nodeType ;
2148+ return nodeType ;
21462149 }
21472150 }
21482151
2149- return value == null ? null : (BasicType <T >) typeConfiguration .getBasicTypeForJavaType ( value .getClass () );
2150- }
2151-
2152- private static SqmExpressible <?> getNodeType (SqmExpression <?> expression ) {
2153- if ( expression instanceof SqmPath <?> sqmPath ) {
2154- return sqmPath .getResolvedModel () instanceof SqmSingularPersistentAttribute <?,?> attribute
2155- ? attribute .getSqmPathSource ()
2156- : sqmPath .getResolvedModel ();
2157- // : sqmPath.getExpressible();
2152+ if ( value == null ) {
2153+ return null ;
21582154 }
21592155 else {
2160- return expression .getNodeType ();
2156+ @ SuppressWarnings ("unchecked" ) // this is completely safe
2157+ final Class <? extends X > valueClass = (Class <? extends X >) value .getClass ();
2158+ return typeConfiguration .getBasicTypeForJavaType ( valueClass );
21612159 }
21622160 }
21632161
21642162 private <T > ValueBindJpaCriteriaParameter <T > valueParameter (T value , SqmExpression <? extends T > typeInferenceSource ) {
2165- final BindableType <T > bindableType =
2166- resolveInferredParameterType ( value , typeInferenceSource , getTypeConfiguration () );
2163+ final var bindableType = resolveInferredParameterType ( value , typeInferenceSource , getTypeConfiguration () );
21672164 if ( bindableType == null || isInstance ( bindableType , value ) ) {
2168- return new ValueBindJpaCriteriaParameter <>( bindableType , value , this );
2165+ @ SuppressWarnings ("unchecked" ) // safe, we just checked
2166+ final var widerType = (BindableType <? super T >) bindableType ;
2167+ return new ValueBindJpaCriteriaParameter <>( widerType , value , this );
21692168 }
21702169 final T coercedValue =
21712170 resolveExpressible ( bindableType ).getExpressibleJavaType ()
2172- .coerce (value , this ::getTypeConfiguration );
2173- return isInstance ( bindableType , coercedValue )
2174- ? new ValueBindJpaCriteriaParameter <>( bindableType , coercedValue , this )
2175- // ignore typeInferenceSource and fall back the value type
2176- : new ValueBindJpaCriteriaParameter <>( getParameterBindType ( value ), value , this );
2171+ .coerce ( value , this ::getTypeConfiguration );
2172+ // ignore typeInferenceSource and fall back the value type
2173+ if ( isInstance ( bindableType , coercedValue ) ) {
2174+ @ SuppressWarnings ("unchecked" ) // safe, we just checked
2175+ final var widerType = (BindableType <? super T >) bindableType ;
2176+ return new ValueBindJpaCriteriaParameter <>( widerType , coercedValue , this );
2177+ }
2178+ return new ValueBindJpaCriteriaParameter <>( getParameterBindType ( value ), value , this );
21772179 }
21782180
21792181 private <E > ValueBindJpaCriteriaParameter <? extends Collection <E >> collectionValueParameter (Collection <E > value , SqmExpression <E > elementTypeInferenceSource ) {
0 commit comments