Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.DomainResult;
Expand Down Expand Up @@ -49,10 +48,8 @@ public AbstractDiscriminatorMapping(
BasicType<Object> underlyingJdbcMapping) {
this.underlyingJdbcMapping = underlyingJdbcMapping;
this.mappingType = mappingType;

this.role = mappingType.getNavigableRole().append( DISCRIMINATOR_ROLE_NAME );

this.discriminatorType = discriminatorType;
this.role = mappingType.getNavigableRole().append( DISCRIMINATOR_ROLE_NAME );
}

public EntityMappingType getEntityDescriptor() {
Expand Down Expand Up @@ -106,7 +103,7 @@ public DomainResult createDomainResult(
String resultVariable,
DomainResultCreationState creationState) {
// create a SqlSelection based on the underlying JdbcMapping
final SqlSelection sqlSelection = resolveSqlSelection(
final var sqlSelection = resolveSqlSelection(
navigablePath,
underlyingJdbcMapping,
tableGroup,
Expand All @@ -132,8 +129,7 @@ private SqlSelection resolveSqlSelection(
TableGroup tableGroup,
FetchParent fetchParent,
SqlAstCreationState creationState) {
final SqlExpressionResolver expressionResolver = creationState.getSqlExpressionResolver();
return expressionResolver.resolveSqlSelection(
return creationState.getSqlExpressionResolver().resolveSqlSelection(
resolveSqlExpression( navigablePath, jdbcMappingToUse, tableGroup, creationState ),
jdbcMappingToUse.getJdbcJavaType(),
fetchParent,
Expand All @@ -149,15 +145,13 @@ public BasicFetch<?> generateFetch(
boolean selected,
String resultVariable,
DomainResultCreationState creationState) {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(
fetchParent.getNavigablePath()
);

final var tableGroup =
creationState.getSqlAstCreationState().getFromClauseAccess()
.getTableGroup( fetchParent.getNavigablePath() );
assert tableGroup != null;

// create a SqlSelection based on the underlying JdbcMapping
final SqlSelection sqlSelection = resolveSqlSelection(
final var sqlSelection = resolveSqlSelection(
fetchablePath,
underlyingJdbcMapping,
tableGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;

import jakarta.persistence.criteria.Nulls;
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.EntityValuedModelPart;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
Expand All @@ -18,17 +17,14 @@
import org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression;
import org.hibernate.query.SortDirection;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectClause;
import org.hibernate.sql.ast.tree.select.SortSpecification;
import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.internal.SqlSelectionImpl;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;
Expand Down Expand Up @@ -61,9 +57,9 @@ public Expression resolve(
TableGroup tableGroup,
String modelPartName,
SqlAstCreationState creationState) {
final BasicValuedModelPart selection = referenceModelPart.asBasicValuedModelPart();
final var selection = referenceModelPart.asBasicValuedModelPart();
if ( selection != null ) {
final TableReference tableReference = tableGroup.resolveTableReference(
final var tableReference = tableGroup.resolveTableReference(
null,
selection,
selection.getContainingTableExpression()
Expand All @@ -81,7 +77,7 @@ public Expression resolve(
);
}
else if ( referenceModelPart instanceof EntityValuedModelPart entityValuedModelPart ) {
final ModelPart subPart =
final var subPart =
ELEMENT_TOKEN.equals( modelPartName )
? entityValuedModelPart.getEntityMappingType().getIdentifierMapping()
: entityValuedModelPart.findSubPart( modelPartName );
Expand All @@ -93,13 +89,13 @@ else if ( referenceModelPart instanceof EmbeddableValuedModelPart embeddableValu
final int size = embeddableValuedModelPart.getNumberOfFetchables();
final List<Expression> expressions = new ArrayList<>( size );
for ( int i = 0; i < size; i++ ) {
final Fetchable fetchable = embeddableValuedModelPart.getFetchable( i );
final var fetchable = embeddableValuedModelPart.getFetchable( i );
expressions.add( resolve( fetchable, ast, tableGroup, modelPartName, creationState ) );
}
return new SqlTuple( expressions, embeddableValuedModelPart );
}
else {
ModelPart subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
final var subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
assert subPart.asBasicValuedModelPart() != null;
return resolve( subPart, ast, tableGroup, modelPartName, creationState );
}
Expand Down Expand Up @@ -140,7 +136,7 @@ private void apply(
SortDirection sortOrder,
Nulls nullPrecedence,
SqlAstCreationState creationState) {
final BasicValuedModelPart basicPart = referenceModelPart.asBasicValuedModelPart();
final var basicPart = referenceModelPart.asBasicValuedModelPart();
if ( basicPart != null ) {
addSortSpecification(
basicPart,
Expand All @@ -153,10 +149,11 @@ private void apply(
);
}
else if ( referenceModelPart instanceof EntityValuedModelPart entityValuedModelPart ) {
final ModelPart subPart = ELEMENT_TOKEN.equals( modelPartName )
? entityValuedModelPart.getEntityMappingType().getIdentifierMapping()
// Default to using the foreign key of an entity valued model part
: entityValuedModelPart.findSubPart( ForeignKeyDescriptor.PART_NAME );
final var subPart =
ELEMENT_TOKEN.equals( modelPartName )
? entityValuedModelPart.getEntityMappingType().getIdentifierMapping()
// Default to using the foreign key of an entity valued model part
: entityValuedModelPart.findSubPart( ForeignKeyDescriptor.PART_NAME );
apply(
subPart,
ast,
Expand Down Expand Up @@ -195,8 +192,8 @@ private void addSortSpecification(
SortDirection sortOrder,
Nulls nullPrecedence,
SqlAstCreationState creationState) {
if ( embeddableValuedModelPart.getFetchableName()
.equals( modelPartName ) || ELEMENT_TOKEN.equals( modelPartName ) ) {
if ( embeddableValuedModelPart.getFetchableName().equals( modelPartName )
|| ELEMENT_TOKEN.equals( modelPartName ) ) {
embeddableValuedModelPart.forEachSelectable(
(columnIndex, selection) -> {
addSortSpecification(
Expand All @@ -212,7 +209,7 @@ private void addSortSpecification(
);
}
else {
ModelPart subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
final var subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
addSortSpecification(
castNonNull( subPart.asBasicValuedModelPart() ),
ast,
Expand All @@ -233,49 +230,43 @@ private void addSortSpecification(
SortDirection sortOrder,
Nulls nullPrecedence,
SqlAstCreationState creationState) {
final TableReference tableReference = tableGroup.resolveTableReference( null, selection.getContainingTableExpression() );
final Expression expression = creationState.getSqlExpressionResolver().resolveSqlExpression(
createColumnReferenceKey(
tableReference,
selection.getSelectionExpression(),
selection.getJdbcMapping()
),
processingState -> new ColumnReference(
tableReference,
selection
)
);
final var tableReference =
tableGroup.resolveTableReference( null,
selection.getContainingTableExpression() );
final var expression =
creationState.getSqlExpressionResolver()
.resolveSqlExpression(
createColumnReferenceKey(
tableReference,
selection.getSelectionExpression(),
selection.getJdbcMapping()
),
processingState -> new ColumnReference( tableReference, selection )
);
// It makes no sense to order by an expression multiple times
// SQL Server even reports a query error in this case
if ( ast.hasSortSpecifications() ) {
for ( SortSpecification sortSpecification : ast.getSortSpecifications() ) {
for ( var sortSpecification : ast.getSortSpecifications() ) {
if ( sortSpecification.getSortExpression() == expression ) {
return;
}
}
}

final SelectClause selectClause = ast.getSelectClause();

if ( selectClause.isDistinct() && selectClauseDoesNotContainOrderExpression( expression, selectClause ) ) {
final var selectClause = ast.getSelectClause();
if ( selectClause.isDistinct()
&& selectClauseDoesNotContainOrderExpression( expression, selectClause ) ) {
final int valuesArrayPosition = selectClause.getSqlSelections().size();
SqlSelection sqlSelection = new SqlSelectionImpl(
valuesArrayPosition,
expression
);
selectClause.addSqlSelection( sqlSelection );
selectClause.addSqlSelection( new SqlSelectionImpl( valuesArrayPosition, expression ) );
}

final Expression sortExpression = OrderingExpression.applyCollation(
expression,
collation,
creationState
);
final var sortExpression =
OrderingExpression.applyCollation( expression, collation, creationState );
ast.addSortSpecification( new SortSpecification( sortExpression, sortOrder, nullPrecedence ) );
}

private static boolean selectClauseDoesNotContainOrderExpression(Expression expression, SelectClause selectClause) {
for ( SqlSelection sqlSelection : selectClause.getSqlSelections() ) {
for ( var sqlSelection : selectClause.getSqlSelections() ) {
if ( sqlSelection.getExpression().equals( expression ) ) {
return false;
}
Expand Down
Loading