HHH-16705 AttributeMappingsList should not implement Iterable

This commit is contained in:
Sanne Grinovero 2023-05-18 21:05:18 +01:00 committed by Sanne Grinovero
parent 4e9789bdff
commit 310a3d2997
14 changed files with 51 additions and 51 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.engine.profile;
import org.hibernate.metamodel.RuntimeMetamodels;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.sql.results.graph.FetchOptions;
@ -55,7 +56,9 @@ public class DefaultFetchProfile extends FetchProfile {
@Override
public boolean hasSubselectLoadableCollectionsEnabled(EntityPersister persister) {
final EntityMappingType entity = metamodels.getEntityMappingType( persister.getEntityName() );
for ( AttributeMapping attributeMapping : entity.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = entity.getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeMapping.getMappedFetchOptions().getStyle() == SUBSELECT ) {
return true;
}

View File

@ -28,7 +28,7 @@ import org.hibernate.metamodel.mapping.internal.ImmutableAttributeMappingList;
* @since 6.2
*/
@Incubating
public interface AttributeMappingsList extends Iterable<AttributeMapping> {
public interface AttributeMappingsList {
int size();

View File

@ -259,7 +259,9 @@ public interface EmbeddableMappingType extends ManagedMappingType, SelectableMap
}
default int compare(Object value1, Object value2) {
for ( AttributeMapping attributeMapping : getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attributeMapping = attributeMappings.get( i );
final Getter getter = attributeMapping.getPropertyAccess().getGetter();
final int comparison = attributeMapping.compare( getter.get( value1 ), getter.get( value2 ) );
if ( comparison != 0 ) {

View File

@ -103,7 +103,9 @@ public interface ManagedMappingType extends MappingType, FetchableContainer {
@Override
default boolean hasPartitionedSelectionMapping() {
for ( AttributeMapping attributeMapping : getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeMapping.hasPartitionedSelectionMapping() ) {
return true;
}

View File

@ -38,11 +38,6 @@ public final class ImmutableAttributeMappingList implements AttributeMappingsLis
return list[i]; //intentional unguarded array access: let it explode
}
@Override
public Iterator<AttributeMapping> iterator() {
return new AttributeMappingIterator();
}
@Override
public void forEach(Consumer<? super AttributeMapping> attributeMappingConsumer) {
for ( AttributeMapping o : list ) {
@ -57,16 +52,15 @@ public final class ImmutableAttributeMappingList implements AttributeMappingsLis
}
}
private final class AttributeMappingIterator implements Iterator<AttributeMapping> {
//Intentionally not implementing Iterator
public final class AttributeMappingIterator {
private int idx = 0;
@Override
public boolean hasNext() {
return idx < ImmutableAttributeMappingList.this.list.length;
}
@Override
public AttributeMapping next() {
return ImmutableAttributeMappingList.this.list[idx++];
}

View File

@ -7,7 +7,6 @@
package org.hibernate.metamodel.mapping.internal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
@ -19,7 +18,7 @@ import org.hibernate.sql.results.graph.Fetchable;
/**
* This mutable representation of AttributeMappingsList is meant to
* exist temporarily to assit migration to the new contract.
* exist temporarily to assist migration to the new contract.
* @deprecated Please get rid of it: such collections should be immutable.
*/
@Deprecated
@ -41,11 +40,6 @@ public final class MutableAttributeMappingList implements AttributeMappingsList
return asAttributeMapping( this.list.get( idx ) );
}
@Override
public Iterator<AttributeMapping> iterator() {
return new AttributeMappingIterator();
}
@Override
public void forEach(final Consumer<? super AttributeMapping> consumer) {
for ( int i = 0; i < list.size(); i++ ) {
@ -99,20 +93,4 @@ public final class MutableAttributeMappingList implements AttributeMappingsList
}
}
private final class AttributeMappingIterator implements Iterator<AttributeMapping> {
private Iterator iter = MutableAttributeMappingList.this.list.iterator();
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public AttributeMapping next() {
return asAttributeMapping( iter.next() );
}
}
}

View File

@ -2396,7 +2396,8 @@ public abstract class AbstractEntityPersister
Arrays.sort( attributeNames );
int index = 0;
for ( final AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
fields.add( attributeMapping.getStateArrayPosition() );
index++;
@ -2450,7 +2451,8 @@ public abstract class AbstractEntityPersister
// Sort attribute names so that we can traverse mappings efficiently
Arrays.sort( attributeNames );
int index = 0;
for ( final AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
final String attributeName = attributeMapping.getAttributeName();
if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
final int position = attributeMapping.getStateArrayPosition();
@ -3368,7 +3370,8 @@ public abstract class AbstractEntityPersister
protected UpdateCoordinator buildUpdateCoordinator() {
// we only have updates to issue for entities with one or more singular attributes
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeMapping instanceof SingularAttributeMapping ) {
return new UpdateCoordinatorStandard( this, factory );
}
@ -4852,7 +4855,8 @@ public abstract class AbstractEntityPersister
// in the collected names. iterate here because it is already alphabetical
final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>();
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
if ( attributeNames.contains( attributeMapping.getAttributeName() ) ) {
collectedAttrMappings.add( (SingularAttributeMapping) attributeMapping );
}
@ -5815,7 +5819,8 @@ public abstract class AbstractEntityPersister
@Override
public int forEachSelectable(int offset, SelectableConsumer selectableConsumer) {
int span = 0;
for ( AttributeMapping attributeMapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
span += attributeMapping.forEachSelectable( span + offset, selectableConsumer );
}
return span;

View File

@ -18,6 +18,7 @@ import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.EntityRowIdMapping;
import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.mapping.SelectableMapping;
@ -357,7 +358,9 @@ public class DeleteCoordinator extends AbstractMutationCoordinator {
applyOptimisticLocking( deleteGroupBuilder, loadedState, session );
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );

View File

@ -25,7 +25,6 @@ import org.hibernate.engine.jdbc.mutation.ParameterUsage;
import org.hibernate.engine.jdbc.mutation.internal.MutationQueryOptions;
import org.hibernate.engine.jdbc.mutation.internal.NoBatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -1110,7 +1109,9 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
private void applyPartictionKeyRestriction(TableUpdateBuilder<?> tableUpdateBuilder) {
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );
@ -1624,7 +1625,9 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
private void addPartitionRestriction(TableUpdateBuilderStandard<JdbcMutationOperation> updateBuilder) {
final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) {
for ( AttributeMapping attributeMapping : persister.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = persister.getAttributeMappings();
for ( int m = 0; m < attributeMappings.size(); m++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( m );
final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );

View File

@ -12,6 +12,7 @@ import java.util.function.Consumer;
import org.hibernate.metamodel.mapping.Association;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
@ -166,7 +167,9 @@ public class CteTable {
}
else {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart;
for ( AttributeMapping mapping : embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( !( mapping instanceof PluralAttributeMapping ) ) {
forEachCteColumn( prefix + "_" + mapping.getAttributeName(), mapping, consumer );
}
@ -188,7 +191,9 @@ public class CteTable {
}
offset += discriminatorMapping.getJdbcTypeCount();
}
for ( AttributeMapping attribute : entityDescriptor.getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = entityDescriptor.getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping attribute = attributeMappings.get( i );
if ( !( attribute instanceof PluralAttributeMapping ) ) {
final int result = determineModelPartStartIndex( offset, attribute, modelPart );
if ( result < 0 ) {
@ -216,7 +221,9 @@ public class CteTable {
}
else if ( modelPart instanceof EmbeddableValuedModelPart ) {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart;
for ( AttributeMapping mapping : embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings() ) {
final AttributeMappingsList attributeMappings = embeddablePart.getEmbeddableTypeDescriptor().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping mapping = attributeMappings.get( i );
final int result = determineModelPartStartIndex( offset, mapping, modelPartToFind );
if ( result < 0 ) {
return result;

View File

@ -186,7 +186,7 @@ public class XmlAccessTest {
.getMappingMetamodel()
.findEntityDescriptor( classUnderTest.getName() );
final AttributeMappingsList attributeMappings = entityDescriptor.getAttributeMappings();
final AttributeMapping attributeMapping = attributeMappings.iterator().next();
final AttributeMapping attributeMapping = attributeMappings.get( 0 );
final Getter accessGetter = attributeMapping.getPropertyAccess().getGetter();

View File

@ -238,7 +238,8 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware {
.findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ){
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}

View File

@ -206,7 +206,8 @@ public class EntityGraphLoadPlanBuilderTest implements SessionFactoryScopeAware
entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}

View File

@ -235,7 +235,8 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware {
.findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null;
for ( AttributeMapping mapping : attributeMappings ) {
for ( int i = 0; i < attributeMappings.size(); i++ ) {
AttributeMapping mapping = attributeMappings.get( i );
if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping;
}