Search in sources :

Example 1 with CollectionQuerySpace

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

the class LoadQueryJoinAndFetchProcessor method renderManyToManyJoin.

private void renderManyToManyJoin(Join join, JoinFragment joinFragment) {
    // for many-to-many we have 3 table aliases.  By way of example, consider a normal m-n: User<->Role
    // where User is the FetchOwner and Role (User.roles) is the Fetch.  We'd have:
    //		1) the owner's table : user - in terms of rendering the joins (not the fetch select fragments), the
    // 			lhs table alias is only needed to qualify the lhs join columns, but we already have the qualified
    // 			columns here (aliasedLhsColumnNames)
    //final String ownerTableAlias = ...;
    //		2) the m-n table : user_role
    //		3) the element table : role
    final EntityPersister entityPersister = ((EntityQuerySpace) join.getRightHandSide()).getEntityPersister();
    final String entityTableAlias = aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(join.getRightHandSide().getUid());
    if (StringHelper.isEmpty(entityTableAlias)) {
        throw new IllegalStateException("Collection element (many-to-many) table alias cannot be empty");
    }
    final String manyToManyFilter;
    if (JoinDefinedByMetadata.class.isInstance(join) && CollectionPropertyNames.COLLECTION_ELEMENTS.equals(((JoinDefinedByMetadata) join).getJoinedPropertyName())) {
        final CollectionQuerySpace leftHandSide = (CollectionQuerySpace) join.getLeftHandSide();
        final CollectionPersister persister = leftHandSide.getCollectionPersister();
        manyToManyFilter = persister.getManyToManyFilterFragment(entityTableAlias, buildingParameters.getQueryInfluencers().getEnabledFilters());
    } else {
        manyToManyFilter = null;
    }
    addJoins(join, joinFragment, (Joinable) entityPersister, manyToManyFilter);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) EntityQuerySpace(org.hibernate.loader.plan.spi.EntityQuerySpace) CollectionQuerySpace(org.hibernate.loader.plan.spi.CollectionQuerySpace)

Example 2 with CollectionQuerySpace

use of org.hibernate.loader.plan.spi.CollectionQuerySpace 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

CollectionQuerySpace (org.hibernate.loader.plan.spi.CollectionQuerySpace)2 JoinDefinedByMetadata (org.hibernate.loader.plan.spi.JoinDefinedByMetadata)2 AssertionFailure (org.hibernate.AssertionFailure)1 EntityQuerySpace (org.hibernate.loader.plan.spi.EntityQuerySpace)1 Join (org.hibernate.loader.plan.spi.Join)1 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1