Search in sources :

Example 1 with EmbeddedCollectionPart

use of org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitMemberOfPredicate.

@Override
public Predicate visitMemberOfPredicate(SqmMemberOfPredicate predicate) {
    final SqmPath<?> pluralPath = predicate.getPluralPath();
    prepareReusablePath(pluralPath, () -> null);
    final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) determineValueMapping(pluralPath);
    if (pluralAttributeMapping.getElementDescriptor() instanceof EntityCollectionPart) {
        inferrableTypeAccessStack.push(() -> ((EntityCollectionPart) pluralAttributeMapping.getElementDescriptor()).getKeyTargetMatchPart());
    } else if (pluralAttributeMapping.getElementDescriptor() instanceof EmbeddedCollectionPart) {
        inferrableTypeAccessStack.push(pluralAttributeMapping::getElementDescriptor);
    } else {
        inferrableTypeAccessStack.push(() -> pluralAttributeMapping);
    }
    final Expression lhs;
    try {
        lhs = (Expression) predicate.getLeftHandExpression().accept(this);
    } finally {
        inferrableTypeAccessStack.pop();
    }
    final FromClauseAccess parentFromClauseAccess = getFromClauseAccess();
    final QuerySpec subQuerySpec = new QuerySpec(false);
    pushProcessingState(new SqlAstQueryPartProcessingStateImpl(subQuerySpec, getCurrentProcessingState(), this, currentClauseStack::getCurrent, false));
    try {
        final TableGroup tableGroup = pluralAttributeMapping.createRootTableGroup(true, pluralPath.getNavigablePath(), null, () -> subQuerySpec::applyPredicate, this, creationContext);
        pluralAttributeMapping.applyBaseRestrictions(subQuerySpec::applyPredicate, tableGroup, true, getLoadQueryInfluencers().getEnabledFilters(), null, this);
        getFromClauseAccess().registerTableGroup(pluralPath.getNavigablePath(), tableGroup);
        registerPluralTableGroupParts(tableGroup);
        subQuerySpec.getFromClause().addRoot(tableGroup);
        final CollectionPart elementDescriptor = pluralAttributeMapping.getElementDescriptor();
        if (elementDescriptor instanceof EntityCollectionPart) {
            ((EntityCollectionPart) elementDescriptor).getKeyTargetMatchPart().createDomainResult(pluralPath.getNavigablePath(), tableGroup, null, this);
        } else {
            elementDescriptor.createDomainResult(pluralPath.getNavigablePath(), tableGroup, null, this);
        }
        subQuerySpec.applyPredicate(pluralAttributeMapping.getKeyDescriptor().generateJoinPredicate(parentFromClauseAccess.findTableGroup(pluralPath.getNavigablePath().getParent()), tableGroup, getSqlExpressionResolver(), creationContext));
    } finally {
        popProcessingStateStack();
    }
    return new InSubQueryPredicate(lhs, subQuerySpec, predicate.isNegated(), getBooleanType());
}
Also used : SqlAstQueryPartProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstQueryPartProcessingStateImpl) EntityCollectionPart(org.hibernate.metamodel.mapping.internal.EntityCollectionPart) 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) EmbeddedCollectionPart(org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) EntityCollectionPart(org.hibernate.metamodel.mapping.internal.EntityCollectionPart) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EmbeddedCollectionPart(org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart)

Example 2 with EmbeddedCollectionPart

use of org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart in project hibernate-orm by hibernate.

the class EmbeddableAccessTests method verifyRuntimeModel.

@Test
public void verifyRuntimeModel(SessionFactoryScope scope) {
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final EntityMappingType personDescriptor = runtimeMetamodels.getEntityMappingType(Person.class);
    // Person defines FIELD access, while Name uses PROPERTY
    // - if we find the property annotations, the attribute names will be
    // `firstName` and `lastName`, and the columns `first_name` and `last_name`
    // - otherwise, we have property and column names being `first` and `last`
    final EmbeddableMappingType nameEmbeddable = ((EmbeddedAttributeMapping) personDescriptor.findAttributeMapping("name")).getEmbeddableTypeDescriptor();
    assertThat(nameEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
    final AttributeMapping nameFirst = nameEmbeddable.getAttributeMapping(0);
    final AttributeMapping nameLast = nameEmbeddable.getAttributeMapping(1);
    assertThat(nameFirst.getAttributeName()).isEqualTo("firstName");
    assertThat(nameLast.getAttributeName()).isEqualTo("lastName");
    final PluralAttributeMapping aliasesAttribute = (PluralAttributeMapping) personDescriptor.findAttributeMapping("aliases");
    final EmbeddableMappingType aliasEmbeddable = ((EmbeddedCollectionPart) aliasesAttribute.getElementDescriptor()).getEmbeddableTypeDescriptor();
    assertThat(aliasEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
    final AttributeMapping aliasFirst = nameEmbeddable.getAttributeMapping(0);
    final AttributeMapping aliasLast = nameEmbeddable.getAttributeMapping(1);
    assertThat(aliasFirst.getAttributeName()).isEqualTo("firstName");
    assertThat(aliasLast.getAttributeName()).isEqualTo("lastName");
}
Also used : EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) EmbeddedCollectionPart(org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test)

Aggregations

PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)2 EmbeddedCollectionPart (org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart)2 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)1 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)1 EntityCollectionPart (org.hibernate.metamodel.mapping.internal.EntityCollectionPart)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 SqlAstQueryPartProcessingStateImpl (org.hibernate.query.sqm.sql.internal.SqlAstQueryPartProcessingStateImpl)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 SqmInSubQueryPredicate (org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate)1 SqmQuerySpec (org.hibernate.query.sqm.tree.select.SqmQuerySpec)1 FromClauseAccess (org.hibernate.sql.ast.spi.FromClauseAccess)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1