Search in sources :

Example 1 with EntityDomainType

use of org.hibernate.metamodel.model.domain.EntityDomainType in project hibernate-orm by hibernate.

the class StandardEntityGraphTraversalStateImpl method appliesTo.

private boolean appliesTo(FetchParent fetchParent) {
    if (currentGraphContext == null || !(fetchParent instanceof EntityResultGraphNode)) {
        return false;
    }
    final EntityResultGraphNode entityFetchParent = (EntityResultGraphNode) fetchParent;
    final EntityMappingType entityFetchParentMappingType = entityFetchParent.getEntityValuedModelPart().getEntityMappingType();
    assert currentGraphContext.getGraphedType() instanceof EntityDomainType;
    final EntityDomainType entityDomainType = (EntityDomainType) currentGraphContext.getGraphedType();
    return entityDomainType.getHibernateEntityName().equals(entityFetchParentMappingType.getEntityName());
}
Also used : EntityResultGraphNode(org.hibernate.sql.results.graph.entity.EntityResultGraphNode) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 2 with EntityDomainType

use of org.hibernate.metamodel.model.domain.EntityDomainType in project hibernate-orm by hibernate.

the class MappingMetamodelImpl method resolveMappingExpressible.

@Override
public MappingModelExpressible<?> resolveMappingExpressible(SqmExpressible<?> sqmExpressible, Function<NavigablePath, TableGroup> tableGroupLocator) {
    if (sqmExpressible instanceof SqmPath) {
        final SqmPath<?> sqmPath = (SqmPath<?>) sqmExpressible;
        final NavigablePath navigablePath = sqmPath.getNavigablePath();
        if (navigablePath.getParent() != null) {
            final TableGroup parentTableGroup = tableGroupLocator.apply(navigablePath.getParent());
            return parentTableGroup.getModelPart().findSubPart(navigablePath.getLocalName(), null);
        }
        return tableGroupLocator.apply(navigablePath.getParent()).getModelPart();
    }
    if (sqmExpressible instanceof BasicType<?>) {
        return (BasicType<?>) sqmExpressible;
    }
    if (sqmExpressible instanceof BasicSqmPathSource<?>) {
        return getTypeConfiguration().getBasicTypeForJavaType(((BasicSqmPathSource<?>) sqmExpressible).getJavaType());
    }
    if (sqmExpressible instanceof SqmFieldLiteral) {
        return getTypeConfiguration().getBasicTypeForJavaType(((SqmFieldLiteral<?>) sqmExpressible).getJavaType());
    }
    if (sqmExpressible instanceof CompositeSqmPathSource) {
        throw new NotYetImplementedFor6Exception("Resolution of embedded-valued SqmExpressible nodes not yet implemented");
    }
    if (sqmExpressible instanceof EmbeddableTypeImpl) {
        return (MappingModelExpressible<?>) sqmExpressible;
    }
    if (sqmExpressible instanceof EntityDomainType<?>) {
        return getEntityDescriptor(((EntityDomainType<?>) sqmExpressible).getHibernateEntityName());
    }
    if (sqmExpressible instanceof TupleType<?>) {
        final MappingModelExpressible<?> mappingModelExpressible = tupleTypeCache.get(sqmExpressible);
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
        final TupleType<?> tupleType = (TupleType<?>) sqmExpressible;
        final MappingModelExpressible<?>[] components = new MappingModelExpressible<?>[tupleType.componentCount()];
        for (int i = 0; i < components.length; i++) {
            components[i] = resolveMappingExpressible(tupleType.get(i), tableGroupLocator);
        }
        final MappingModelExpressible<?> createdMappingModelExpressible = new TupleMappingModelExpressible(components);
        final MappingModelExpressible<?> existingMappingModelExpressible = tupleTypeCache.putIfAbsent(tupleType, createdMappingModelExpressible);
        return existingMappingModelExpressible == null ? createdMappingModelExpressible : existingMappingModelExpressible;
    }
    return null;
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) BasicType(org.hibernate.type.BasicType) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) TupleType(org.hibernate.metamodel.model.domain.TupleType) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) SqmFieldLiteral(org.hibernate.query.sqm.tree.expression.SqmFieldLiteral)

