88import java .util .List ;
99
1010import jakarta .persistence .criteria .Nulls ;
11- import org .hibernate .metamodel .mapping .BasicValuedModelPart ;
1211import org .hibernate .metamodel .mapping .EmbeddableValuedModelPart ;
1312import org .hibernate .metamodel .mapping .EntityValuedModelPart ;
1413import org .hibernate .metamodel .mapping .ForeignKeyDescriptor ;
1817import org .hibernate .metamodel .mapping .ordering .ast .OrderingExpression ;
1918import org .hibernate .query .SortDirection ;
2019import org .hibernate .sql .ast .spi .SqlAstCreationState ;
21- import org .hibernate .sql .ast .spi .SqlSelection ;
2220import org .hibernate .sql .ast .tree .SqlAstNode ;
2321import org .hibernate .sql .ast .tree .expression .ColumnReference ;
2422import org .hibernate .sql .ast .tree .expression .Expression ;
2523import org .hibernate .sql .ast .tree .expression .SqlTuple ;
2624import org .hibernate .sql .ast .tree .from .TableGroup ;
27- import org .hibernate .sql .ast .tree .from .TableReference ;
2825import org .hibernate .sql .ast .tree .select .QuerySpec ;
2926import org .hibernate .sql .ast .tree .select .SelectClause ;
3027import org .hibernate .sql .ast .tree .select .SortSpecification ;
31- import org .hibernate .sql .results .graph .Fetchable ;
3228import org .hibernate .sql .results .internal .SqlSelectionImpl ;
3329
3430import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
@@ -61,9 +57,9 @@ public Expression resolve(
6157 TableGroup tableGroup ,
6258 String modelPartName ,
6359 SqlAstCreationState creationState ) {
64- final BasicValuedModelPart selection = referenceModelPart .asBasicValuedModelPart ();
60+ final var selection = referenceModelPart .asBasicValuedModelPart ();
6561 if ( selection != null ) {
66- final TableReference tableReference = tableGroup .resolveTableReference (
62+ final var tableReference = tableGroup .resolveTableReference (
6763 null ,
6864 selection ,
6965 selection .getContainingTableExpression ()
@@ -81,7 +77,7 @@ public Expression resolve(
8177 );
8278 }
8379 else if ( referenceModelPart instanceof EntityValuedModelPart entityValuedModelPart ) {
84- final ModelPart subPart =
80+ final var subPart =
8581 ELEMENT_TOKEN .equals ( modelPartName )
8682 ? entityValuedModelPart .getEntityMappingType ().getIdentifierMapping ()
8783 : entityValuedModelPart .findSubPart ( modelPartName );
@@ -93,13 +89,13 @@ else if ( referenceModelPart instanceof EmbeddableValuedModelPart embeddableValu
9389 final int size = embeddableValuedModelPart .getNumberOfFetchables ();
9490 final List <Expression > expressions = new ArrayList <>( size );
9591 for ( int i = 0 ; i < size ; i ++ ) {
96- final Fetchable fetchable = embeddableValuedModelPart .getFetchable ( i );
92+ final var fetchable = embeddableValuedModelPart .getFetchable ( i );
9793 expressions .add ( resolve ( fetchable , ast , tableGroup , modelPartName , creationState ) );
9894 }
9995 return new SqlTuple ( expressions , embeddableValuedModelPart );
10096 }
10197 else {
102- ModelPart subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
98+ final var subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
10399 assert subPart .asBasicValuedModelPart () != null ;
104100 return resolve ( subPart , ast , tableGroup , modelPartName , creationState );
105101 }
@@ -140,7 +136,7 @@ private void apply(
140136 SortDirection sortOrder ,
141137 Nulls nullPrecedence ,
142138 SqlAstCreationState creationState ) {
143- final BasicValuedModelPart basicPart = referenceModelPart .asBasicValuedModelPart ();
139+ final var basicPart = referenceModelPart .asBasicValuedModelPart ();
144140 if ( basicPart != null ) {
145141 addSortSpecification (
146142 basicPart ,
@@ -153,10 +149,11 @@ private void apply(
153149 );
154150 }
155151 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 );
160157 apply (
161158 subPart ,
162159 ast ,
@@ -195,8 +192,8 @@ private void addSortSpecification(
195192 SortDirection sortOrder ,
196193 Nulls nullPrecedence ,
197194 SqlAstCreationState creationState ) {
198- if ( embeddableValuedModelPart .getFetchableName ()
199- . equals ( modelPartName ) || ELEMENT_TOKEN .equals ( modelPartName ) ) {
195+ if ( embeddableValuedModelPart .getFetchableName (). equals ( modelPartName )
196+ || ELEMENT_TOKEN .equals ( modelPartName ) ) {
200197 embeddableValuedModelPart .forEachSelectable (
201198 (columnIndex , selection ) -> {
202199 addSortSpecification (
@@ -212,7 +209,7 @@ private void addSortSpecification(
212209 );
213210 }
214211 else {
215- ModelPart subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
212+ final var subPart = embeddableValuedModelPart .findSubPart ( modelPartName , null );
216213 addSortSpecification (
217214 castNonNull ( subPart .asBasicValuedModelPart () ),
218215 ast ,
@@ -233,49 +230,43 @@ private void addSortSpecification(
233230 SortDirection sortOrder ,
234231 Nulls nullPrecedence ,
235232 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+ );
248246 // It makes no sense to order by an expression multiple times
249247 // SQL Server even reports a query error in this case
250248 if ( ast .hasSortSpecifications () ) {
251- for ( SortSpecification sortSpecification : ast .getSortSpecifications () ) {
249+ for ( var sortSpecification : ast .getSortSpecifications () ) {
252250 if ( sortSpecification .getSortExpression () == expression ) {
253251 return ;
254252 }
255253 }
256254 }
257255
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 ) ) {
261259 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 ) );
267261 }
268262
269- final Expression sortExpression = OrderingExpression .applyCollation (
270- expression ,
271- collation ,
272- creationState
273- );
263+ final var sortExpression =
264+ OrderingExpression .applyCollation ( expression , collation , creationState );
274265 ast .addSortSpecification ( new SortSpecification ( sortExpression , sortOrder , nullPrecedence ) );
275266 }
276267
277268 private static boolean selectClauseDoesNotContainOrderExpression (Expression expression , SelectClause selectClause ) {
278- for ( SqlSelection sqlSelection : selectClause .getSqlSelections () ) {
269+ for ( var sqlSelection : selectClause .getSqlSelections () ) {
279270 if ( sqlSelection .getExpression ().equals ( expression ) ) {
280271 return false ;
281272 }
0 commit comments