8
8
import java .util .List ;
9
9
10
10
import jakarta .persistence .criteria .Nulls ;
11
- import org .hibernate .metamodel .mapping .BasicValuedModelPart ;
12
11
import org .hibernate .metamodel .mapping .EmbeddableValuedModelPart ;
13
12
import org .hibernate .metamodel .mapping .EntityValuedModelPart ;
14
13
import org .hibernate .metamodel .mapping .ForeignKeyDescriptor ;
18
17
import org .hibernate .metamodel .mapping .ordering .ast .OrderingExpression ;
19
18
import org .hibernate .query .SortDirection ;
20
19
import org .hibernate .sql .ast .spi .SqlAstCreationState ;
21
- import org .hibernate .sql .ast .spi .SqlSelection ;
22
20
import org .hibernate .sql .ast .tree .SqlAstNode ;
23
21
import org .hibernate .sql .ast .tree .expression .ColumnReference ;
24
22
import org .hibernate .sql .ast .tree .expression .Expression ;
25
23
import org .hibernate .sql .ast .tree .expression .SqlTuple ;
26
24
import org .hibernate .sql .ast .tree .from .TableGroup ;
27
- import org .hibernate .sql .ast .tree .from .TableReference ;
28
25
import org .hibernate .sql .ast .tree .select .QuerySpec ;
29
26
import org .hibernate .sql .ast .tree .select .SelectClause ;
30
27
import org .hibernate .sql .ast .tree .select .SortSpecification ;
31
- import org .hibernate .sql .results .graph .Fetchable ;
32
28
import org .hibernate .sql .results .internal .SqlSelectionImpl ;
33
29
34
30
import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
@@ -61,9 +57,9 @@ public Expression resolve(
61
57
TableGroup tableGroup ,
62
58
String modelPartName ,
63
59
SqlAstCreationState creationState ) {
64
- final BasicValuedModelPart selection = referenceModelPart .asBasicValuedModelPart ();
60
+ final var selection = referenceModelPart .asBasicValuedModelPart ();
65
61
if ( selection != null ) {
66
- final TableReference tableReference = tableGroup .resolveTableReference (
62
+ final var tableReference = tableGroup .resolveTableReference (
67
63
null ,
68
64
selection ,
69
65
selection .getContainingTableExpression ()
@@ -81,7 +77,7 @@ public Expression resolve(
81
77
);
82
78
}
83
79
else if ( referenceModelPart instanceof EntityValuedModelPart entityValuedModelPart ) {
84
- final ModelPart subPart =
80
+ final var subPart =
85
81
ELEMENT_TOKEN .equals ( modelPartName )
86
82
? entityValuedModelPart .getEntityMappingType ().getIdentifierMapping ()
87
83
: entityValuedModelPart .findSubPart ( modelPartName );
@@ -93,13 +89,13 @@ else if ( referenceModelPart instanceof EmbeddableValuedModelPart embeddableValu
93
89
final int size = embeddableValuedModelPart .getNumberOfFetchables ();
94
90
final List <Expression > expressions = new ArrayList <>( size );
95
91
for ( int i = 0 ; i < size ; i ++ ) {
96
- final Fetchable fetchable = embeddableValuedModelPart .getFetchable ( i );
92
+ final var fetchable = embeddableValuedModelPart .getFetchable ( i );
97
93
expressions .add ( resolve ( fetchable , ast , tableGroup , modelPartName , creationState ) );
98
94
}
99
95
return new SqlTuple ( expressions , embeddableValuedModelPart );
100
96
}
101
97
else {
102
- ModelPart subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
98
+ final var subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
103
99
assert subPart .asBasicValuedModelPart () != null ;
104
100
return resolve ( subPart , ast , tableGroup , modelPartName , creationState );
105
101
}
@@ -140,7 +136,7 @@ private void apply(
140
136
SortDirection sortOrder ,
141
137
Nulls nullPrecedence ,
142
138
SqlAstCreationState creationState ) {
143
- final BasicValuedModelPart basicPart = referenceModelPart .asBasicValuedModelPart ();
139
+ final var basicPart = referenceModelPart .asBasicValuedModelPart ();
144
140
if ( basicPart != null ) {
145
141
addSortSpecification (
146
142
basicPart ,
@@ -153,10 +149,11 @@ private void apply(
153
149
);
154
150
}
155
151
else if ( referenceModelPart instanceof EntityValuedModelPart entityValuedModelPart ) {
156
- final ModelPart subPart = ELEMENT_TOKEN .equals ( modelPartName )
157
- ? entityValuedModelPart .getEntityMappingType ().getIdentifierMapping ()
158
- // Default to using the foreign key of an entity valued model part
159
- : entityValuedModelPart .findSubPart ( ForeignKeyDescriptor .PART_NAME );
152
+ final var subPart =
153
+ ELEMENT_TOKEN .equals ( modelPartName )
154
+ ? entityValuedModelPart .getEntityMappingType ().getIdentifierMapping ()
155
+ // Default to using the foreign key of an entity valued model part
156
+ : entityValuedModelPart .findSubPart ( ForeignKeyDescriptor .PART_NAME );
160
157
apply (
161
158
subPart ,
162
159
ast ,
@@ -195,8 +192,8 @@ private void addSortSpecification(
195
192
SortDirection sortOrder ,
196
193
Nulls nullPrecedence ,
197
194
SqlAstCreationState creationState ) {
198
- if ( embeddableValuedModelPart .getFetchableName ()
199
- . equals ( modelPartName ) || ELEMENT_TOKEN .equals ( modelPartName ) ) {
195
+ if ( embeddableValuedModelPart .getFetchableName (). equals ( modelPartName )
196
+ || ELEMENT_TOKEN .equals ( modelPartName ) ) {
200
197
embeddableValuedModelPart .forEachSelectable (
201
198
(columnIndex , selection ) -> {
202
199
addSortSpecification (
@@ -212,7 +209,7 @@ private void addSortSpecification(
212
209
);
213
210
}
214
211
else {
215
- ModelPart subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
212
+ final var subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
216
213
addSortSpecification (
217
214
castNonNull ( subPart .asBasicValuedModelPart () ),
218
215
ast ,
@@ -233,49 +230,43 @@ private void addSortSpecification(
233
230
SortDirection sortOrder ,
234
231
Nulls nullPrecedence ,
235
232
SqlAstCreationState creationState ) {
236
- final TableReference tableReference = tableGroup .resolveTableReference ( null , selection .getContainingTableExpression () );
237
- final Expression expression = creationState .getSqlExpressionResolver ().resolveSqlExpression (
238
- createColumnReferenceKey (
239
- tableReference ,
240
- selection .getSelectionExpression (),
241
- selection .getJdbcMapping ()
242
- ),
243
- processingState -> new ColumnReference (
244
- tableReference ,
245
- selection
246
- )
247
- );
233
+ final var tableReference =
234
+ tableGroup .resolveTableReference ( null ,
235
+ selection .getContainingTableExpression () );
236
+ final var expression =
237
+ creationState .getSqlExpressionResolver ()
238
+ .resolveSqlExpression (
239
+ createColumnReferenceKey (
240
+ tableReference ,
241
+ selection .getSelectionExpression (),
242
+ selection .getJdbcMapping ()
243
+ ),
244
+ processingState -> new ColumnReference ( tableReference , selection )
245
+ );
248
246
// It makes no sense to order by an expression multiple times
249
247
// SQL Server even reports a query error in this case
250
248
if ( ast .hasSortSpecifications () ) {
251
- for ( SortSpecification sortSpecification : ast .getSortSpecifications () ) {
249
+ for ( var sortSpecification : ast .getSortSpecifications () ) {
252
250
if ( sortSpecification .getSortExpression () == expression ) {
253
251
return ;
254
252
}
255
253
}
256
254
}
257
255
258
- final SelectClause selectClause = ast .getSelectClause ();
259
-
260
- if ( selectClause . isDistinct () && selectClauseDoesNotContainOrderExpression ( expression , selectClause ) ) {
256
+ final var selectClause = ast .getSelectClause ();
257
+ if ( selectClause . isDistinct ()
258
+ && selectClauseDoesNotContainOrderExpression ( expression , selectClause ) ) {
261
259
final int valuesArrayPosition = selectClause .getSqlSelections ().size ();
262
- SqlSelection sqlSelection = new SqlSelectionImpl (
263
- valuesArrayPosition ,
264
- expression
265
- );
266
- selectClause .addSqlSelection ( sqlSelection );
260
+ selectClause .addSqlSelection ( new SqlSelectionImpl ( valuesArrayPosition , expression ) );
267
261
}
268
262
269
- final Expression sortExpression = OrderingExpression .applyCollation (
270
- expression ,
271
- collation ,
272
- creationState
273
- );
263
+ final var sortExpression =
264
+ OrderingExpression .applyCollation ( expression , collation , creationState );
274
265
ast .addSortSpecification ( new SortSpecification ( sortExpression , sortOrder , nullPrecedence ) );
275
266
}
276
267
277
268
private static boolean selectClauseDoesNotContainOrderExpression (Expression expression , SelectClause selectClause ) {
278
- for ( SqlSelection sqlSelection : selectClause .getSqlSelections () ) {
269
+ for ( var sqlSelection : selectClause .getSqlSelections () ) {
279
270
if ( sqlSelection .getExpression ().equals ( expression ) ) {
280
271
return false ;
281
272
}
0 commit comments