29
29
import org .hibernate .type .BasicPluralType ;
30
30
import org .hibernate .type .BasicType ;
31
31
import org .hibernate .type .QueryParameterJavaObjectType ;
32
+ import org .hibernate .type .descriptor .converter .spi .BasicValueConverter ;
32
33
import org .hibernate .type .descriptor .jdbc .JdbcType ;
33
34
34
35
import java .time .temporal .Temporal ;
@@ -137,8 +138,8 @@ public static boolean areTypesComparable(
137
138
138
139
// for tuple constructors, we must check each element
139
140
140
- if ( lhsDomainType instanceof TupleType && rhsDomainType instanceof TupleType ) {
141
- return areTupleTypesComparable (bindingContext , ( TupleType <?>) lhsDomainType , ( TupleType <?>) rhsDomainType );
141
+ if ( lhsDomainType instanceof TupleType <?> lhsTuple && rhsDomainType instanceof TupleType <?> rhsTuple ) {
142
+ return areTupleTypesComparable ( bindingContext , lhsTuple , rhsTuple );
142
143
}
143
144
144
145
// allow comparing an embeddable against a tuple literal
@@ -151,18 +152,18 @@ public static boolean areTypesComparable(
151
152
152
153
// entities can be compared if they belong to the same inheritance hierarchy
153
154
154
- if ( lhsDomainType instanceof EntityType && rhsDomainType instanceof EntityType ) {
155
- return areEntityTypesComparable ( ( EntityType <?>) lhsDomainType , ( EntityType <?>) rhsDomainType , bindingContext );
155
+ if ( lhsDomainType instanceof EntityType <?> lhsEntity && rhsDomainType instanceof EntityType <?> rhsEntity ) {
156
+ return areEntityTypesComparable ( lhsEntity , rhsEntity , bindingContext );
156
157
}
157
158
158
159
// entities can be compared to discriminators if they belong to
159
160
// the same inheritance hierarchy
160
161
161
- if ( lhsDomainType instanceof EntityDiscriminatorSqmPathSource ) {
162
- return isDiscriminatorTypeComparable ( ( EntityDiscriminatorSqmPathSource <?>) lhsDomainType , rhsDomainType , bindingContext );
162
+ if ( lhsDomainType instanceof EntityDiscriminatorSqmPathSource <?> discriminatorSource ) {
163
+ return isDiscriminatorTypeComparable ( discriminatorSource , rhsDomainType , bindingContext );
163
164
}
164
- if ( rhsDomainType instanceof EntityDiscriminatorSqmPathSource ) {
165
- return isDiscriminatorTypeComparable ( ( EntityDiscriminatorSqmPathSource <?>) rhsDomainType , lhsDomainType , bindingContext );
165
+ if ( rhsDomainType instanceof EntityDiscriminatorSqmPathSource <?> discriminatorSource ) {
166
+ return isDiscriminatorTypeComparable ( discriminatorSource , lhsDomainType , bindingContext );
166
167
}
167
168
168
169
// Treat the expressions as comparable if they belong to the same
@@ -171,10 +172,9 @@ public static boolean areTypesComparable(
171
172
// decent approach which allows comparison between literals and
172
173
// enums, user-defined types, etc.
173
174
174
- if ( lhsDomainType instanceof JdbcMapping && rhsDomainType instanceof JdbcMapping ) {
175
- if ( areJdbcMappingsComparable ( (JdbcMapping ) lhsDomainType , (JdbcMapping ) rhsDomainType , bindingContext ) ) {
176
- return true ;
177
- }
175
+ if ( lhsDomainType instanceof JdbcMapping lhsMapping && rhsDomainType instanceof JdbcMapping rhsMapping
176
+ && areJdbcMappingsComparable ( lhsMapping , rhsMapping , bindingContext ) ) {
177
+ return true ;
178
178
}
179
179
180
180
// Workaround: these are needed for a handful of slightly "weird" cases
@@ -183,7 +183,7 @@ public static boolean areTypesComparable(
183
183
// sort of hole warned about above, and accepts many things which are
184
184
// not well-typed.
185
185
186
- // TODO: sort all this out, and remove this branch
186
+ // // TODO: sort all this out, and remove this branch
187
187
if ( isSameJavaType ( lhsType , rhsType ) ) {
188
188
return true ;
189
189
}
@@ -202,28 +202,27 @@ private static boolean areJdbcMappingsComparable(
202
202
else if ( lhsJdbcMapping .getValueConverter () != null || rhsJdbcMapping .getValueConverter () != null ) {
203
203
final JdbcMapping lhsDomainMapping = getDomainJdbcType ( lhsJdbcMapping , bindingContext );
204
204
final JdbcMapping rhsDomainMapping = getDomainJdbcType ( rhsJdbcMapping , bindingContext );
205
- return lhsDomainMapping != null && rhsDomainMapping != null && areJdbcTypesComparable (
206
- lhsDomainMapping .getJdbcType (),
207
- rhsDomainMapping .getJdbcType ()
208
- );
205
+ return lhsDomainMapping != null && rhsDomainMapping != null
206
+ && areJdbcTypesComparable ( lhsDomainMapping .getJdbcType (), rhsDomainMapping .getJdbcType () );
209
207
}
210
208
return false ;
211
209
}
212
210
213
211
private static boolean areJdbcTypesComparable (JdbcType lhsJdbcType , JdbcType rhsJdbcType ) {
214
212
return lhsJdbcType .getJdbcTypeCode () == rhsJdbcType .getJdbcTypeCode ()
215
- // "families" of implicitly-convertible JDBC types
216
- // (this list might need to be extended in future)
217
- || lhsJdbcType .isStringLike () && rhsJdbcType .isStringLike ()
218
- || lhsJdbcType .isTemporal () && rhsJdbcType .isTemporal ()
219
- || lhsJdbcType .isNumber () && rhsJdbcType .isNumber ();
213
+ // "families" of implicitly-convertible JDBC types
214
+ // (this list might need to be extended in future)
215
+ || lhsJdbcType .isStringLike () && rhsJdbcType .isStringLike ()
216
+ || lhsJdbcType .isTemporal () && rhsJdbcType .isTemporal ()
217
+ || lhsJdbcType .isNumber () && rhsJdbcType .isNumber ();
220
218
}
221
219
222
220
private static JdbcMapping getDomainJdbcType (JdbcMapping jdbcMapping , BindingContext bindingContext ) {
223
- if ( jdbcMapping .getValueConverter () != null ) {
221
+ final BasicValueConverter <?,?> valueConverter = jdbcMapping .getValueConverter ();
222
+ if ( valueConverter != null ) {
224
223
final BasicType <?> basicType =
225
224
bindingContext .getTypeConfiguration ()
226
- .getBasicTypeForJavaType ( jdbcMapping . getValueConverter () .getDomainJavaType ().getJavaType () );
225
+ .getBasicTypeForJavaType ( valueConverter .getDomainJavaType ().getJavaType () );
227
226
if ( basicType != null ) {
228
227
return basicType .getJdbcMapping ();
229
228
}
@@ -232,17 +231,14 @@ private static JdbcMapping getDomainJdbcType(JdbcMapping jdbcMapping, BindingCon
232
231
}
233
232
234
233
private static EmbeddableDomainType <?> getEmbeddableType (SqmExpressible <?> expressible ) {
235
- return expressible instanceof EmbeddableDomainType <?> ? ( EmbeddableDomainType <?>) expressible : null ;
234
+ return expressible instanceof EmbeddableDomainType <?> embeddableDomainType ? embeddableDomainType : null ;
236
235
}
237
236
238
237
private static boolean areEmbeddableTypesComparable (
239
238
EmbeddableDomainType <?> lhsType ,
240
239
EmbeddableDomainType <?> rhsType ) {
241
- if ( rhsType .getJavaType () == lhsType .getJavaType () ) {
242
- return true ;
243
- }
244
-
245
- return lhsType .isPolymorphic () && getRootEmbeddableType ( lhsType ) == getRootEmbeddableType ( rhsType );
240
+ return rhsType .getJavaType () == lhsType .getJavaType ()
241
+ || lhsType .isPolymorphic () && getRootEmbeddableType ( lhsType ) == getRootEmbeddableType ( rhsType );
246
242
}
247
243
248
244
private static ManagedDomainType <?> getRootEmbeddableType (EmbeddableDomainType <?> embeddableType ) {
@@ -262,7 +258,7 @@ private static boolean areTupleTypesComparable(
262
258
}
263
259
else {
264
260
for ( int i = 0 ; i < lhsTuple .componentCount (); i ++ ) {
265
- if ( !areTypesComparable ( lhsTuple .get (i ), rhsTuple .get (i ), bindingContext ) ) {
261
+ if ( !areTypesComparable ( lhsTuple .get (i ), rhsTuple .get (i ), bindingContext ) ) {
266
262
return false ;
267
263
}
268
264
}
@@ -273,29 +269,28 @@ private static boolean areTupleTypesComparable(
273
269
private static boolean areEntityTypesComparable (
274
270
EntityType <?> lhsType , EntityType <?> rhsType ,
275
271
BindingContext bindingContext ) {
276
- EntityPersister lhsEntity = getEntityDescriptor (bindingContext , lhsType .getName () );
277
- EntityPersister rhsEntity = getEntityDescriptor (bindingContext , rhsType .getName () );
272
+ final EntityPersister lhsEntity = getEntityDescriptor ( bindingContext , lhsType .getName () );
273
+ final EntityPersister rhsEntity = getEntityDescriptor ( bindingContext , rhsType .getName () );
278
274
return lhsEntity .getRootEntityName ().equals ( rhsEntity .getRootEntityName () );
279
275
}
280
276
281
277
private static boolean isDiscriminatorTypeComparable (
282
278
EntityDiscriminatorSqmPathSource <?> lhsDiscriminator , SqmExpressible <?> rhsType ,
283
279
BindingContext bindingContext ) {
284
- String entityName = lhsDiscriminator .getEntityDomainType ().getHibernateEntityName ();
285
- EntityPersister lhsEntity = bindingContext .getMappingMetamodel ().getEntityDescriptor ( entityName );
286
- if ( rhsType instanceof EntityType ) {
287
- String rhsEntityName = (( EntityType <?>) rhsType ) .getName ();
288
- EntityPersister rhsEntity = getEntityDescriptor (bindingContext , rhsEntityName );
280
+ final String entityName = lhsDiscriminator .getEntityDomainType ().getHibernateEntityName ();
281
+ final EntityPersister lhsEntity = bindingContext .getMappingMetamodel ().getEntityDescriptor ( entityName );
282
+ if ( rhsType instanceof EntityType <?> entityType ) {
283
+ final String rhsEntityName = entityType .getName ();
284
+ final EntityPersister rhsEntity = getEntityDescriptor ( bindingContext , rhsEntityName );
289
285
return lhsEntity .getRootEntityName ().equals ( rhsEntity .getRootEntityName () );
290
286
}
291
- else if ( rhsType instanceof EntityDiscriminatorSqmPathSource ) {
292
- EntityDiscriminatorSqmPathSource <?> discriminator = (EntityDiscriminatorSqmPathSource <?>) rhsType ;
293
- String rhsEntityName = discriminator .getEntityDomainType ().getHibernateEntityName ();
294
- EntityPersister rhsEntity = bindingContext .getMappingMetamodel ().getEntityDescriptor ( rhsEntityName );
287
+ else if ( rhsType instanceof EntityDiscriminatorSqmPathSource <?> discriminator ) {
288
+ final String rhsEntityName = discriminator .getEntityDomainType ().getHibernateEntityName ();
289
+ final EntityPersister rhsEntity = bindingContext .getMappingMetamodel ().getEntityDescriptor ( rhsEntityName );
295
290
return rhsEntity .getRootEntityName ().equals ( lhsEntity .getRootEntityName () );
296
291
}
297
292
else {
298
- BasicType <?> discriminatorType = (BasicType <?>)
293
+ final BasicType <?> discriminatorType = (BasicType <?>)
299
294
lhsDiscriminator .getEntityMapping ().getDiscriminatorMapping ().getMappedType ();
300
295
return areTypesComparable ( discriminatorType , rhsType , bindingContext );
301
296
}
@@ -317,8 +312,9 @@ private static boolean isTypeAssignable(
317
312
318
313
// entities can be assigned if they belong to the same inheritance hierarchy
319
314
320
- if ( targetType instanceof EntityType && expressionType instanceof EntityType ) {
321
- return isEntityTypeAssignable ( (EntityType <?>) targetType , (EntityType <?>) expressionType , bindingContext );
315
+ if ( targetType instanceof EntityType <?> targetEntity
316
+ && expressionType instanceof EntityType <?> expressionEntity ) {
317
+ return isEntityTypeAssignable ( targetEntity , expressionEntity , bindingContext );
322
318
}
323
319
324
320
// Treat the expression as assignable to the target path if they belong
@@ -327,11 +323,11 @@ private static boolean isTypeAssignable(
327
323
// decent approach which allows comparison between literals and enums,
328
324
// user-defined types, etc.
329
325
330
- DomainType <?> lhsDomainType = targetType .getSqmType ();
331
- DomainType <?> rhsDomainType = expressionType .getSqmType ();
332
- if ( lhsDomainType instanceof JdbcMapping && rhsDomainType instanceof JdbcMapping ) {
333
- JdbcType lhsJdbcType = (( JdbcMapping ) lhsDomainType ) .getJdbcType ();
334
- JdbcType rhsJdbcType = (( JdbcMapping ) rhsDomainType ) .getJdbcType ();
326
+ final DomainType <?> lhsDomainType = targetType .getSqmType ();
327
+ final DomainType <?> rhsDomainType = expressionType .getSqmType ();
328
+ if ( lhsDomainType instanceof JdbcMapping lhsMapping && rhsDomainType instanceof JdbcMapping rhsMapping ) {
329
+ final JdbcType lhsJdbcType = lhsMapping .getJdbcType ();
330
+ final JdbcType rhsJdbcType = rhsMapping .getJdbcType ();
335
331
if ( lhsJdbcType .getJdbcTypeCode () == rhsJdbcType .getJdbcTypeCode ()
336
332
// "families" of implicitly-convertible JDBC types
337
333
// (this list might need to be extended in future)
@@ -367,8 +363,8 @@ private static boolean isSameJavaType(SqmExpressible<?> leftType, SqmExpressible
367
363
private static boolean isEntityTypeAssignable (
368
364
EntityType <?> lhsType , EntityType <?> rhsType ,
369
365
BindingContext bindingContext ) {
370
- EntityPersister lhsEntity = getEntityDescriptor (bindingContext , lhsType .getName () );
371
- EntityPersister rhsEntity = getEntityDescriptor (bindingContext , rhsType .getName () );
366
+ final EntityPersister lhsEntity = getEntityDescriptor ( bindingContext , lhsType .getName () );
367
+ final EntityPersister rhsEntity = getEntityDescriptor ( bindingContext , rhsType .getName () );
372
368
return lhsEntity .isSubclassEntityName ( rhsEntity .getEntityName () );
373
369
}
374
370
@@ -384,23 +380,23 @@ public static void assertComparable(Expression<?> x, Expression<?> y, BindingCon
384
380
final SqmExpression <?> left = (SqmExpression <?>) x ;
385
381
final SqmExpression <?> right = (SqmExpression <?>) y ;
386
382
final Integer leftTupleLength = left .getTupleLength ();
387
- final Integer rightTupleLength ;
388
- if ( leftTupleLength != null && ( rightTupleLength = right . getTupleLength () ) != null
383
+ final Integer rightTupleLength = right . getTupleLength () ;
384
+ if ( leftTupleLength != null && rightTupleLength != null
389
385
&& leftTupleLength .intValue () != rightTupleLength .intValue () ) {
390
386
throw new SemanticException ( "Cannot compare tuples of different lengths" );
391
387
}
392
388
393
- // SqmMemerOfPredicate is the only one allowing multi-valued paths, its comparability is now evaluated in areTypesComparable
389
+ // SqmMemberOfPredicate is the only one allowing multivalued paths, its comparability is now evaluated in areTypesComparable
394
390
// i.e. without calling this method, so we can check this here for other Predicates that do call this
395
391
if ( left instanceof SqmPluralValuedSimplePath || right instanceof SqmPluralValuedSimplePath ) {
396
- throw new SemanticException ( "Multi valued paths are only allowed for the member of operator" );
392
+ throw new SemanticException ( "Multivalued paths are only allowed for the ' member of' operator" );
397
393
}
398
394
399
395
// allow comparing literal null to things
400
396
if ( !( left instanceof SqmLiteralNull ) && !( right instanceof SqmLiteralNull ) ) {
401
397
final SqmExpressible <?> leftType = left .getExpressible ();
402
398
final SqmExpressible <?> rightType = right .getExpressible ();
403
- if ( !areTypesComparable ( leftType , rightType , bindingContext ) ) {
399
+ if ( !areTypesComparable ( leftType , rightType , bindingContext ) ) {
404
400
throw new SemanticException (
405
401
String .format (
406
402
"Cannot compare left expression of type '%s' with right expression of type '%s'" ,
@@ -424,8 +420,8 @@ public static void assertAssignable(
424
420
// TODO: check that the target path is nullable
425
421
}
426
422
else {
427
- SqmPathSource <?> targetType = targetPath .getNodeType ();
428
- SqmExpressible <?> expressionType = expression .getNodeType ();
423
+ final SqmPathSource <?> targetType = targetPath .getNodeType ();
424
+ final SqmExpressible <?> expressionType = expression .getNodeType ();
429
425
if ( !isTypeAssignable ( targetType , expressionType , bindingContext ) ) {
430
426
throw new SemanticException (
431
427
String .format (
@@ -542,20 +538,21 @@ else if ( isNumberArray( leftNodeType ) ) {
542
538
}
543
539
544
540
public static boolean isNumberArray (SqmExpressible <?> expressible ) {
545
- final DomainType <?> domainType ;
546
- if ( expressible != null && ( domainType = expressible .getSqmType () ) != null ) {
547
- return domainType instanceof BasicPluralType <?, ?> && Number .class .isAssignableFrom (
548
- ( (BasicPluralType <?, ?>) domainType ).getElementType ().getJavaType ()
549
- );
541
+ if ( expressible != null ) {
542
+ final DomainType <?> domainType = expressible .getSqmType ();
543
+ if ( domainType != null ) {
544
+ return domainType instanceof BasicPluralType <?, ?> basicPluralType
545
+ && Number .class .isAssignableFrom ( basicPluralType .getElementType ().getJavaType () );
546
+ }
550
547
}
551
548
return false ;
552
549
}
553
550
554
551
public static void assertString (SqmExpression <?> expression ) {
555
552
final SqmExpressible <?> nodeType = expression .getNodeType ();
556
553
if ( nodeType != null ) {
557
- final DomainType <?> domainType = nodeType .getSqmType ();
558
- if ( !( domainType instanceof JdbcMapping ) || !( ( JdbcMapping ) domainType ) .getJdbcType ().isStringLike () ) {
554
+ if ( !( nodeType .getSqmType () instanceof JdbcMapping jdbcMapping )
555
+ || !jdbcMapping .getJdbcType ().isStringLike () ) {
559
556
throw new SemanticException (
560
557
"Operand of 'like' is of type '" + nodeType .getTypeName () +
561
558
"' which is not a string (its JDBC type code is not string-like)"
@@ -567,8 +564,8 @@ public static void assertString(SqmExpression<?> expression) {
567
564
public static void assertDuration (SqmExpression <?> expression ) {
568
565
final SqmExpressible <?> nodeType = expression .getNodeType ();
569
566
if ( nodeType != null ) {
570
- final DomainType <?> domainType = nodeType .getSqmType ();
571
- if ( !( domainType instanceof JdbcMapping ) || !( ( JdbcMapping ) domainType ) .getJdbcType ().isDuration () ) {
567
+ if ( !( nodeType .getSqmType () instanceof JdbcMapping jdbcMapping )
568
+ || !jdbcMapping .getJdbcType ().isDuration () ) {
572
569
throw new SemanticException (
573
570
"Operand of 'by' is of type '" + nodeType .getTypeName () +
574
571
"' which is not a duration (its JDBC type code is not duration-like)"
@@ -580,8 +577,8 @@ public static void assertDuration(SqmExpression<?> expression) {
580
577
public static void assertNumeric (SqmExpression <?> expression , UnaryArithmeticOperator op ) {
581
578
final SqmExpressible <?> nodeType = expression .getNodeType ();
582
579
if ( nodeType != null ) {
583
- final DomainType <?> domainType = nodeType .getSqmType ();
584
- if ( !( domainType instanceof JdbcMapping ) || !( ( JdbcMapping ) domainType ) .getJdbcType ().isNumber () ) {
580
+ if ( !( nodeType .getSqmType () instanceof JdbcMapping jdbcMapping )
581
+ || !jdbcMapping .getJdbcType ().isNumber () ) {
585
582
throw new SemanticException (
586
583
"Operand of " + op .getOperatorChar () + " is of type '" + nodeType .getTypeName () +
587
584
"' which is not a numeric type (its JDBC type code is not numeric)"
0 commit comments