diff --git a/hibernate-core/src/main/java/org/hibernate/loader/plan/build/internal/AbstractLoadPlanBuildingAssociationVisitationStrategy.java b/hibernate-core/src/main/java/org/hibernate/loader/plan/build/internal/AbstractLoadPlanBuildingAssociationVisitationStrategy.java index 848701f50e..367d3fff4b 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/plan/build/internal/AbstractLoadPlanBuildingAssociationVisitationStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/plan/build/internal/AbstractLoadPlanBuildingAssociationVisitationStrategy.java @@ -666,23 +666,30 @@ public void foundCircularAssociation(AssociationAttributeDefinition attributeDef // 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() ); - // 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??? final FetchSource registeredFetchSource = registeredFetchSource( associationKey ); - if ( registeredFetchSource != null && ! associationKey.equals( currentEntityReferenceAssociationKey ) ) { - currentSource().buildBidirectionalEntityReference( - attributeDefinition, - fetchStrategy, - registeredFetchSource( associationKey ).resolveEntityReference() - ); + if ( registeredFetchSource != null ) { + final ExpandingFetchSource currentSource = currentSource(); + final Joinable currentEntityPersister = (Joinable) currentSource.resolveEntityReference().getEntityPersister(); + + final AssociationKey currentEntityReferenceAssociationKey = + new AssociationKey( currentEntityPersister.getTableName(), currentEntityPersister.getKeyColumnNames() ); + // 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.buildBidirectionalEntityReference( + attributeDefinition, + fetchStrategy, + registeredFetchSource.resolveEntityReference() + ); + } + } + else { + // Do nothing, no bi-directionality } } else {