Example 3 with EntityDomainType

use of org.hibernate.metamodel.model.domain.EntityDomainType 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 4 with EntityDomainType

use of org.hibernate.metamodel.model.domain.EntityDomainType in project hibernate-orm by hibernate.

the class MatchingIdSelectionHelper method generateMatchingIdSelectQuery.

/**
 * @asciidoc
 *
 * Generates a query-spec for selecting all ids matching the restriction defined as part
 * of the user's update/delete query.  This query-spec is generally used:
 *
 * 		* to select all the matching ids via JDBC - see {@link MatchingIdSelectionHelper#selectMatchingIds}
 * 		* as a sub-query restriction to insert rows into an "id table"
 */
public static QuerySpec generateMatchingIdSelectQuery(EntityMappingType targetEntityDescriptor, SqmDeleteOrUpdateStatement sqmStatement, DomainParameterXref domainParameterXref, Predicate restriction, MultiTableSqmMutationConverter sqmConverter, SessionFactoryImplementor sessionFactory) {
    final EntityDomainType entityDomainType = sqmStatement.getTarget().getModel();
    if (log.isTraceEnabled()) {
        log.tracef("Starting generation of entity-id SQM selection - %s", entityDomainType.getHibernateEntityName());
    }
    final QuerySpec idSelectionQuery = new QuerySpec(true, 1);
    final TableGroup mutatingTableGroup = sqmConverter.getMutatingTableGroup();
    idSelectionQuery.getFromClause().addRoot(mutatingTableGroup);
    targetEntityDescriptor.getIdentifierMapping().forEachSelectable((position, selection) -> {
        final TableReference tableReference = mutatingTableGroup.resolveTableReference(mutatingTableGroup.getNavigablePath(), selection.getContainingTableExpression());
        final Expression expression = sqmConverter.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sessionFactory));
        idSelectionQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(position, position + 1, expression));
    });
    idSelectionQuery.applyPredicate(restriction);
    return idSelectionQuery;
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 5 with EntityDomainType

use of org.hibernate.metamodel.model.domain.EntityDomainType in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method verifyAnimalZooSelection.

private static void verifyAnimalZooSelection(Query q) {
    final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
    final SqmSelection<?> sqmSelection = sqmStatement.getQuerySpec().getSelectClause().getSelections().get(0);
    assertThat(sqmSelection.getSelectableNode(), instanceOf(SqmPath.class));
    final SqmPath<?> selectedPath = (SqmPath<?>) sqmSelection.getSelectableNode();
    assertThat(selectedPath.getReferencedPathSource(), instanceOf(EntitySqmPathSource.class));
    final EntitySqmPathSource selectedAttr = (EntitySqmPathSource) selectedPath.getReferencedPathSource();
    assertThat(selectedAttr.getPathName(), is("zoo"));
    assertThat(selectedAttr.getSqmPathType(), instanceOf(EntityDomainType.class));
    final EntityDomainType<?> zooType = (EntityDomainType<?>) selectedAttr.getSqmPathType();
    assertThat(zooType.getHibernateEntityName(), is(Zoo.class.getName()));
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) EntitySqmPathSource(org.hibernate.metamodel.model.domain.internal.EntitySqmPathSource) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType)

Aggregations

EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)13 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)7 ArrayList (java.util.ArrayList)3 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)3 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)3 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)2 MappedSuperclass (org.hibernate.mapping.MappedSuperclass)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)2 ModelPart (org.hibernate.metamodel.mapping.ModelPart)2 EmbeddableDomainType (org.hibernate.metamodel.model.domain.EmbeddableDomainType)2 JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)2 MappedSuperclassDomainType (org.hibernate.metamodel.model.domain.MappedSuperclassDomainType)2 PersistentAttribute (org.hibernate.metamodel.model.domain.PersistentAttribute)2 NavigablePath (org.hibernate.query.spi.NavigablePath)2 SqmPolymorphicRootDescriptor (org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor)2 SqmTreatedPath (org.hibernate.query.sqm.tree.domain.SqmTreatedPath)2 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)2