Search in sources :

Example 11 with ModelPart

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

the class EntityValuedPathInterpretation method from.

public static <T> EntityValuedPathInterpretation<T> from(SqmEntityValuedSimplePath<T> sqmPath, MappingModelExpressible<?> inferredMapping, SqmToSqlAstConverter sqlAstCreationState) {
    final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath());
    final EntityValuedModelPart pathMapping = (EntityValuedModelPart) sqlAstCreationState.getFromClauseAccess().findTableGroup(sqmPath.getLhs().getNavigablePath()).getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
    final EntityValuedModelPart mapping;
    if (inferredMapping instanceof EntityAssociationMapping) {
        final EntityAssociationMapping inferredAssociation = (EntityAssociationMapping) inferredMapping;
        if (pathMapping instanceof EntityAssociationMapping && inferredMapping != pathMapping) {
            // In here, the inferred mapping and the actual path mapping are association mappings,
            // but for different associations, so we have to check if both associations point to the same target
            final EntityAssociationMapping pathAssociation = (EntityAssociationMapping) pathMapping;
            final ModelPart pathTargetPart = pathAssociation.getForeignKeyDescriptor().getPart(pathAssociation.getSideNature().inverse());
            final ModelPart inferredTargetPart = inferredAssociation.getForeignKeyDescriptor().getPart(inferredAssociation.getSideNature().inverse());
            // which will render the FK of the path association
            if (pathTargetPart == inferredTargetPart) {
                mapping = pathMapping;
            } else {
                // Otherwise, we need to use the entity mapping type to force rendering the PK
                // for e.g. `a.assoc1 = a.assoc2` when both associations have different target join columns
                mapping = pathMapping.getEntityMappingType();
            }
        } else {
            // This is the case when the inferred mapping is an association, but the path mapping is not,
            // or the path mapping and the inferred mapping are for the same association
            mapping = (EntityValuedModelPart) inferredMapping;
        }
    } else {
        mapping = pathMapping;
    }
    final ModelPart resultModelPart;
    if (mapping instanceof EntityAssociationMapping) {
        final EntityAssociationMapping associationMapping = (EntityAssociationMapping) mapping;
        final ModelPart keyTargetMatchPart = associationMapping.getKeyTargetMatchPart();
        if (keyTargetMatchPart instanceof ToOneAttributeMapping) {
            resultModelPart = ((ToOneAttributeMapping) keyTargetMatchPart).getKeyTargetMatchPart();
        } else {
            resultModelPart = keyTargetMatchPart;
        }
    } else {
        resultModelPart = mapping.getEntityMappingType().getIdentifierMapping();
    }
    return from(sqmPath.getNavigablePath(), tableGroup, resultModelPart, mapping, mapping, sqlAstCreationState);
}
Also used : TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) EntityAssociationMapping(org.hibernate.metamodel.mapping.EntityAssociationMapping)

Example 12 with ModelPart

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

the class BasicValuedPathInterpretation method from.

/**
 * Static factory
 */
