Search in sources :

Example 1 with AbstractSqmSpecificPluralPartPath

use of org.hibernate.query.sqm.tree.domain.AbstractSqmSpecificPluralPartPath in project hibernate-orm by hibernate.

the class SqmMappingModelHelper method resolveSqmPath.

private static ModelPart resolveSqmPath(SqmPath<?> sqmPath, MappingMetamodel domainModel, Function<NavigablePath, TableGroup> tableGroupLocator) {
    if (sqmPath instanceof SqmTreatedPath) {
        final SqmTreatedPath treatedPath = (SqmTreatedPath) sqmPath;
        final EntityDomainType treatTargetType = treatedPath.getTreatTarget();
        return domainModel.findEntityDescriptor(treatTargetType.getHibernateEntityName());
    }
    // see if the LHS is treated
    if (sqmPath.getLhs() instanceof SqmTreatedPath) {
        final SqmTreatedPath treatedPath = (SqmTreatedPath) sqmPath.getLhs();
        final EntityDomainType treatTargetType = treatedPath.getTreatTarget();
        final EntityPersister container = domainModel.findEntityDescriptor(treatTargetType.getHibernateEntityName());
        return container.findSubPart(sqmPath.getNavigablePath().getLocalName(), container);
    }
    // Plural path parts are not joined and thus also have no table group
    if (sqmPath instanceof AbstractSqmSpecificPluralPartPath<?>) {
        final TableGroup lhsTableGroup = tableGroupLocator.apply(sqmPath.getLhs().getLhs().getNavigablePath());
        final ModelPartContainer pluralPart = (ModelPartContainer) lhsTableGroup.getModelPart().findSubPart(sqmPath.getLhs().getReferencedPathSource().getPathName(), null);
        final CollectionPart collectionPart = (CollectionPart) pluralPart.findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
        // as that is the mapping type of the expression
        if (collectionPart instanceof EntityCollectionPart) {
            return ((EntityCollectionPart) collectionPart).getEntityMappingType();
        }
        return collectionPart;
    }
    if (sqmPath.getLhs() == null) {
        final EntityDomainType<?> entityDomainType = (EntityDomainType<?>) sqmPath.getReferencedPathSource();
        return domainModel.findEntityDescriptor(entityDomainType.getHibernateEntityName());
    }
    final TableGroup lhsTableGroup = tableGroupLocator.apply(sqmPath.getLhs().getNavigablePath());
    final ModelPartContainer modelPart;
    if (lhsTableGroup == null) {
        modelPart = (ModelPartContainer) resolveSqmPath(sqmPath.getLhs(), domainModel, tableGroupLocator);
    } else {
        modelPart = lhsTableGroup.getModelPart();
    }
    return modelPart.findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityCollectionPart(org.hibernate.metamodel.mapping.internal.EntityCollectionPart) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) AbstractSqmSpecificPluralPartPath(org.hibernate.query.sqm.tree.domain.AbstractSqmSpecificPluralPartPath) SqmTreatedPath(org.hibernate.query.sqm.tree.domain.SqmTreatedPath) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) EntityCollectionPart(org.hibernate.metamodel.mapping.internal.EntityCollectionPart) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer)

Example 2 with AbstractSqmSpecificPluralPartPath

use of org.hibernate.query.sqm.tree.domain.AbstractSqmSpecificPluralPartPath in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method prepareForSelection.

private void prepareForSelection(SqmPath<?> selectionPath) {
    final SqmPath<?> path;
    // through a cardinality preserving mechanism in visitIndexAggregateFunction/visitElementAggregateFunction
    if (selectionPath instanceof AbstractSqmSpecificPluralPartPath<?>) {
        path = selectionPath.getLhs().getLhs();
    } else {
        path = selectionPath;
    }
    final FromClauseIndex fromClauseIndex = getFromClauseIndex();
    final TableGroup tableGroup = fromClauseIndex.findTableGroup(path.getNavigablePath());
    if (tableGroup == null) {
        prepareReusablePath(path, () -> null);
        final NavigablePath navigablePath;
        if (CollectionPart.Nature.fromNameExact(path.getNavigablePath().getUnaliasedLocalName()) != null) {
            navigablePath = path.getLhs().getLhs().getNavigablePath();
        } else if (path instanceof SqmTreatedRoot<?, ?>) {
            navigablePath = ((SqmTreatedRoot<?, ?>) path).getWrappedPath().getNavigablePath();
        } else {
            navigablePath = path.getLhs().getNavigablePath();
        }
        final TableGroup createdTableGroup = createTableGroup(fromClauseIndex.getTableGroup(navigablePath), path);
        if (createdTableGroup != null) {
            if (path instanceof SqmTreatedPath<?, ?>) {
                fromClauseIndex.register(path, createdTableGroup);
            }
            if (path instanceof SqmFrom<?, ?>) {
                registerTreatUsage((SqmFrom<?, ?>) path, createdTableGroup);
            }
        }
    } else if (path instanceof SqmFrom<?, ?>) {
        registerTreatUsage((SqmFrom<?, ?>) path, tableGroup);
    }
}
Also used : VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) NavigablePath(org.hibernate.query.spi.NavigablePath) SqmFrom(org.hibernate.query.sqm.tree.from.SqmFrom) AbstractSqmSpecificPluralPartPath(org.hibernate.query.sqm.tree.domain.AbstractSqmSpecificPluralPartPath) SqmTreatedPath(org.hibernate.query.sqm.tree.domain.SqmTreatedPath)

Aggregations

AbstractSqmSpecificPluralPartPath (org.hibernate.query.sqm.tree.domain.AbstractSqmSpecificPluralPartPath)2 SqmTreatedPath (org.hibernate.query.sqm.tree.domain.SqmTreatedPath)2 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)2 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)1 ModelPartContainer (org.hibernate.metamodel.mapping.ModelPartContainer)1 EntityCollectionPart (org.hibernate.metamodel.mapping.internal.EntityCollectionPart)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 SqmFrom (org.hibernate.query.sqm.tree.from.SqmFrom)1 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)1 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)1 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)1 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)1 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)1 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)1