Skip to content

Commit

Permalink
HHH-17406 Remove instanceof / cast usages of BasicValuedModelPart
Browse files Browse the repository at this point in the history
This should help with type pollution, though it's not strictly needed for the scope of the generated values change.
  • Loading branch information
mbladel committed Dec 22, 2023
1 parent cd9e86a commit ecececa
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,11 @@ public FetchMemento resolve(ResultSetMappingResolutionContext resolutionContext)
navigablePath = navigablePath.append( fetchable.getFetchableName() );
}

if ( fetchable instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicPart = fetchable.asBasicValuedModelPart();
if ( basicPart != null ) {
return new FetchMementoBasicStandard(
navigablePath,
(BasicValuedModelPart) fetchable,
basicPart,
columnAliases.get( 0 )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,9 @@ public ResultMemento asResultMemento(NavigablePath path, ResultSetMappingResolut

final ModelPart subPart = entityMapping.findSubPart( propertyPath, null );

//noinspection StatementWithEmptyBody
if ( subPart == null ) {
// throw an exception
}

if ( subPart instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicPart = subPart != null ? subPart.asBasicValuedModelPart() : null;
if ( basicPart != null ) {
assert columnNames.size() == 1;
final BasicValuedModelPart basicPart = (BasicValuedModelPart) subPart;

return new ModelPartResultMementoBasicImpl( path, basicPart, columnNames.get( 0 ) );
}
Expand Down Expand Up @@ -405,10 +400,9 @@ public FetchMemento resolve(ResultSetMappingResolutionContext resolutionContext)
}

private FetchMemento getFetchMemento(NavigablePath navigablePath, ModelPart subPart) {
if ( subPart instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicPart = subPart.asBasicValuedModelPart();
if ( basicPart != null ) {
assert columnNames.size() == 1;
final BasicValuedModelPart basicPart = (BasicValuedModelPart) subPart;

return new FetchMementoBasicStandard( navigablePath, basicPart, columnNames.get( 0 ) );
}
else if ( subPart instanceof EntityValuedFetchable ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ else if ( loadQueryInfluencers.getEnabledCascadingFetchProfile() != null ) {
}
else if ( fetchDepth > maximumFetchDepth + 1 ) {
// We can preserve the existing value of joined for basic and embedded values
if ( !( fetchable instanceof BasicValuedModelPart ) && !( fetchable instanceof EmbeddedAttributeMapping ) ) {
if ( fetchable.asBasicValuedModelPart() == null && !( fetchable instanceof EmbeddedAttributeMapping ) ) {
joined = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.hibernate.persister.entity.UnionSubclassEntityPersister;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;

/**
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -134,18 +136,21 @@ else if ( attribute1 instanceof EmbeddableValuedModelPart ) {
}
return true;
}
else if ( attribute1 instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basic1 = (BasicValuedModelPart) attribute1;
final BasicValuedModelPart basic2 = (BasicValuedModelPart) attribute2;
if ( !basic1.getSelectionExpression().equals( basic2.getSelectionExpression() ) ) {
return false;
}
if ( basic1.getContainingTableExpression().equals( basic2.getContainingTableExpression() ) ) {
return true;
else {
final BasicValuedModelPart basic1 = attribute1.asBasicValuedModelPart();
if ( basic1 != null ) {
final BasicValuedModelPart basic2 = castNonNull( attribute2.asBasicValuedModelPart() );
if ( !basic1.getSelectionExpression().equals( basic2.getSelectionExpression() ) ) {
return false;
}
if ( basic1.getContainingTableExpression().equals( basic2.getContainingTableExpression() ) ) {
return true;
}
// For union subclass mappings we also consider mappings compatible that just match the selection expression,
// because we match up columns of disjoint union subclass types by column name
return attribute1.findContainingEntityMapping()
.getEntityPersister() instanceof UnionSubclassEntityPersister;
}
// For union subclass mappings we also consider mappings compatible that just match the selection expression,
// because we match up columns of disjoint union subclass types by column name
return attribute1.findContainingEntityMapping().getEntityPersister() instanceof UnionSubclassEntityPersister;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.internal.SqlSelectionImpl;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;
import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey;

/**
Expand Down Expand Up @@ -61,8 +62,8 @@ public Expression resolve(
TableGroup tableGroup,
String modelPartName,
SqlAstCreationState creationState) {
if ( referenceModelPart instanceof BasicValuedModelPart ) {
final BasicValuedModelPart selection = (BasicValuedModelPart) referenceModelPart;
final BasicValuedModelPart selection = referenceModelPart.asBasicValuedModelPart();
if ( selection != null ) {
final TableReference tableReference = tableGroup.resolveTableReference(
null,
selection,
Expand Down Expand Up @@ -104,7 +105,7 @@ else if ( referenceModelPart instanceof EmbeddableValuedModelPart ) {
}
else {
ModelPart subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
assert subPart instanceof BasicValuedModelPart;
assert subPart.asBasicValuedModelPart() != null;
return resolve( subPart, ast, tableGroup, modelPartName, creationState );
}
}
Expand Down Expand Up @@ -144,9 +145,10 @@ public void apply(
SortDirection sortOrder,
NullPrecedence nullPrecedence,
SqlAstCreationState creationState) {
if ( referenceModelPart instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicPart = referenceModelPart.asBasicValuedModelPart();
if ( basicPart != null ) {
addSortSpecification(
(BasicValuedModelPart) referenceModelPart,
basicPart,
ast,
tableGroup,
collation,
Expand Down Expand Up @@ -231,9 +233,8 @@ private void addSortSpecification(
}
else {
ModelPart subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
assert subPart instanceof BasicValuedModelPart;
addSortSpecification(
(BasicValuedModelPart) subPart,
castNonNull( subPart.asBasicValuedModelPart() ),
ast,
tableGroup,
collation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,14 +620,15 @@ private ForeignKeyDescriptor createForeignKeyDescriptor(

final String collectionTableName = ( (CollectionMutationTarget) getCollectionDescriptor() ).getCollectionTableMapping().getTableName();

if ( fkTargetModelPart instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicFkTarget = fkTargetModelPart.asBasicValuedModelPart();
if ( basicFkTarget != null ) {
return createSimpleForeignKeyDescriptor(
fkBootDescriptorSource,
entityType,
creationProcess,
dialect,
collectionTableName,
(BasicValuedModelPart) fkTargetModelPart
basicFkTarget
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;
import static org.hibernate.metamodel.mapping.MappingModelCreationLogging.MAPPING_MODEL_CREATION_MESSAGE_LOGGER;
import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey;

Expand Down Expand Up @@ -767,9 +768,8 @@ private static void interpretPluralAttributeMappingKeyDescriptor(

if ( keyType instanceof BasicType ) {
assert bootValueMappingKey.getColumnSpan() == 1;
assert fkTargetPart instanceof BasicValuedModelPart;

final BasicValuedModelPart simpleFkTargetPart = (BasicValuedModelPart) fkTargetPart;
final BasicValuedModelPart simpleFkTargetPart = castNonNull( fkTargetPart.asBasicValuedModelPart() );

final String keyTableExpression = collectionTableName;//getTableIdentifierExpression( bootValueMappingKey.getTable(), creationProcess );
final SelectableMapping keySelectableMapping = SelectableMappingImpl.from(
Expand Down Expand Up @@ -919,8 +919,8 @@ else if ( modelPart == null ) {
fkTarget = referencedEntityDescriptor.findByPath( bootValueMapping.getReferencedPropertyName() );
}

if ( fkTarget instanceof BasicValuedModelPart ) {
final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget;
final BasicValuedModelPart simpleFkTarget = fkTarget.asBasicValuedModelPart();
if ( simpleFkTarget != null ) {
final Iterator<Selectable> columnIterator = bootValueMapping.getSelectables().iterator();
final Table table = bootValueMapping.getTable();
final String tableExpression = getTableIdentifierExpression( table, creationProcess );
Expand Down Expand Up @@ -1595,8 +1595,7 @@ public static Expression buildColumnReferenceExpression(
return new SqlTuple( columnReferences, modelPart );
}
else {
assert modelPart instanceof BasicValuedModelPart;
final BasicValuedModelPart basicPart = (BasicValuedModelPart) modelPart;
final BasicValuedModelPart basicPart = castNonNull( modelPart.asBasicValuedModelPart() );
final String qualifier;
if ( tableGroup == null ) {
qualifier = basicPart.getContainingTableExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public ForeignKeyDescriptor withKeySelectionMapping(
public ForeignKeyDescriptor withTargetPart(ValuedModelPart targetPart) {
return new SimpleForeignKeyDescriptor(
keySide.getModelPart(),
(BasicValuedModelPart) targetPart,
targetPart.asBasicValuedModelPart(),
refersToPrimaryKey,
hasConstraint,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ private ImmutableFetchList fetchProcessor(FetchParent fetchParent, LoaderSqlAstC
// Ignore plural attributes
if ( !( fetchable instanceof PluralAttributeMapping ) ) {
final FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
if ( fetchable instanceof BasicValuedModelPart ) {
if ( fetchable.asBasicValuedModelPart() != null ) {
// Ignore lazy basic columns
if ( fetchTiming == FetchTiming.DELAYED ) {
continue;
Expand Down Expand Up @@ -4660,7 +4660,7 @@ protected List<? extends ModelPart> initInsertGeneratedProperties(List<Attribute
final List<ModelPart> generatedBasicAttributes = new ArrayList<>( originalSize );
for ( AttributeMapping generatedAttribute : generatedAttributes ) {
// todo (7.0) : support non selectable mappings? Component, ToOneAttributeMapping, ...
if ( generatedAttribute instanceof BasicValuedModelPart
if ( generatedAttribute.asBasicValuedModelPart() != null
&& generatedAttribute.getContainingTableExpression().equals( getSubclassTableName( 0 ) ) ) {
generatedBasicAttributes.add( generatedAttribute );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public NavigablePath getNavigablePath() {
public ResultBuilder resolve(
Consumer<String> querySpaceConsumer,
ResultSetMappingResolutionContext context) {
if ( referencedModelPart instanceof BasicValuedModelPart ) {
return new ImplicitModelPartResultBuilderBasic( navigablePath, (BasicValuedModelPart) referencedModelPart );
final BasicValuedModelPart basicPart = referencedModelPart.asBasicValuedModelPart();
if ( basicPart != null ) {
return new ImplicitModelPartResultBuilderBasic( navigablePath, basicPart );
}

if ( referencedModelPart instanceof EmbeddableValuedModelPart ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public ResultBuilderEntityValued resolve(
// Implicit basic fetches are DELAYED by default, so register fetch builders for the remaining basic fetchables
entityDescriptor.forEachAttributeMapping(
attributeMapping -> {
final Function<String, FetchBuilder> fetchBuilderCreator;
if ( attributeMapping instanceof BasicValuedModelPart ) {
fetchBuilderCreator = k -> new DelayedFetchBuilderBasicPart(
final BasicValuedModelPart basicPart = attributeMapping.asBasicValuedModelPart();
if ( basicPart != null ) {
final Function<String, FetchBuilder> fetchBuilderCreator = k -> new DelayedFetchBuilderBasicPart(
navigablePath.append( k ),
(BasicValuedModelPart) attributeMapping,
basicPart,
isEnhancedForLazyLoading
);
explicitFetchBuilderMap.computeIfAbsent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ public static ImplicitFetchBuilder implicitFetchBuilder(
NavigablePath fetchPath,
Fetchable fetchable,
DomainResultCreationState creationState) {
if ( fetchable instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicValuedFetchable = (BasicValuedModelPart) fetchable;
final BasicValuedModelPart basicValuedFetchable = fetchable.asBasicValuedModelPart();
if ( basicValuedFetchable != null ) {
return new ImplicitFetchBuilderBasic( fetchPath, basicValuedFetchable, creationState );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public Fetch buildFetch(
final Fetchable attributeMapping = (Fetchable) parent.getReferencedMappingContainer().findSubPart( fetchableName, null );
final SqlExpressionResolver sqlExpressionResolver = domainResultCreationState.getSqlAstCreationState().getSqlExpressionResolver();

if ( attributeMapping instanceof BasicValuedModelPart ) {
final BasicValuedModelPart basicPart = attributeMapping.asBasicValuedModelPart();
if ( basicPart != null ) {
attributeMapping.forEachSelectable(
getSelectableConsumer(
fetchPath,
Expand All @@ -92,7 +93,7 @@ public Fetch buildFetch(
creationStateImpl,
ownerTableGroup,
sqlExpressionResolver,
(BasicValuedModelPart) attributeMapping
basicPart
)
);
return parent.generateFetchableFetch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.hibernate.sql.ast.tree.predicate.InListPredicate;
import org.hibernate.sql.exec.spi.ExecutionContext;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;

/**
* MatchingIdRestrictionProducer producing a restriction based on an in-values-list predicate. E.g.:
*
Expand Down Expand Up @@ -88,7 +90,7 @@ public InListPredicate produceRestriction(
final InListPredicate predicate;

if ( idColumnCount == 1 ) {
final BasicValuedModelPart basicIdMapping = (BasicValuedModelPart) identifierMapping;
final BasicValuedModelPart basicIdMapping = castNonNull( identifierMapping.asBasicValuedModelPart() );
final String idColumn = basicIdMapping.getSelectionExpression();
final Expression inFixture = new ColumnReference(
mutatingTableReference,
Expand Down
Loading

0 comments on commit ecececa

Please sign in to comment.