Search in sources :

Example 6 with JoinDefinedByMetadata

use of org.hibernate.loader.plan.spi.JoinDefinedByMetadata in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method renderCollectionJoin.

private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
    final CollectionQuerySpace rightHandSide = (CollectionQuerySpace) join.getRightHandSide();
    // The SQL join to the "collection table" needs to be rendered.
    //
    // In the case of a basic collection, that's the only join needed.
    //
    // For one-to-many/many-to-many, we need to render the "collection table join"
    // here (as already stated). There will be a follow-on join (rhs will have a join) for the associated entity.
    // For many-to-many, the follow-on join will join to the associated entity element table. For one-to-many,
    // the collection table is the associated entity table, so the follow-on join will not be rendered..
    // currently we do not explicitly track the joins under the CollectionQuerySpace to know which is
    // the element join and which is the index join (maybe we should?).
    JoinDefinedByMetadata collectionElementJoin = null;
    JoinDefinedByMetadata collectionIndexJoin = null;
    for (Join collectionJoin : rightHandSide.getJoins()) {
        if (JoinDefinedByMetadata.class.isInstance(collectionJoin)) {
            final JoinDefinedByMetadata collectionJoinDefinedByMetadata = (JoinDefinedByMetadata) collectionJoin;
            if (CollectionPropertyNames.COLLECTION_ELEMENTS.equals(collectionJoinDefinedByMetadata.getJoinedPropertyName())) {
                if (collectionElementJoin != null) {
                    throw new AssertionFailure(String.format("More than one element join defined for: %s", rightHandSide.getCollectionPersister().getRole()));
                }
                collectionElementJoin = collectionJoinDefinedByMetadata;
            }
            if (CollectionPropertyNames.COLLECTION_INDICES.equals(collectionJoinDefinedByMetadata.getJoinedPropertyName())) {
                if (collectionIndexJoin != null) {
                    throw new AssertionFailure(String.format("More than one index join defined for: %s", rightHandSide.getCollectionPersister().getRole()));
                }
                collectionIndexJoin = collectionJoinDefinedByMetadata;
            }
        }
    }
    if (rightHandSide.getCollectionPersister().isOneToMany() || rightHandSide.getCollectionPersister().isManyToMany()) {
        // sql aliases to use for the entity.
        if (collectionElementJoin == null) {
            throw new IllegalStateException(String.format("Could not locate collection element join within collection join [%s : %s]", rightHandSide.getUid(), rightHandSide.getCollectionPersister()));
        }
        aliasResolutionContext.generateCollectionReferenceAliases(rightHandSide.getUid(), rightHandSide.getCollectionPersister(), collectionElementJoin.getRightHandSide().getUid());
    } else {
        aliasResolutionContext.generateCollectionReferenceAliases(rightHandSide.getUid(), rightHandSide.getCollectionPersister(), null);
    }
    if (rightHandSide.getCollectionPersister().hasIndex() && rightHandSide.getCollectionPersister().getIndexType().isEntityType()) {
        // sql aliases to use for the entity.
        if (collectionIndexJoin == null) {
            throw new IllegalStateException(String.format("Could not locate collection index join within collection join [%s : %s]", rightHandSide.getUid(), rightHandSide.getCollectionPersister()));
        }
        aliasResolutionContext.generateEntityReferenceAliases(collectionIndexJoin.getRightHandSide().getUid(), rightHandSide.getCollectionPersister().getIndexDefinition().toEntityDefinition().getEntityPersister());
    }
    addJoins(join, joinFragment, (Joinable) rightHandSide.getCollectionPersister(), null);
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) Join(org.hibernate.loader.plan.spi.Join) CollectionQuerySpace(org.hibernate.loader.plan.spi.CollectionQuerySpace)

Aggregations

JoinDefinedByMetadata (org.hibernate.loader.plan.spi.JoinDefinedByMetadata)6 ExpandingCompositeQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingCompositeQuerySpace)2 CollectionQuerySpace (org.hibernate.loader.plan.spi.CollectionQuerySpace)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 AssertionFailure (org.hibernate.AssertionFailure)1 ExpandingCollectionQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingCollectionQuerySpace)1 ExpandingEntityQuerySpace (org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace)1 EntityQuerySpace (org.hibernate.loader.plan.spi.EntityQuerySpace)1 Join (org.hibernate.loader.plan.spi.Join)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 PropertyMapping (org.hibernate.persister.entity.PropertyMapping)1 WalkingException (org.hibernate.persister.walking.spi.WalkingException)1 CollectionType (org.hibernate.type.CollectionType)1 CompositeType (org.hibernate.type.CompositeType)1