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 660b1801eb
commit 4bb95b7891
13 changed files with 49 additions and 52 deletions

View File

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

View File

@ -259,7 +259,9 @@ public interface EmbeddableMappingType extends ManagedMappingType, SelectableMap
} }
default int compare(Object value1, Object value2) { 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 Getter getter = attributeMapping.getPropertyAccess().getGetter();
final int comparison = attributeMapping.compare( getter.get( value1 ), getter.get( value2 ) ); final int comparison = attributeMapping.compare( getter.get( value1 ), getter.get( value2 ) );
if ( comparison != 0 ) { if ( comparison != 0 ) {

View File

@ -103,7 +103,9 @@ public interface ManagedMappingType extends MappingType, FetchableContainer {
@Override @Override
default boolean hasPartitionedSelectionMapping() { 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() ) { if ( attributeMapping.hasPartitionedSelectionMapping() ) {
return true; return true;
} }

View File

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

View File

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

@ -2391,7 +2391,8 @@ public abstract class AbstractEntityPersister
Arrays.sort( attributeNames ); Arrays.sort( attributeNames );
int index = 0; 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] ) ) { if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
fields.add( attributeMapping.getStateArrayPosition() ); fields.add( attributeMapping.getStateArrayPosition() );
index++; index++;
@ -2445,7 +2446,8 @@ public abstract class AbstractEntityPersister
// Sort attribute names so that we can traverse mappings efficiently // Sort attribute names so that we can traverse mappings efficiently
Arrays.sort( attributeNames ); Arrays.sort( attributeNames );
int index = 0; 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(); final String attributeName = attributeMapping.getAttributeName();
if ( isPrefix( attributeMapping, attributeNames[index] ) ) { if ( isPrefix( attributeMapping, attributeNames[index] ) ) {
final int position = attributeMapping.getStateArrayPosition(); final int position = attributeMapping.getStateArrayPosition();
@ -3363,7 +3365,8 @@ public abstract class AbstractEntityPersister
protected UpdateCoordinator buildUpdateCoordinator() { protected UpdateCoordinator buildUpdateCoordinator() {
// we only have updates to issue for entities with one or more singular attributes // 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 ) { if ( attributeMapping instanceof SingularAttributeMapping ) {
return new UpdateCoordinatorStandard( this, factory ); return new UpdateCoordinatorStandard( this, factory );
} }
@ -4835,7 +4838,8 @@ public abstract class AbstractEntityPersister
// in the collected names. iterate here because it is already alphabetical // in the collected names. iterate here because it is already alphabetical
final List<SingularAttributeMapping> collectedAttrMappings = new ArrayList<>(); 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() ) ) { if ( attributeNames.contains( attributeMapping.getAttributeName() ) ) {
collectedAttrMappings.add( (SingularAttributeMapping) attributeMapping ); collectedAttrMappings.add( (SingularAttributeMapping) attributeMapping );
} }
@ -5798,7 +5802,8 @@ public abstract class AbstractEntityPersister
@Override @Override
public int forEachSelectable(int offset, SelectableConsumer selectableConsumer) { public int forEachSelectable(int offset, SelectableConsumer selectableConsumer) {
int span = 0; 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 ); span += attributeMapping.forEachSelectable( span + offset, selectableConsumer );
} }
return span; 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.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.EntityRowIdMapping; import org.hibernate.metamodel.mapping.EntityRowIdMapping;
import org.hibernate.metamodel.mapping.EntityVersionMapping; import org.hibernate.metamodel.mapping.EntityVersionMapping;
import org.hibernate.metamodel.mapping.SelectableMapping; import org.hibernate.metamodel.mapping.SelectableMapping;
@ -357,7 +358,9 @@ public class DeleteCoordinator extends AbstractMutationCoordinator {
applyOptimisticLocking( deleteGroupBuilder, loadedState, session ); applyOptimisticLocking( deleteGroupBuilder, loadedState, session );
final AbstractEntityPersister persister = entityPersister(); final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) { 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(); final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) { for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( 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.MutationQueryOptions;
import org.hibernate.engine.jdbc.mutation.internal.NoBatchKeyAccess; import org.hibernate.engine.jdbc.mutation.internal.NoBatchKeyAccess;
import org.hibernate.engine.jdbc.mutation.spi.BatchKeyAccess; 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.EntityEntry;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -1110,7 +1109,9 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
private void applyPartictionKeyRestriction(TableUpdateBuilder<?> tableUpdateBuilder) { private void applyPartictionKeyRestriction(TableUpdateBuilder<?> tableUpdateBuilder) {
final AbstractEntityPersister persister = entityPersister(); final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) { 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(); final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) { for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( i ); final SelectableMapping selectableMapping = attributeMapping.getSelectable( i );
@ -1624,7 +1625,9 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
private void addPartitionRestriction(TableUpdateBuilderStandard<JdbcMutationOperation> updateBuilder) { private void addPartitionRestriction(TableUpdateBuilderStandard<JdbcMutationOperation> updateBuilder) {
final AbstractEntityPersister persister = entityPersister(); final AbstractEntityPersister persister = entityPersister();
if ( persister.hasPartitionedSelectionMapping() ) { 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(); final int jdbcTypeCount = attributeMapping.getJdbcTypeCount();
for ( int i = 0; i < jdbcTypeCount; i++ ) { for ( int i = 0; i < jdbcTypeCount; i++ ) {
final SelectableMapping selectableMapping = attributeMapping.getSelectable( 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.Association;
import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.AttributeMappingsList;
import org.hibernate.metamodel.mapping.BasicValuedMapping; import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart; import org.hibernate.metamodel.mapping.DiscriminatedAssociationModelPart;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
@ -166,7 +167,9 @@ public class CteTable {
} }
else { else {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart; 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 ) ) { if ( !( mapping instanceof PluralAttributeMapping ) ) {
forEachCteColumn( prefix + "_" + mapping.getAttributeName(), mapping, consumer ); forEachCteColumn( prefix + "_" + mapping.getAttributeName(), mapping, consumer );
} }
@ -188,7 +191,9 @@ public class CteTable {
} }
offset += discriminatorMapping.getJdbcTypeCount(); 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 ) ) { if ( !( attribute instanceof PluralAttributeMapping ) ) {
final int result = determineModelPartStartIndex( offset, attribute, modelPart ); final int result = determineModelPartStartIndex( offset, attribute, modelPart );
if ( result < 0 ) { if ( result < 0 ) {
@ -216,7 +221,9 @@ public class CteTable {
} }
else if ( modelPart instanceof EmbeddableValuedModelPart ) { else if ( modelPart instanceof EmbeddableValuedModelPart ) {
final EmbeddableValuedModelPart embeddablePart = ( EmbeddableValuedModelPart ) modelPart; 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 ); final int result = determineModelPartStartIndex( offset, mapping, modelPartToFind );
if ( result < 0 ) { if ( result < 0 ) {
return result; return result;

View File

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

View File

@ -236,9 +236,10 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware {
.getRuntimeMetamodels() .getRuntimeMetamodels()
.getMappingMetamodel() .getMappingMetamodel()
.findEntityDescriptor( entityClass.getName() ); .findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings(); final AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null; 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 ) ) { if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping; fetchable = mapping;
} }

View File

@ -204,9 +204,10 @@ public class EntityGraphLoadPlanBuilderTest implements SessionFactoryScopeAware
private Fetchable getFetchable(String attributeName, Class entityClass) { private Fetchable getFetchable(String attributeName, Class entityClass) {
EntityPersister person = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor( EntityPersister person = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(
entityClass.getName() ); entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings(); final AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null; 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 ) ) { if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping; fetchable = mapping;
} }

View File

@ -235,7 +235,8 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware {
.findEntityDescriptor( entityClass.getName() ); .findEntityDescriptor( entityClass.getName() );
AttributeMappingsList attributeMappings = person.getAttributeMappings(); AttributeMappingsList attributeMappings = person.getAttributeMappings();
Fetchable fetchable = null; 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 ) ) { if ( mapping.getAttributeName().equals( attributeName ) ) {
fetchable = mapping; fetchable = mapping;
} }