From ebedc75e0fb1a3c1d11bed59855aba63d0b54757 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 27 Nov 2013 01:23:46 -0800 Subject: [PATCH] HHH-8750 : Move load plan implementations from spi to internal --- .../plan/build/spi/QuerySpaceTreePrinter.java | 2 +- .../internal/AliasResolutionContextImpl.java | 40 +++++++++++++------ .../CollectionReferenceAliasesImpl.java | 9 +++-- .../LoadQueryJoinAndFetchProcessor.java | 27 +++++++------ .../spi/BasicCollectionLoadQueryDetails.java | 2 +- .../exec/spi/CollectionLoadQueryDetails.java | 22 ++++------ .../exec/spi/CollectionReferenceAliases.java | 6 +-- 7 files changed, 60 insertions(+), 48 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/build/spi/QuerySpaceTreePrinter.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/build/spi/QuerySpaceTreePrinter.java index e68bd5e6db..26bd570883 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/build/spi/QuerySpaceTreePrinter.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/build/spi/QuerySpaceTreePrinter.java @@ -150,7 +150,7 @@ public class QuerySpaceTreePrinter { + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() ) + "}" ); - final EntityAliases elementAliases = collectionReferenceAliases.getEntityElementColumnAliases(); + final EntityAliases elementAliases = collectionReferenceAliases.getEntityElementAliases().getColumnAliases(); if ( elementAliases != null ) { printWriter.println( TreePrinterHelper.INSTANCE.generateNodePrefix( depth + detailDepthOffset ) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/AliasResolutionContextImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/AliasResolutionContextImpl.java index 715d22f447..ef94f4aa86 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/AliasResolutionContextImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/AliasResolutionContextImpl.java @@ -175,14 +175,27 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { * * * - * @param uid The query space UID for the collection reference. + * @param collectionQuerySpaceUid The query space UID for the collection reference. * @param persister The collection persister for collection reference. + * @param elementQuerySpaceUid The query space UID for the collection element if + * the element is an entity type; null, otherwise. * @return the generated collection reference aliases. + * @throws IllegalArgumentException if the collection element is an entity type and + * {@code elementQuerySpaceUid} is null. * * @see org.hibernate.loader.plan.spi.CollectionReference#getQuerySpaceUid() * @see org.hibernate.loader.plan.spi.CollectionReference#getCollectionPersister() */ - public CollectionReferenceAliases generateCollectionReferenceAliases(String uid, CollectionPersister persister) { + public CollectionReferenceAliases generateCollectionReferenceAliases( + String collectionQuerySpaceUid, + CollectionPersister persister, + String elementQuerySpaceUid) { + if ( persister.getElementType().isEntityType() && elementQuerySpaceUid == null ) { + throw new IllegalArgumentException( + "elementQuerySpaceUid must be non-null for one-to-many or many-to-many associations." + ); + } + final String manyToManyTableAlias; final String tableAlias; if ( persister.isManyToMany() ) { @@ -194,15 +207,18 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { tableAlias = createTableAlias( persister.getRole() ); } - final CollectionReferenceAliasesImpl aliases = new CollectionReferenceAliasesImpl( + final CollectionReferenceAliases collectionAliases = new CollectionReferenceAliasesImpl( tableAlias, manyToManyTableAlias, createCollectionAliases( persister ), createCollectionElementAliases( persister ) ); - registerQuerySpaceAliases( uid, aliases ); - return aliases; + registerQuerySpaceAliases( collectionQuerySpaceUid, collectionAliases ); + if ( collectionAliases.getEntityElementAliases() != null ) { + registerQuerySpaceAliases( elementQuerySpaceUid, collectionAliases.getEntityElementAliases() ); + } + return collectionAliases; } private CollectionAliases createCollectionAliases(CollectionPersister collectionPersister) { @@ -219,7 +235,7 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { } } - public void registerQuerySpaceAliases(String querySpaceUid, EntityReferenceAliases entityReferenceAliases) { + private void registerQuerySpaceAliases(String querySpaceUid, EntityReferenceAliases entityReferenceAliases) { if ( entityReferenceAliasesMap == null ) { entityReferenceAliasesMap = new HashMap(); } @@ -227,7 +243,7 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { registerSqlTableAliasMapping( querySpaceUid, entityReferenceAliases.getTableAlias() ); } - public void registerSqlTableAliasMapping(String querySpaceUid, String sqlTableAlias) { + private void registerSqlTableAliasMapping(String querySpaceUid, String sqlTableAlias) { if ( querySpaceUidToSqlTableAliasMap == null ) { querySpaceUidToSqlTableAliasMap = new HashMap(); } @@ -270,7 +286,7 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { return entityReferenceAliasesMap == null ? null : entityReferenceAliasesMap.get( querySpaceUid ); } - public void registerQuerySpaceAliases(String querySpaceUid, CollectionReferenceAliases collectionReferenceAliases) { + private void registerQuerySpaceAliases(String querySpaceUid, CollectionReferenceAliases collectionReferenceAliases) { if ( collectionReferenceAliasesMap == null ) { collectionReferenceAliasesMap = new HashMap(); } @@ -354,17 +370,17 @@ public class AliasResolutionContextImpl implements AliasResolutionContext { + "suffixed key columns - " + StringHelper.join( ", ", collectionReferenceAliases.getCollectionColumnAliases().getSuffixedKeyAliases() ) ); - final EntityAliases elementAliases = collectionReferenceAliases.getEntityElementColumnAliases(); + final EntityReferenceAliases elementAliases = collectionReferenceAliases.getEntityElementAliases(); if ( elementAliases != null ) { printWriter.println( TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 ) - + "entity-element alias suffix - " + elementAliases.getSuffix() + + "entity-element alias suffix - " + elementAliases.getColumnAliases().getSuffix() ); printWriter.println( TreePrinterHelper.INSTANCE.generateNodePrefix( depth+3 ) - + elementAliases.getSuffix() + + elementAliases.getColumnAliases().getSuffix() + "entity-element suffixed key columns - " - + StringHelper.join( ", ", elementAliases.getSuffixedKeyAliases() ) + + StringHelper.join( ", ", elementAliases.getColumnAliases().getSuffixedKeyAliases() ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/CollectionReferenceAliasesImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/CollectionReferenceAliasesImpl.java index dc88b50a43..1d97fa2766 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/CollectionReferenceAliasesImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/CollectionReferenceAliasesImpl.java @@ -27,6 +27,7 @@ import org.hibernate.internal.util.StringHelper; import org.hibernate.loader.CollectionAliases; import org.hibernate.loader.EntityAliases; import org.hibernate.loader.plan.exec.spi.CollectionReferenceAliases; +import org.hibernate.loader.plan.exec.spi.EntityReferenceAliases; /** * @author Steve Ebersole @@ -35,7 +36,7 @@ public class CollectionReferenceAliasesImpl implements CollectionReferenceAliase private final String tableAlias; private final String manyToManyAssociationTableAlias; private final CollectionAliases collectionAliases; - private final EntityAliases entityElementAliases; + private final EntityReferenceAliases entityElementAliases; public CollectionReferenceAliasesImpl( String tableAlias, @@ -45,7 +46,9 @@ public class CollectionReferenceAliasesImpl implements CollectionReferenceAliase this.tableAlias = tableAlias; this.manyToManyAssociationTableAlias = manyToManyAssociationTableAlias; this.collectionAliases = collectionAliases; - this.entityElementAliases = entityElementAliases; + this.entityElementAliases = entityElementAliases == null ? + null : + new EntityReferenceAliasesImpl( tableAlias, entityElementAliases ); } @Override @@ -66,7 +69,7 @@ public class CollectionReferenceAliasesImpl implements CollectionReferenceAliase } @Override - public EntityAliases getEntityElementColumnAliases() { + public EntityReferenceAliases getEntityElementAliases() { return entityElementAliases; } } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/LoadQueryJoinAndFetchProcessor.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/LoadQueryJoinAndFetchProcessor.java index c3de3fbee9..3b2d05c3b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/LoadQueryJoinAndFetchProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/internal/LoadQueryJoinAndFetchProcessor.java @@ -274,10 +274,6 @@ public class LoadQueryJoinAndFetchProcessor { private void renderCollectionJoin(Join join, JoinFragment joinFragment) { final CollectionQuerySpace rightHandSide = (CollectionQuerySpace) join.getRightHandSide(); - final CollectionReferenceAliases aliases = aliasResolutionContext.generateCollectionReferenceAliases( - rightHandSide.getUid(), - rightHandSide.getCollectionPersister() - ); // The SQL join to the "collection table" needs to be rendered. // @@ -319,12 +315,17 @@ public class LoadQueryJoinAndFetchProcessor { ) ); } - aliasResolutionContext.registerQuerySpaceAliases( - collectionElementJoin.getRightHandSide().getUid(), - new EntityReferenceAliasesImpl( - aliases.getElementTableAlias(), - aliases.getEntityElementColumnAliases() - ) + aliasResolutionContext.generateCollectionReferenceAliases( + rightHandSide.getUid(), + rightHandSide.getCollectionPersister(), + collectionElementJoin.getRightHandSide().getUid() + ); + } + else { + aliasResolutionContext.generateCollectionReferenceAliases( + rightHandSide.getUid(), + rightHandSide.getCollectionPersister(), + null ); } @@ -554,7 +555,7 @@ public class LoadQueryJoinAndFetchProcessor { elementTableAlias, collectionTableAlias, - aliases.getEntityElementColumnAliases().getSuffix(), + aliases.getEntityElementAliases().getColumnAliases().getSuffix(), aliases.getCollectionColumnAliases().getSuffix(), true ) @@ -565,7 +566,7 @@ public class LoadQueryJoinAndFetchProcessor { selectStatementBuilder.appendSelectClauseFragment( elementPersister.selectFragment( elementTableAlias, - aliases.getEntityElementColumnAliases().getSuffix() + aliases.getEntityElementAliases().getColumnAliases().getSuffix() ) ); @@ -602,7 +603,7 @@ public class LoadQueryJoinAndFetchProcessor { selectStatementBuilder.appendSelectClauseFragment( elementPersister.selectFragment( aliases.getElementTableAlias(), - aliases.getEntityElementColumnAliases().getSuffix() + aliases.getEntityElementAliases().getColumnAliases().getSuffix() ) ); readerCollector.add( diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/BasicCollectionLoadQueryDetails.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/BasicCollectionLoadQueryDetails.java index 3f05174b73..23afd22d00 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/BasicCollectionLoadQueryDetails.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/BasicCollectionLoadQueryDetails.java @@ -97,7 +97,7 @@ public class BasicCollectionLoadQueryDetails extends CollectionLoadQueryDetails selectStatementBuilder.appendSelectClauseFragment( elementPersister.selectFragment( getCollectionReferenceAliases().getElementTableAlias(), - getCollectionReferenceAliases().getEntityElementColumnAliases().getSuffix() + getCollectionReferenceAliases().getEntityElementAliases().getColumnAliases().getSuffix() ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionLoadQueryDetails.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionLoadQueryDetails.java index 6c2ee3616f..cd6ca85db2 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionLoadQueryDetails.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionLoadQueryDetails.java @@ -29,7 +29,6 @@ import java.sql.SQLException; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.util.StringHelper; import org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl; -import org.hibernate.loader.plan.exec.internal.EntityReferenceAliasesImpl; import org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl; import org.hibernate.loader.plan.exec.process.internal.CollectionReturnReader; import org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl; @@ -71,15 +70,16 @@ public abstract class CollectionLoadQueryDetails extends AbstractLoadQueryDetail buildingParameters, ( (QueryableCollection) rootReturn.getCollectionPersister() ).getKeyColumnNames(), rootReturn, -// collectionReferenceAliases.getCollectionTableAlias(), -// collectionReferenceAliases.getCollectionColumnAliases().getSuffix(), -// loadPlan.getQuerySpaces().getQuerySpaceByUid( rootReturn.getQuerySpaceUid() ), -// (OuterJoinLoadable) rootReturn.getCollectionPersister(), factory ); + final String elementUid = rootReturn.getCollectionPersister().getElementType().isEntityType() ? + rootReturn.getElementGraph().getQuerySpaceUid() : + null; + this.collectionReferenceAliases = aliasResolutionContext.generateCollectionReferenceAliases( rootReturn.getQuerySpaceUid(), - rootReturn.getCollectionPersister() + rootReturn.getCollectionPersister(), + elementUid ); this.readerCollector = new CollectionLoaderReaderCollectorImpl( new CollectionReturnReader( rootReturn ), @@ -87,16 +87,8 @@ public abstract class CollectionLoadQueryDetails extends AbstractLoadQueryDetail ); if ( rootReturn.getCollectionPersister().getElementType().isEntityType() ) { final EntityReference elementEntityReference = rootReturn.getElementGraph().resolveEntityReference(); - final EntityReferenceAliases elementEntityReferenceAliases = new EntityReferenceAliasesImpl( - collectionReferenceAliases.getElementTableAlias(), - collectionReferenceAliases.getEntityElementColumnAliases() - ); - aliasResolutionContext.registerQuerySpaceAliases( - elementEntityReference.getQuerySpaceUid(), - elementEntityReferenceAliases - ); readerCollector.add( - new EntityReferenceInitializerImpl( elementEntityReference, elementEntityReferenceAliases ) + new EntityReferenceInitializerImpl( elementEntityReference, collectionReferenceAliases.getEntityElementAliases() ) ); } if ( rootReturn.getCollectionPersister().hasIndex() && diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionReferenceAliases.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionReferenceAliases.java index 611c7e45ad..01a16a48c9 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionReferenceAliases.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/exec/spi/CollectionReferenceAliases.java @@ -61,9 +61,9 @@ public interface CollectionReferenceAliases { public CollectionAliases getCollectionColumnAliases(); /** - * Obtain the column aliases for the element values when the element of the collection is an entity. + * Obtain the entity reference aliases for the element values when the element of the collection is an entity. * - * @return The column aliases for the entity element; {@code null} if the collection element is not an entity. + * @return The entity reference aliases for the entity element; {@code null} if the collection element is not an entity. */ - public EntityAliases getEntityElementColumnAliases(); + public EntityReferenceAliases getEntityElementAliases(); }