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 2ea06b497f..f779f290bb 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 @@ -89,6 +89,7 @@ public abstract class AbstractLoadPlanBuildingAssociationVisitationStrategy private final SessionFactoryImplementor sessionFactory; private final QuerySpacesImpl querySpaces; + //TODO: I don't see propertyPathStack used anywhere. Can it be deleted? private final PropertyPathStack propertyPathStack = new PropertyPathStack(); private final ArrayDeque fetchSourceStack = new ArrayDeque(); @@ -935,6 +936,8 @@ public abstract class AbstractLoadPlanBuildingAssociationVisitationStrategy * Maintains stack information for the property paths we are processing for logging purposes. Because of the * recursive calls it is often useful (while debugging) to be able to see the "property path" as part of the * logging output. + * + * TODO: I don't see PropertyPathStack used anywhere. Can it be deleted? */ public static class PropertyPathStack { private ArrayDeque pathStack = new ArrayDeque(); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index ce5f1015a2..b064f4d773 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -2012,8 +2012,8 @@ public abstract class AbstractCollectionPersister @Override public EntityDefinition toEntityDefinition() { - if ( getType().isComponentType() ) { - throw new IllegalStateException( "Cannot treat composite collection index type as entity" ); + if ( !getType().isEntityType() ) { + throw new IllegalStateException( "Cannot treat collection index type as entity" ); } return (EntityPersister) ( (AssociationType) getIndexType() ).getAssociatedJoinable( getFactory() ); } @@ -2021,7 +2021,7 @@ public abstract class AbstractCollectionPersister @Override public CompositionDefinition toCompositeDefinition() { if ( ! getType().isComponentType() ) { - throw new IllegalStateException( "Cannot treat entity collection index type as composite" ); + throw new IllegalStateException( "Cannot treat collection index type as composite" ); } return new CompositeCollectionElementDefinition() { @Override @@ -2056,6 +2056,15 @@ public abstract class AbstractCollectionPersister } }; } + + @Override + public AnyMappingDefinition toAnyMappingDefinition() { + final Type type = getType(); + if ( ! type.isAnyType() ) { + throw new IllegalStateException( "Cannot treat collection index type as ManyToAny" ); + } + return new StandardAnyTypeDefinition( (AnyType) type, isLazy() || isExtraLazy() ); + } }; } @@ -2076,15 +2085,15 @@ public abstract class AbstractCollectionPersister public AnyMappingDefinition toAnyMappingDefinition() { final Type type = getType(); if ( ! type.isAnyType() ) { - throw new WalkingException( "Cannot treat collection element type as ManyToAny" ); + throw new IllegalStateException( "Cannot treat collection element type as ManyToAny" ); } return new StandardAnyTypeDefinition( (AnyType) type, isLazy() || isExtraLazy() ); } @Override public EntityDefinition toEntityDefinition() { - if ( getType().isComponentType() ) { - throw new WalkingException( "Cannot treat composite collection element type as entity" ); + if ( !getType().isEntityType() ) { + throw new IllegalStateException( "Cannot treat collection element type as entity" ); } return getElementPersister(); } @@ -2093,7 +2102,7 @@ public abstract class AbstractCollectionPersister public CompositeCollectionElementDefinition toCompositeElementDefinition() { if ( ! getType().isComponentType() ) { - throw new WalkingException( "Cannot treat entity collection element type as composite" ); + throw new IllegalStateException( "Cannot treat entity collection element type as composite" ); } return new CompositeCollectionElementDefinition() { diff --git a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionElementDefinition.java b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionElementDefinition.java index bb685d1816..759abd0930 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionElementDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionElementDefinition.java @@ -46,15 +46,15 @@ public interface CollectionElementDefinition { /** * If the element type returned by {@link #getType()} is an - * {@link org.hibernate.type.EntityType}, then the entity + * {@link org.hibernate.type.AnyType}, then the any mapping * definition for the collection element is returned; * otherwise, IllegalStateException is thrown. * - * @return the entity definition for the collection element. + * @return the any mapping definition for the collection element. * * @throws IllegalStateException if the collection element type * returned by {@link #getType()} is not of type - * {@link org.hibernate.type.EntityType}. + * {@link org.hibernate.type.AnyType}. */ public AnyMappingDefinition toAnyMappingDefinition(); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionIndexDefinition.java b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionIndexDefinition.java index a3c7d753cd..4908d5d24a 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionIndexDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/walking/spi/CollectionIndexDefinition.java @@ -65,4 +65,18 @@ public interface CollectionIndexDefinition { * {@link org.hibernate.type.CompositeType}. */ public CompositionDefinition toCompositeDefinition(); + + /** + * If the index type returned by {@link #getType()} is an + * {@link org.hibernate.type.AnyType}, then the any mapping + * definition for the collection index is returned; + * otherwise, IllegalStateException is thrown. + * + * @return the any mapping definition for the collection index. + * + * @throws IllegalStateException if the collection index type + * returned by {@link #getType()} is not of type + * {@link org.hibernate.type.AnyType}. + */ + public AnyMappingDefinition toAnyMappingDefinition(); } 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 840b0c39b4..efd17e9bfe 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 @@ -39,7 +39,7 @@ import org.hibernate.type.Type; * Implements metamodel graph walking. In layman terms, we are walking the graph of the users domain model as * defined/understood by mapped associations. *

