From 9e1acbac41a1bf8e5a99bc3e1ed9c96d5c2cdec5 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Mon, 16 Sep 2013 17:27:54 -0700 Subject: [PATCH] HHH-8276 - Integrate LoadPlans into UniqueEntityLoader (PoC) --- .../returns/AbstractCollectionReference.java | 1 + ...BuildingAssociationVisitationStrategy.java | 20 +++++++++++-------- .../walking/spi/MetamodelGraphWalker.java | 10 ++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCollectionReference.java b/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCollectionReference.java index 20ed1ef9f4..4f093a509e 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCollectionReference.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCollectionReference.java @@ -29,6 +29,7 @@ import org.hibernate.loader.plan2.build.spi.LoadPlanBuildingContext; import org.hibernate.loader.plan2.spi.CollectionFetchableElement; import org.hibernate.loader.plan2.spi.CollectionFetchableIndex; import org.hibernate.loader.plan2.spi.CollectionReference; +import org.hibernate.loader.plan2.spi.FetchSource; import org.hibernate.loader.plan2.spi.Join; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/spi/AbstractLoadPlanBuildingAssociationVisitationStrategy.java b/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/spi/AbstractLoadPlanBuildingAssociationVisitationStrategy.java index 52ec6d8116..e2c5691015 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/spi/AbstractLoadPlanBuildingAssociationVisitationStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan2/build/spi/AbstractLoadPlanBuildingAssociationVisitationStrategy.java @@ -584,28 +584,32 @@ public abstract class AbstractLoadPlanBuildingAssociationVisitationStrategy return; // nothing to do } + final AssociationKey associationKey = attributeDefinition.getAssociationKey(); + // go ahead and build the bidirectional fetch if ( attributeDefinition.getAssociationNature() == AssociationAttributeDefinition.AssociationNature.ENTITY ) { final Joinable currentEntityPersister = (Joinable) currentSource().resolveEntityReference().getEntityPersister(); final AssociationKey currentEntityReferenceAssociationKey = new AssociationKey( currentEntityPersister.getTableName(), currentEntityPersister.getKeyColumnNames() ); - final Joinable fetchSourceEntityPersister = - (Joinable) registeredFetchSource( attributeDefinition.getAssociationKey() ).resolveEntityReference().getEntityPersister(); - final AssociationKey fetchSourceEntityReferenceAssociationKey = - new AssociationKey( fetchSourceEntityPersister.getTableName(), fetchSourceEntityPersister.getKeyColumnNames() ); - if ( ! attributeDefinition.getAssociationKey().equals( currentEntityReferenceAssociationKey ) && - ! attributeDefinition.getAssociationKey().equals( fetchSourceEntityReferenceAssociationKey ) ) { + // if associationKey is equal to currentEntityReferenceAssociationKey + // that means that the current EntityPersister has a single primary key attribute + // (i.e., derived attribute) which is mapped by attributeDefinition. + // This is not a bidirectional association. + // TODO: AFAICT, to avoid an overflow, the associated entity must already be loaded into the session, or + // it must be loaded when the ID for the dependent entity is resolved. Is there some other way to + // deal with this??? + if ( ! associationKey.equals( currentEntityReferenceAssociationKey ) ) { currentSource().buildBidirectionalEntityFetch( attributeDefinition, fetchStrategy, - fetchedAssociationKeySourceMap.get( attributeDefinition.getAssociationKey() ).resolveEntityReference(), + registeredFetchSource( associationKey ).resolveEntityReference(), this ); } } else { // Collection - currentSource().buildCollectionFetch( attributeDefinition, fetchStrategy, this ); + //currentSource().buildCollectionFetch( attributeDefinition, fetchStrategy, this ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/MetamodelGraphWalker.java b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/MetamodelGraphWalker.java index 0c87a392a3..bb14154478 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/MetamodelGraphWalker.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/MetamodelGraphWalker.java @@ -31,6 +31,7 @@ import org.jboss.logging.Logger; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.loader.PropertyPath; import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.collection.QueryableCollection; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; @@ -261,6 +262,15 @@ public class MetamodelGraphWalker { visitAttributes( elementDefinition.toCompositeElementDefinition() ); } else if ( elementDefinition.getType().isEntityType() ) { + if ( ! collectionDefinition.getCollectionPersister().isOneToMany() ) { + final QueryableCollection queryableCollection = (QueryableCollection) collectionDefinition.getCollectionPersister(); + addAssociationKey( + new AssociationKey( + queryableCollection.getTableName(), + queryableCollection.getElementColumnNames() + ) + ); + } visitEntityDefinition( elementDefinition.toEntityDefinition() ); }