Skip to content

Commit 831e5e2

Browse files
committed
big cleanup to o.h.metamodel.mapping.internal
1 parent beab449 commit 831e5e2

File tree

44 files changed

+1048
-1260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1048
-1260
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDiscriminatorMapping.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.hibernate.metamodel.model.domain.NavigableRole;
2020
import org.hibernate.spi.NavigablePath;
2121
import org.hibernate.sql.ast.spi.SqlAstCreationState;
22-
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
2322
import org.hibernate.sql.ast.spi.SqlSelection;
2423
import org.hibernate.sql.ast.tree.from.TableGroup;
2524
import org.hibernate.sql.results.graph.DomainResult;
@@ -49,10 +48,8 @@ public AbstractDiscriminatorMapping(
4948
BasicType<Object> underlyingJdbcMapping) {
5049
this.underlyingJdbcMapping = underlyingJdbcMapping;
5150
this.mappingType = mappingType;
52-
53-
this.role = mappingType.getNavigableRole().append( DISCRIMINATOR_ROLE_NAME );
54-
5551
this.discriminatorType = discriminatorType;
52+
this.role = mappingType.getNavigableRole().append( DISCRIMINATOR_ROLE_NAME );
5653
}
5754

5855
public EntityMappingType getEntityDescriptor() {
@@ -106,7 +103,7 @@ public DomainResult createDomainResult(
106103
String resultVariable,
107104
DomainResultCreationState creationState) {
108105
// create a SqlSelection based on the underlying JdbcMapping
109-
final SqlSelection sqlSelection = resolveSqlSelection(
106+
final var sqlSelection = resolveSqlSelection(
110107
navigablePath,
111108
underlyingJdbcMapping,
112109
tableGroup,
@@ -132,8 +129,7 @@ private SqlSelection resolveSqlSelection(
132129
TableGroup tableGroup,
133130
FetchParent fetchParent,
134131
SqlAstCreationState creationState) {
135-
final SqlExpressionResolver expressionResolver = creationState.getSqlExpressionResolver();
136-
return expressionResolver.resolveSqlSelection(
132+
return creationState.getSqlExpressionResolver().resolveSqlSelection(
137133
resolveSqlExpression( navigablePath, jdbcMappingToUse, tableGroup, creationState ),
138134
jdbcMappingToUse.getJdbcJavaType(),
139135
fetchParent,
@@ -149,15 +145,13 @@ public BasicFetch<?> generateFetch(
149145
boolean selected,
150146
String resultVariable,
151147
DomainResultCreationState creationState) {
152-
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
153-
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(
154-
fetchParent.getNavigablePath()
155-
);
156-
148+
final var tableGroup =
149+
creationState.getSqlAstCreationState().getFromClauseAccess()
150+
.getTableGroup( fetchParent.getNavigablePath() );
157151
assert tableGroup != null;
158152

159153
// create a SqlSelection based on the underlying JdbcMapping
160-
final SqlSelection sqlSelection = resolveSqlSelection(
154+
final var sqlSelection = resolveSqlSelection(
161155
fetchablePath,
162156
underlyingJdbcMapping,
163157
tableGroup,

hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/AbstractDomainPath.java

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.List;
99

1010
import jakarta.persistence.criteria.Nulls;
11-
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
1211
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
1312
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
1413
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
@@ -18,17 +17,14 @@
1817
import org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression;
1918
import org.hibernate.query.SortDirection;
2019
import org.hibernate.sql.ast.spi.SqlAstCreationState;
21-
import org.hibernate.sql.ast.spi.SqlSelection;
2220
import org.hibernate.sql.ast.tree.SqlAstNode;
2321
import org.hibernate.sql.ast.tree.expression.ColumnReference;
2422
import org.hibernate.sql.ast.tree.expression.Expression;
2523
import org.hibernate.sql.ast.tree.expression.SqlTuple;
2624
import org.hibernate.sql.ast.tree.from.TableGroup;
27-
import org.hibernate.sql.ast.tree.from.TableReference;
2825
import org.hibernate.sql.ast.tree.select.QuerySpec;
2926
import org.hibernate.sql.ast.tree.select.SelectClause;
3027
import org.hibernate.sql.ast.tree.select.SortSpecification;
31-
import org.hibernate.sql.results.graph.Fetchable;
3228
import org.hibernate.sql.results.internal.SqlSelectionImpl;
3329

3430
import 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

Comments
 (0)