Skip to content

Commit 2940f5b

Browse files
committed
HHH-19449 simplify solution to HHH-17405
seems we can just use getExpressible() which already calls getResolvedModel()
1 parent dfd852c commit 2940f5b

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/SingularAttributeImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.hibernate.metamodel.model.domain.SimpleDomainType;
1818
import org.hibernate.query.SemanticException;
1919
import org.hibernate.query.sqm.NodeBuilder;
20+
import org.hibernate.query.sqm.SqmExpressible;
2021
import org.hibernate.query.sqm.SqmPathSource;
2122
import org.hibernate.query.hql.spi.SqmCreationState;
2223
import org.hibernate.query.sqm.internal.SqmMappingModelHelper;
@@ -138,6 +139,11 @@ public SqmPathSource<J> getSqmPathSource() {
138139
return sqmPathSource;
139140
}
140141

142+
@Override
143+
public SqmExpressible<J> getExpressible() {
144+
return sqmPathSource;
145+
}
146+
141147
@Override
142148
public boolean isGeneric() {
143149
return sqmPathSource.isGeneric();

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
import org.hibernate.query.sqm.tree.domain.SqmPluralValuedSimplePath;
107107
import org.hibernate.query.sqm.tree.domain.SqmSetJoin;
108108
import org.hibernate.query.sqm.tree.domain.SqmSingularJoin;
109-
import org.hibernate.query.sqm.tree.domain.SqmSingularPersistentAttribute;
110109
import org.hibernate.query.sqm.tree.domain.SqmTreatedRoot;
111110
import org.hibernate.query.sqm.tree.domain.SqmTreatedSingularJoin;
112111
import 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) {

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmPath.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,4 @@ default <Y> SqmPath<Y> get(String attributeName, boolean includeSubtypes) {
170170

171171
@Override
172172
SqmPath<T> copy(SqmCopyContext context);
173-
174-
175173
}

0 commit comments

Comments
 (0)