get rid of more iterators in the mapping package

This commit is contained in:
Gavin King 2022-01-26 20:29:10 +01:00
parent 54b9677d99
commit 12a515a95a
12 changed files with 140 additions and 75 deletions

View File

@ -478,7 +478,7 @@ public class BinderHelper {
catch (MappingException me) {
//swallow it
}
Iterator joins = current.getJoinIterator();
Iterator<Join> joins = current.getJoinIterator();
while ( !found && joins.hasNext() ) {
result = joins.next();
currentTable = ( (Join) result ).getTable();

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.cfg;
import java.util.Iterator;
import java.util.Map;
import jakarta.persistence.JoinColumn;
@ -27,7 +26,6 @@ import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.SortableValue;
import org.hibernate.type.ForeignKeyDirection;
@ -176,10 +174,8 @@ public class OneToOneSecondPass implements SecondPass {
propertyHolder.addProperty( prop, inferredData.getDeclaringClass() );
}
else if ( otherSideProperty.getValue() instanceof ManyToOne ) {
Iterator<Join> it = otherSide.getJoinIterator();
Join otherSideJoin = null;
while ( it.hasNext() ) {
Join otherSideJoinValue = it.next();
for ( Join otherSideJoinValue : otherSide.getJoins() ) {
if ( otherSideJoinValue.containsProperty( otherSideProperty ) ) {
otherSideJoin = otherSideJoinValue;
break;

View File

@ -2020,10 +2020,8 @@ public abstract class CollectionBinder {
}
else {
//find the appropriate reference key, can be in a join
Iterator<Join> joinsIt = referencedEntity.getJoinIterator();
KeyValue key = null;
while ( joinsIt.hasNext() ) {
Join join = joinsIt.next();
for ( Join join : referencedEntity.getJoins() ) {
if ( join.containsProperty( property ) ) {
key = join.getKey();
break;

View File

@ -31,6 +31,7 @@ import org.hibernate.MappingException;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
@ -844,12 +845,12 @@ public class EntityBinder {
* Those operations has to be done after the id definition of the persistence class.
* ie after the properties parsing
*/
Iterator joins = secondaryTables.values().iterator();
Iterator joinColumns = secondaryTableJoins.values().iterator();
Iterator<Join> joins = secondaryTables.values().iterator();
Iterator<Object> joinColumns = secondaryTableJoins.values().iterator();
while ( joins.hasNext() ) {
Object uncastedColumn = joinColumns.next();
Join join = (Join) joins.next();
Join join = joins.next();
createPrimaryColumnsToSecondaryTable( uncastedColumn, propertyHolder, join );
}
}
@ -1203,16 +1204,13 @@ public class EntityBinder {
//comment and index are processed here
if ( table == null ) return;
String appliedTable = table.appliesTo();
Iterator tables = persistentClass.getTableClosureIterator();
Table hibTable = null;
while ( tables.hasNext() ) {
Table pcTable = (Table) tables.next();
for ( Table pcTable : persistentClass.getTableClosure() ) {
if ( pcTable.getQuotedName().equals( appliedTable ) ) {
//we are in the correct table to find columns
hibTable = pcTable;
break;
}
hibTable = null;
}
if ( hibTable == null ) {
//maybe a join/secondary table

View File

@ -323,6 +323,9 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
@Deprecated(since = "6.0")
public abstract Iterator<Property> getPropertyClosureIterator();
public abstract List<Table> getTableClosure();
@Deprecated(since = "6.0")
public abstract Iterator<Table> getTableClosureIterator();
public abstract Iterator<KeyValue> getKeyClosureIterator();
@ -360,10 +363,20 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
return new JoinedList<>( lists );
}
@Deprecated(since = "6.0")
public Iterator<Join> getSubclassJoinClosureIterator() {
return new JoinedIterator<>( getJoinClosureIterator(), subclassJoins.iterator() );
}
public List<Join> getSubclassJoinClosure() {
return new JoinedList<>( getJoinClosure(), subclassJoins );
}
public List<Table> getSubclassTableClosure() {
return new JoinedList<>( getTableClosure(), subclassTables );
}
@Deprecated(since = "6.0")
public Iterator<Table> getSubclassTableClosureIterator() {
return new JoinedIterator<>( getTableClosureIterator(), subclassTables.iterator() );
}
@ -712,10 +725,20 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
return getClass().getName() + '(' + getEntityName() + ')';
}
public List<Join> getJoins() {
return joins;
}
public List<Join> getJoinClosure() {
return joins;
}
@Deprecated(since = "6.0")
public Iterator<Join> getJoinIterator() {
return joins.iterator();
}
@Deprecated(since = "6.0")
public Iterator<Join> getJoinClosureIterator() {
return joins.iterator();
}
@ -739,9 +762,7 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
public int getJoinNumber(Property prop) {
int result = 1;
Iterator<Join> iter = getSubclassJoinClosureIterator();
while ( iter.hasNext() ) {
Join join = iter.next();
for ( Join join : getSubclassJoinClosure() ) {
if ( join.containsProperty( prop ) ) {
return result;
}
@ -998,10 +1019,8 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
}
checkColumnDuplication( cols, getDiscriminator() );
checkPropertyColumnDuplication( cols, getNonDuplicatedProperties() );
Iterator<Join> iter = getJoinIterator();
while ( iter.hasNext() ) {
for ( Join join : getJoins() ) {
cols.clear();
Join join = iter.next();
checkColumnDuplication( cols, join.getKey() );
checkPropertyColumnDuplication( cols, join.getProperties() );
}

View File

@ -136,11 +136,16 @@ public class RootClass extends PersistentClass implements TableOwner {
return getProperties();
}
@Override
@Override @Deprecated
public Iterator<Table> getTableClosureIterator() {
return new SingletonIterator<>( getTable() );
}
@Override
public List<Table> getTableClosure() {
return List.of( getTable() );
}
@Override
public Iterator<KeyValue> getKeyClosureIterator() {
return new SingletonIterator<>( getKey() );
@ -344,13 +349,12 @@ public class RootClass extends PersistentClass implements TableOwner {
return synchronizedTables;
}
@SuppressWarnings("UnnecessaryUnboxing")
public Set<Table> getIdentityTables() {
Set<Table> tables = new HashSet<>();
Iterator iter = getSubclassClosureIterator();
Iterator<PersistentClass> iter = getSubclassClosureIterator();
while ( iter.hasNext() ) {
PersistentClass clazz = (PersistentClass) iter.next();
if ( clazz.isAbstract() == null || !clazz.isAbstract().booleanValue() ) {
PersistentClass clazz = iter.next();
if ( clazz.isAbstract() == null || !clazz.isAbstract() ) {
tables.add( clazz.getIdentityTable() );
}
}

View File

@ -38,10 +38,12 @@ public class Subclass extends PersistentClass {
this.subclassId = superclass.nextSubclassId();
}
@Override
int nextSubclassId() {
return getSuperclass().nextSubclassId();
}
@Override
public int getSubclassId() {
return subclassId;
}
@ -51,55 +53,74 @@ public class Subclass extends PersistentClass {
return getSuperclass().getNaturalIdCacheRegionName();
}
@Override
public String getCacheConcurrencyStrategy() {
return getRootClass().getCacheConcurrencyStrategy();
}
@Override
public RootClass getRootClass() {
return getSuperclass().getRootClass();
}
@Override
public PersistentClass getSuperclass() {
return superclass;
}
@Override
public Property getIdentifierProperty() {
return getSuperclass().getIdentifierProperty();
}
@Override
public Property getDeclaredIdentifierProperty() {
return null;
}
@Override
public KeyValue getIdentifier() {
return getSuperclass().getIdentifier();
}
@Override
public boolean hasIdentifierProperty() {
return getSuperclass().hasIdentifierProperty();
}
@Override
public Value getDiscriminator() {
return getSuperclass().getDiscriminator();
}
@Override
public boolean isMutable() {
return getSuperclass().isMutable();
}
@Override
public boolean isInherited() {
return true;
}
@Override
public boolean isPolymorphic() {
return true;
}
@Override
public void addProperty(Property p) {
super.addProperty(p);
getSuperclass().addSubclassProperty(p);
}
@Override
public void addMappedsuperclassProperty(Property p) {
super.addMappedsuperclassProperty( p );
getSuperclass().addSubclassProperty(p);
}
@Override
public void addJoin(Join j) {
super.addJoin(j);
getSuperclass().addSubclassJoin(j);
@ -110,7 +131,7 @@ public class Subclass extends PersistentClass {
return new JoinedList<>( getSuperclass().getPropertyClosure(), getProperties() );
}
@Deprecated
@Deprecated @Override
public Iterator<Property> getPropertyClosureIterator() {
return new JoinedIterator<>(
getSuperclass().getPropertyClosureIterator(),
@ -118,60 +139,86 @@ public class Subclass extends PersistentClass {
);
}
@Deprecated @Override
public Iterator<Table> getTableClosureIterator() {
return new JoinedIterator<>(
getSuperclass().getTableClosureIterator(),
new SingletonIterator<>( getTable() )
);
}
@Override
public List<Table> getTableClosure() {
return new JoinedList<>(
getSuperclass().getTableClosure(),
List.of( getTable() )
);
}
@Override
public Iterator<KeyValue> getKeyClosureIterator() {
return new JoinedIterator<>(
getSuperclass().getKeyClosureIterator(),
new SingletonIterator<>( getKey() )
);
}
@Override
protected void addSubclassProperty(Property p) {
super.addSubclassProperty(p);
getSuperclass().addSubclassProperty(p);
}
@Override
protected void addSubclassJoin(Join j) {
super.addSubclassJoin(j);
getSuperclass().addSubclassJoin(j);
}
@Override
protected void addSubclassTable(Table table) {
super.addSubclassTable(table);
getSuperclass().addSubclassTable(table);
}
@Override
public boolean isVersioned() {
return getSuperclass().isVersioned();
}
@Override
public Property getVersion() {
return getSuperclass().getVersion();
}
@Override
public Property getDeclaredVersion() {
return null;
}
@Override
public boolean hasEmbeddedIdentifier() {
return getSuperclass().hasEmbeddedIdentifier();
}
@Override
public Class<? extends EntityPersister> getEntityPersisterClass() {
return classPersisterClass == null
? getSuperclass().getEntityPersisterClass()
: classPersisterClass;
}
@Override
public Table getRootTable() {
return getSuperclass().getRootTable();
}
@Override
public KeyValue getKey() {
return getSuperclass().getIdentifier();
}
@Override
public boolean isExplicitPolymorphism() {
return getSuperclass().isExplicitPolymorphism();
}
@ -180,10 +227,12 @@ public class Subclass extends PersistentClass {
this.superclass = superclass;
}
@Override
public String getWhere() {
return getSuperclass().getWhere();
}
@Override
public boolean isJoinedSubclass() {
return getTable()!=getRootTable();
}
@ -195,18 +244,28 @@ public class Subclass extends PersistentClass {
getKey().createForeignKeyOfEntity( getSuperclass().getEntityName() );
}
@Override
public void setEntityPersisterClass(Class<? extends EntityPersister> classPersisterClass) {
this.classPersisterClass = classPersisterClass;
}
@Override
public int getJoinClosureSpan() {
return getSuperclass().getJoinClosureSpan() + super.getJoinClosureSpan();
}
@Override
public int getPropertyClosureSpan() {
return getSuperclass().getPropertyClosureSpan() + super.getPropertyClosureSpan();
}
@Override
public List<Join> getJoinClosure() {
return new JoinedList<>( getSuperclass().getJoinClosure(), super.getJoinClosure() );
}
@Deprecated(since = "6.0")
public Iterator<Join> getJoinClosureIterator() {
return new JoinedIterator<>(
getSuperclass().getJoinClosureIterator(),
@ -214,26 +273,33 @@ public class Subclass extends PersistentClass {
);
}
@Override
public boolean isClassOrSuperclassJoin(Join join) {
return super.isClassOrSuperclassJoin(join) || getSuperclass().isClassOrSuperclassJoin(join);
}
@Override
public boolean isClassOrSuperclassTable(Table table) {
return super.isClassOrSuperclassTable(table) || getSuperclass().isClassOrSuperclassTable(table);
}
@Override
public Table getTable() {
return getSuperclass().getTable();
}
@Override
public boolean isForceDiscriminator() {
return getSuperclass().isForceDiscriminator();
}
@Override
public boolean isDiscriminatorInsertable() {
return getSuperclass().isDiscriminatorInsertable();
}
@Override
public java.util.Set<String> getSynchronizedTables() {
HashSet<String> result = new HashSet<>();
result.addAll(synchronizedTables);
@ -241,21 +307,25 @@ public class Subclass extends PersistentClass {
return result;
}
@Override
public Object accept(PersistentClassVisitor mv) {
return mv.accept(this);
}
@Override
public java.util.List<FilterConfiguration> getFilters() {
java.util.List<FilterConfiguration> filters = new ArrayList<>(super.getFilters());
filters.addAll(getSuperclass().getFilters());
return filters;
}
@Override
public boolean hasSubselectLoadableCollections() {
return super.hasSubselectLoadableCollections() ||
getSuperclass().hasSubselectLoadableCollections();
}
@Override
public String getTuplizerImplClassName(RepresentationMode mode) {
String impl = super.getTuplizerImplClassName( mode );
if ( impl == null ) {
@ -264,6 +334,7 @@ public class Subclass extends PersistentClass {
return impl;
}
@Override
public Map getTuplizerMap() {
Map specificTuplizerDefs = super.getTuplizerMap();
Map superclassTuplizerDefs = getSuperclass().getTuplizerMap();
@ -282,6 +353,7 @@ public class Subclass extends PersistentClass {
}
}
@Override
public Component getIdentifierMapper() {
return superclass.getIdentifierMapper();
}

View File

@ -223,9 +223,7 @@ public class ToOneAttributeMapping
.getEntityBinding( manyToOne.getReferencedEntityName() );
if ( cardinality == Cardinality.LOGICAL_ONE_TO_ONE ) {
// Handle join table cases
final Iterator<Join> joinClosureIterator = entityBinding.getJoinClosureIterator();
while ( joinClosureIterator.hasNext() ) {
final Join join = joinClosureIterator.next();
for ( Join join : entityBinding.getJoinClosure() ) {
if ( join.getPersistentClass().getEntityName().equals( entityBinding.getEntityName() )
&& join.getPropertySpan() == 1
&& join.getTable() == manyToOne.getTable()

View File

@ -282,10 +282,8 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
isNullableTable = new boolean[tableSpan];
isInverseTable = new boolean[tableSpan];
Iterator<Join> joinItr = persistentClass.getJoinClosureIterator();
for ( int tableIndex = 0; joinItr.hasNext(); tableIndex++ ) {
Join join = joinItr.next();
int tableIndex = 0;
for ( Join join : persistentClass.getJoinClosure() ) {
isNullableTable[tableIndex] = join.isOptional();
isInverseTable[tableIndex] = join.isInverse();
@ -311,6 +309,8 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
keyColumnReaders.add( keyColReaders );
keyColumnReaderTemplates.add( keyColReaderTemplates );
cascadeDeletes.add( key.isCascadeDeleteEnabled() && factory.getJdbcServices().getDialect().supportsCascadeDelete() );
tableIndex++;
}
naturalOrderTableNames = ArrayHelper.toStringArray( tableNames );
@ -327,18 +327,16 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
ArrayList<Boolean> isNullables = new ArrayList<>();
keyColumns = new ArrayList<>();
tItr = persistentClass.getSubclassTableClosureIterator();
while ( tItr.hasNext() ) {
Table tab = tItr.next();
isConcretes.add( persistentClass.isClassOrSuperclassTable( tab ) );
for ( Table table : persistentClass.getSubclassTableClosure() ) {
isConcretes.add( persistentClass.isClassOrSuperclassTable( table ) );
isDeferreds.add( Boolean.FALSE );
isLazies.add( Boolean.FALSE );
isInverses.add( Boolean.FALSE );
isNullables.add( Boolean.FALSE );
final String tableName = determineTableName( tab );
final String tableName = determineTableName( table );
subclassTableNames.add( tableName );
String[] key = new String[idColumnSpan];
List<Column> columns = tab.getPrimaryKey().getColumns();
List<Column> columns = table.getPrimaryKey().getColumns();
for ( int k = 0; k < idColumnSpan; k++ ) {
key[k] = columns.get(k).getQuotedName( factory.getJdbcServices().getDialect() );
}
@ -346,9 +344,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
}
//Add joins
joinItr = persistentClass.getSubclassJoinClosureIterator();
while ( joinItr.hasNext() ) {
final Join join = joinItr.next();
for ( Join join : persistentClass.getSubclassJoinClosure() ) {
final Table joinTable = join.getTable();
isConcretes.add( persistentClass.isClassOrSuperclassTable( joinTable ) );
@ -446,11 +442,8 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
throw new AssertionFailure( "Tablespan does not match height of joined-subclass hiearchy." );
}
joinItr = persistentClass.getJoinClosureIterator();
int j = coreTableSpan;
while ( joinItr.hasNext() ) {
Join join = joinItr.next();
for ( Join join : persistentClass.getJoinClosure() ) {
isInverseTable[j] = join.isInverse();
isNullableTable[j] = join.isOptional();
@ -746,9 +739,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
associateSubclassNamesToSubclassTableIndex( tableName, classNames, mapping );
Iterator<Join> itr = persistentClass.getJoinIterator();
while ( itr.hasNext() ) {
final Join join = itr.next();
for ( Join join : persistentClass.getJoins() ) {
final String secondaryTableName = join.getTable().getQualifiedName(
factory.getSqlStringGenerationContext()
);

View File

@ -187,11 +187,9 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
// JOINS
Iterator<Join> joinIter = persistentClass.getJoinClosureIterator();
int j = 1;
final Dialect dialect = factory.getJdbcServices().getDialect();
while ( joinIter.hasNext() ) {
Join join = joinIter.next();
for ( Join join : persistentClass.getJoinClosure() ) {
qualifiedTableNames[j] = determineTableName( join.getTable() );
isInverseTable[j] = join.isInverse();
isNullableTable[j] = join.isOptional();
@ -253,9 +251,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister {
isInverses.add( Boolean.FALSE );
isNullables.add( Boolean.FALSE );
isLazies.add( Boolean.FALSE );
joinIter = persistentClass.getSubclassJoinClosureIterator();
while ( joinIter.hasNext() ) {
Join join = joinIter.next();
for ( Join join : persistentClass.getSubclassJoinClosure() ) {
isConcretes.add( persistentClass.isClassOrSuperclassTable( join.getTable() ) );
isClassOrSuperclassJoins.add( persistentClass.isClassOrSuperclassJoin( join ) );
isInverses.add( join.isInverse() );

View File

@ -182,9 +182,8 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
}
HashSet<String> subclassTables = new HashSet<>();
Iterator<Table> subclassTableIter = persistentClass.getSubclassTableClosureIterator();
while ( subclassTableIter.hasNext() ) {
subclassTables.add( determineTableName( subclassTableIter.next() ) );
for ( Table table : persistentClass.getSubclassTableClosure() ) {
subclassTables.add( determineTableName( table ) );
}
subclassSpaces = ArrayHelper.toStringArray( subclassTables );
@ -210,14 +209,12 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
int idColumnSpan = getIdentifierColumnSpan();
ArrayList<String> tableNames = new ArrayList<>();
ArrayList<String[]> keyColumns = new ArrayList<>();
Iterator<Table> tableIter = persistentClass.getSubclassTableClosureIterator();
while ( tableIter.hasNext() ) {
Table tab = tableIter.next();
if ( !tab.isAbstractUnionTable() ) {
final String tableName = determineTableName( tab );
for ( Table table : persistentClass.getSubclassTableClosure() ) {
if ( !table.isAbstractUnionTable() ) {
final String tableName = determineTableName( table );
tableNames.add( tableName );
String[] key = new String[idColumnSpan];
List<Column> columns = tab.getPrimaryKey().getColumns();
List<Column> columns = table.getPrimaryKey().getColumns();
for ( int k = 0; k < idColumnSpan; k++ ) {
key[k] = columns.get(k).getQuotedName( factory.getJdbcServices().getDialect() );
}
@ -468,14 +465,10 @@ public class UnionSubclassEntityPersister extends AbstractEntityPersister {
);
}
HashSet<Column> columns = new LinkedHashSet<>();
Iterator<Table> titer = model.getSubclassTableClosureIterator();
while ( titer.hasNext() ) {
Table table = titer.next();
Set<Column> columns = new LinkedHashSet<>();
for ( Table table : model.getSubclassTableClosure() ) {
if ( !table.isAbstractUnionTable() ) {
for ( Column column : table.getColumns() ) {
columns.add( column );
}
columns.addAll( table.getColumns() );
}
}

View File

@ -28,9 +28,9 @@ public class DynamicMapInstantiator implements Instantiator {
this.roleName = mappingInfo.getEntityName();
isInstanceEntityNames.add( roleName );
if ( mappingInfo.hasSubclasses() ) {
Iterator itr = mappingInfo.getSubclassClosureIterator();
Iterator<PersistentClass> itr = mappingInfo.getSubclassClosureIterator();
while ( itr.hasNext() ) {
final PersistentClass subclassInfo = ( PersistentClass ) itr.next();
final PersistentClass subclassInfo = itr.next();
isInstanceEntityNames.add( subclassInfo.getEntityName() );
}
}