public static <T> BasicValuedPathInterpretation<T> from(SqmBasicValuedSimplePath<T> sqmPath, SqlAstCreationState sqlAstCreationState, SemanticQueryWalker sqmWalker, boolean jpaQueryComplianceEnabled) {
    final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
    final TableGroup tableGroup = fromClauseAccess.getTableGroup(sqmPath.getNavigablePath().getParent());
    EntityMappingType treatTarget = null;
    if (jpaQueryComplianceEnabled) {
        if (sqmPath.getLhs() instanceof SqmTreatedPath) {
            final EntityDomainType treatTargetDomainType = ((SqmTreatedPath) sqmPath.getLhs()).getTreatTarget();
            final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
            treatTarget = mappingMetamodel.findEntityDescriptor(treatTargetDomainType.getHibernateEntityName());
        } else if (sqmPath.getLhs().getNodeType() instanceof EntityDomainType) {
            final EntityDomainType entityDomainType = (EntityDomainType) sqmPath.getLhs().getNodeType();
            final MappingMetamodel mappingMetamodel = sqlAstCreationState.getCreationContext().getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
            treatTarget = mappingMetamodel.findEntityDescriptor(entityDomainType.getHibernateEntityName());
        }
    }
    final BasicValuedModelPart mapping = (BasicValuedModelPart) tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), treatTarget);
    if (mapping == null) {
        if (jpaQueryComplianceEnabled) {
            // to get the better error, see if we got nothing because of treat handling
            final ModelPart subPart = tableGroup.getModelPart().findSubPart(sqmPath.getReferencedPathSource().getPathName(), null);
            if (subPart != null) {
                throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.IMPLICIT_TREAT);
            }
        }
        throw new SemanticException("`" + sqmPath.getNavigablePath() + "` did not reference a known model part");
    }
    final TableReference tableReference = tableGroup.resolveTableReference(sqmPath.getNavigablePath(), mapping.getContainingTableExpression());
    final Expression expression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, mapping.getSelectionExpression()), sacs -> new ColumnReference(tableReference.getIdentificationVariable(), mapping, sqlAstCreationState.getCreationContext().getSessionFactory()));
    final ColumnReference columnReference;
    if (expression instanceof ColumnReference) {
        columnReference = ((ColumnReference) expression);
    } else if (expression instanceof SqlSelectionExpression) {
        final Expression selectedExpression = ((SqlSelectionExpression) expression).getSelection().getExpression();
        assert selectedExpression instanceof ColumnReference;
        columnReference = (ColumnReference) selectedExpression;
    } else {
        throw new UnsupportedOperationException("Unsupported basic-valued path expression : " + expression);
    }
    return new BasicValuedPathInterpretation<>(columnReference, sqmPath.getNavigablePath(), mapping, tableGroup);
}
Also used : TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) SqmTreatedPath(org.hibernate.query.sqm.tree.domain.SqmTreatedPath) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) TableReference(org.hibernate.sql.ast.tree.from.TableReference) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SemanticException(org.hibernate.query.SemanticException) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 13 with ModelPart

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

the class SmokeTests method testEntityBasedManyToOne.

@Test
public void testEntityBasedManyToOne(SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(OtherEntity.class);
    final EntityPersister simpleEntityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(SimpleEntity.class);
    final ModelPart part = entityDescriptor.findSubPart("simpleEntity");
    assertThat(part, notNullValue());
    assertThat(part, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping attrMapping = (ToOneAttributeMapping) part;
    assertThat(attrMapping.getAttributeName(), is("simpleEntity"));
    assertThat(attrMapping.getMappedType(), is(simpleEntityDescriptor));
    assertThat(attrMapping.getJavaType(), is(simpleEntityDescriptor.getJavaType()));
    assertThat(attrMapping.getDeclaringType(), is(entityDescriptor));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) Test(org.junit.jupiter.api.Test)

Example 14 with ModelPart

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

the class ManyToOneTest method basicTest.

@Test
public void basicTest(SessionFactoryScope scope) {
    final EntityPersister otherDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(OtherEntity.class);
    final ModelPart simpleEntityAssociation = otherDescriptor.findSubPart("simpleEntity");
    assertThat(simpleEntityAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation;
    ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_entity"));
        assertThat(selection.getSelectionExpression(), is("simple_entity_id"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("simple_entity"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Test(org.junit.jupiter.api.Test)

Example 15 with ModelPart

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

the class EntityWithBidirectionalAssociationTest method basicTest.

@Test
public void basicTest(SessionFactoryScope scope) {
    final EntityPersister parentDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Parent.class);
    final ModelPart childAssociation = parentDescriptor.findSubPart("child");
    assertThat(childAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping childAttributeMapping = (ToOneAttributeMapping) childAssociation;
    ForeignKeyDescriptor foreignKeyDescriptor = childAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("PARENT"));
        assertThat(selection.getSelectionExpression(), is("child_id"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("CHILD"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
    final EntityPersister childDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(Child.class);
    final ModelPart parentAssociation = childDescriptor.findSubPart("parent");
    assertThat(parentAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping parentAttributeMapping = (ToOneAttributeMapping) parentAssociation;
    foreignKeyDescriptor = parentAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("PARENT"));
        assertThat(selection.getSelectionExpression(), is("child_id"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("CHILD"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

ModelPart (org.hibernate.metamodel.mapping.ModelPart)45 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)21 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)21 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)16 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)15 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)14 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)13 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)13 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)13 NavigablePath (org.hibernate.spi.NavigablePath)13 ArrayList (java.util.ArrayList)12 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)11 Test (org.junit.jupiter.api.Test)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)9 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)9 Expression (org.hibernate.sql.ast.tree.expression.Expression)9 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)9 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)8 ConvertibleModelPart (org.hibernate.metamodel.mapping.ConvertibleModelPart)8