- * Initially grew as a part of the re-implementation of the legacy JoinWalker functional to instead build LoadPlans. + * Initially grew as a part of the re-implementation of the legacy JoinWalker functionality to instead build LoadPlans. * But this is really quite simple walking. Interesting events are handled by calling out to * implementations of {@link AssociationVisitationStrategy} which really provide the real functionality of what we do * as we walk. @@ -240,7 +240,10 @@ public class MetamodelGraphWalker { try { final Type collectionIndexType = collectionIndexDefinition.getType(); - if ( collectionIndexType.isComponentType() ) { + if ( collectionIndexType.isAnyType() ) { + visitAnyDefinition( collectionIndexDefinition.toAnyMappingDefinition() ); + } + else if ( collectionIndexType.isComponentType() ) { visitCompositeDefinition( collectionIndexDefinition.toCompositeDefinition() ); } else if ( collectionIndexType.isAssociationType() ) { @@ -258,10 +261,14 @@ public class MetamodelGraphWalker { final CollectionElementDefinition elementDefinition = collectionDefinition.getElementDefinition(); strategy.startingCollectionElements( elementDefinition ); - if ( elementDefinition.getType().isComponentType() ) { + final Type collectionElementType = elementDefinition.getType(); + if ( collectionElementType.isAnyType() ) { + visitAnyDefinition( elementDefinition.toAnyMappingDefinition() ); + } + else if ( collectionElementType.isComponentType() ) { visitCompositeDefinition( elementDefinition.toCompositeElementDefinition() ); } - else if ( elementDefinition.getType().isEntityType() ) { + else if ( collectionElementType.isEntityType() ) { if ( ! collectionDefinition.getCollectionPersister().isOneToMany() ) { final QueryableCollection queryableCollection = (QueryableCollection) collectionDefinition.getCollectionPersister(); addAssociationKey( diff --git a/hibernate-core/src/test/java/org/hibernate/test/loadplans/walking/LoggingAssociationVisitationStrategy.java b/hibernate-core/src/test/java/org/hibernate/test/loadplans/walking/LoggingAssociationVisitationStrategy.java index 1412e8c5c1..8467fad1a3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/loadplans/walking/LoggingAssociationVisitationStrategy.java +++ b/hibernate-core/src/test/java/org/hibernate/test/loadplans/walking/LoggingAssociationVisitationStrategy.java @@ -204,31 +204,6 @@ public class LoggingAssociationVisitationStrategy implements AssociationVisitati ); } - - // why do we have these + startingCollectionElements/finishingCollectionElements ??? -// -// @Override -// public void startingCompositeCollectionElement(CompositeCollectionElementDefinition compositionElementDefinition) { -// System.out.println( -// String.format( -// "%s Starting composite (%s)", -// StringHelper.repeat( ">>", ++depth ), -// compositionElementDefinition.getCollectionDefinition().getCollectionPersister().getRole() -// ) -// ); -// } -// -// @Override -// public void finishingCompositeCollectionElement(CompositeCollectionElementDefinition compositionElementDefinition) { -// System.out.println( -// String.format( -// "%s Finishing composite (%s)", -// StringHelper.repeat( "<<", depth-- ), -// compositionElementDefinition.getCollectionDefinition().getCollectionPersister().getRole() -// ) -// ); -// } - @Override public void foundAny(AnyMappingDefinition anyDefinition) { // nothing to do