Search in sources :

Example 1 with ModelPart

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

the class ToOneAttributeMapping method resolveCircularFetch.

@Override
public Fetch resolveCircularFetch(NavigablePath fetchablePath, FetchParent fetchParent, FetchTiming fetchTiming, DomainResultCreationState creationState) {
    final AssociationKey associationKey = foreignKeyDescriptor.getAssociationKey();
    if (creationState.isAssociationKeyVisited(associationKey) || bidirectionalAttributeName != null && !creationState.isRegisteringVisitedAssociationKeys()) {
        NavigablePath parentNavigablePath = fetchablePath.getParent();
        assert parentNavigablePath.equals(fetchParent.getNavigablePath());
        /*
				@Entity
				public class Card {
					@Id
					private String id;
					@ManyToOne
					private CardField field;
				}
				@Entity
				public class CardField {
					@EmbeddedId
					private PrimaryKey primaryKey;
				}
				@Embeddable
				public class PrimaryKey {
					@ManyToOne(optional = false)
					private Card card;
					@ManyToOne(optional = false)
					private Key key;
				}
			 */
        if (parentNavigablePath.getLocalName().equals(ForeignKeyDescriptor.PART_NAME)) {
            // todo (6.0): maybe it's better to have a flag in creation state that marks if we are building a circular fetch domain result already to skip this?
            return null;
        }
        ModelPart parentModelPart = creationState.resolveModelPart(parentNavigablePath);
        if (parentModelPart instanceof EmbeddedIdentifierMappingImpl) {
            while (parentNavigablePath instanceof EntityIdentifierNavigablePath) {
                parentNavigablePath = parentNavigablePath.getParent();
                assert parentNavigablePath != null;
                parentModelPart = creationState.resolveModelPart(parentNavigablePath);
            }
        }
        while (parentModelPart instanceof EmbeddableValuedFetchable) {
            parentNavigablePath = parentNavigablePath.getParent();
            assert parentNavigablePath != null;
            parentModelPart = creationState.resolveModelPart(parentNavigablePath);
        }
        if (isBidirectionalAttributeName(parentNavigablePath, parentModelPart, fetchablePath, creationState)) {
            return createCircularBiDirectionalFetch(fetchablePath, fetchParent, parentNavigablePath, creationState);
        }
        /*
						class Child {
							@OneToOne
							private Mother mother;
						}

						class Mother {
							@OneToOne
							private Child stepMother;
						}

				We have a circularity but it is not bidirectional
			 */
        final TableGroup parentTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().getTableGroup(fetchParent.getNavigablePath());
        final DomainResult<?> foreignKeyDomainResult;
        assert !creationState.isResolvingCircularFetch();
        try {
            creationState.setResolvingCircularFetch(true);
            foreignKeyDomainResult = foreignKeyDescriptor.createDomainResult(fetchablePath, parentTableGroup, sideNature, creationState);
        } finally {
            creationState.setResolvingCircularFetch(false);
        }
        return new CircularFetchImpl(this, getEntityMappingType(), fetchTiming, fetchablePath, fetchParent, this, isSelectByUniqueKey(sideNature), fetchablePath, foreignKeyDomainResult);
    }
    return null;
}
Also used : AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) CircularFetchImpl(org.hibernate.sql.results.internal.domain.CircularFetchImpl) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable)

Example 2 with ModelPart

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

the class ToOneAttributeMapping method generateFetch.

@Override
public EntityFetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
    final TableGroup parentTableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath());
    final NavigablePath parentNavigablePath = fetchablePath.getParent();
    assert parentNavigablePath.equals(fetchParent.getNavigablePath()) || fetchParent.getNavigablePath() instanceof TreatedNavigablePath && parentNavigablePath.equals(fetchParent.getNavigablePath().getRealParent());
    if (fetchTiming == FetchTiming.IMMEDIATE && selected) {
        final TableGroup tableGroup;
        if (fetchParent instanceof EntityResultJoinedSubclassImpl && ((EntityPersister) fetchParent.getReferencedModePart()).findDeclaredAttributeMapping(getPartName()) == null) {
            final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getJoinType(fetchablePath, parentTableGroup), true, false, creationState.getSqlAstCreationState());
            parentTableGroup.addTableGroupJoin(tableGroupJoin);
            tableGroup = tableGroupJoin.getJoinedGroup();
            fromClauseAccess.registerTableGroup(fetchablePath, tableGroup);
        } else {
            tableGroup = fromClauseAccess.resolveTableGroup(fetchablePath, np -> {
                final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getDefaultSqlAstJoinType(parentTableGroup), true, false, creationState.getSqlAstCreationState());
                parentTableGroup.addTableGroupJoin(tableGroupJoin);
                return tableGroupJoin.getJoinedGroup();
            });
        }
        final boolean added = creationState.registerVisitedAssociationKey(foreignKeyDescriptor.getAssociationKey());
        AssociationKey additionalAssociationKey = null;
        if (cardinality == Cardinality.LOGICAL_ONE_TO_ONE && bidirectionalAttributeName != null) {
            final ModelPart bidirectionalModelPart = entityMappingType.findSubPart(bidirectionalAttributeName);
            // Add the inverse association key side as well to be able to resolve to a CircularFetch
            if (bidirectionalModelPart instanceof ToOneAttributeMapping) {
                assert bidirectionalModelPart.getPartMappingType() == declaringTableGroupProducer;
                final ToOneAttributeMapping bidirectionalAttribute = (ToOneAttributeMapping) bidirectionalModelPart;
                final AssociationKey secondKey = bidirectionalAttribute.getForeignKeyDescriptor().getAssociationKey();
                if (creationState.registerVisitedAssociationKey(secondKey)) {
                    additionalAssociationKey = secondKey;
                }
            }
        }
        final EntityFetchJoinedImpl entityFetchJoined = new EntityFetchJoinedImpl(fetchParent, this, tableGroup, fetchablePath, creationState);
        if (added) {
            creationState.removeVisitedAssociationKey(foreignKeyDescriptor.getAssociationKey());
        }
        if (additionalAssociationKey != null) {
            creationState.removeVisitedAssociationKey(additionalAssociationKey);
        }
        return entityFetchJoined;
    }
    /*
			1. No JoinTable
				Model:
					EntityA{
						@ManyToOne
						EntityB b
					}

					EntityB{
						@ManyToOne
						EntityA a
					}

				Relational:
					ENTITY_A( id )
					ENTITY_B( id, entity_a_id)

				1.1 EntityA -> EntityB : as keyResult we need ENTITY_B.id
				1.2 EntityB -> EntityA : as keyResult we need ENTITY_B.entity_a_id (FK referring column)

			2. JoinTable

		 */
    final ForeignKeyDescriptor.Nature resolvingKeySideOfForeignKey = creationState.getCurrentlyResolvingForeignKeyPart();
    final ForeignKeyDescriptor.Nature side;
    if (resolvingKeySideOfForeignKey == ForeignKeyDescriptor.Nature.KEY && this.sideNature == ForeignKeyDescriptor.Nature.TARGET) {
        // If we are currently resolving the key part of a foreign key we do not want to add joins.
        // So if the lhs of this association is the target of the FK, we have to use the KEY part to avoid a join
        side = ForeignKeyDescriptor.Nature.KEY;
    } else {
        side = this.sideNature;
    }
    final DomainResult<?> keyResult = foreignKeyDescriptor.createDomainResult(fetchablePath, parentTableGroup, side, creationState);
    final boolean selectByUniqueKey = isSelectByUniqueKey(side);
    if (fetchTiming == FetchTiming.IMMEDIATE) {
        return new EntityFetchSelectImpl(fetchParent, this, fetchablePath, keyResult, selectByUniqueKey, creationState);
    }
    return new EntityDelayedFetchImpl(fetchParent, this, fetchablePath, keyResult, selectByUniqueKey);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) Arrays(java.util.Arrays) EntityPersister(org.hibernate.persister.entity.EntityPersister) Property(org.hibernate.mapping.Property) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultImpl) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ToOne(org.hibernate.mapping.ToOne) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EntityAssociationMapping(org.hibernate.metamodel.mapping.EntityAssociationMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) PersistentClass(org.hibernate.mapping.PersistentClass) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) Join(org.hibernate.mapping.Join) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityDelayedResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedResultImpl) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) EntityDelayedFetchImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchImpl) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) OneToOne(org.hibernate.mapping.OneToOne) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) NavigablePath(org.hibernate.query.spi.NavigablePath) StringHelper(org.hibernate.internal.util.StringHelper) DomainResult(org.hibernate.sql.results.graph.DomainResult) Set(java.util.Set) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) ArrayHelper(org.hibernate.internal.util.collections.ArrayHelper) CircularBiDirectionalFetchImpl(org.hibernate.sql.results.internal.domain.CircularBiDirectionalFetchImpl) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) CircularFetchImpl(org.hibernate.sql.results.internal.domain.CircularFetchImpl) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) EntityFetchSelectImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) EntityResultJoinedSubclassImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl) SqlAliasStemHelper(org.hibernate.sql.ast.spi.SqlAliasStemHelper) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) Clause(org.hibernate.sql.ast.Clause) TableReference(org.hibernate.sql.ast.tree.from.TableReference) HashSet(java.util.HashSet) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) BiConsumer(java.util.function.BiConsumer) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) ManyToOne(org.hibernate.mapping.ManyToOne) LockMode(org.hibernate.LockMode) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Iterator(java.util.Iterator) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) FetchStyle(org.hibernate.engine.FetchStyle) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) Selectable(org.hibernate.mapping.Selectable) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) EntityFetchJoinedImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl) Type(org.hibernate.type.Type) EntityFetchJoinedImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityResultJoinedSubclassImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl) EntityDelayedFetchImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchImpl) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) EntityFetchSelectImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl)

Example 3 with ModelPart

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

the class MappingModelCreationHelper method interpretPluralAttributeMappingKeyDescriptor.

private static void interpretPluralAttributeMappingKeyDescriptor(PluralAttributeMappingImpl attributeMapping, Collection bootValueMapping, CollectionPersister collectionDescriptor, ManagedMappingType declaringType, Dialect dialect, MappingModelCreationProcess creationProcess) {
    ModelPart attributeMappingSubPart = null;
    if (!StringHelper.isEmpty(collectionDescriptor.getMappedByProperty())) {
        attributeMappingSubPart = ((ModelPartContainer) attributeMapping.getElementDescriptor().getPartMappingType()).findSubPart(collectionDescriptor.getMappedByProperty(), null);
    }
    if (attributeMappingSubPart instanceof ToOneAttributeMapping) {
        final ToOneAttributeMapping referencedAttributeMapping = (ToOneAttributeMapping) attributeMappingSubPart;
        setReferencedAttributeForeignKeyDescriptor(attributeMapping, referencedAttributeMapping, referencedAttributeMapping.findContainingEntityMapping().getEntityPersister(), collectionDescriptor.getMappedByProperty(), dialect, creationProcess);
        return;
    }
    final KeyValue bootValueMappingKey = bootValueMapping.getKey();
    final Type keyType = bootValueMappingKey.getType();
    final ModelPart fkTarget;
    final String lhsPropertyName = collectionDescriptor.getCollectionType().getLHSPropertyName();
    final boolean isReferenceToPrimaryKey = lhsPropertyName == null;
    final ManagedMappingType keyDeclaringType;
    if (collectionDescriptor.getElementType().isEntityType()) {
        keyDeclaringType = ((QueryableCollection) collectionDescriptor).getElementPersister();
    } else {
        // This is not "really correct" but it is as good as it gets.
        // The key declaring type serves as declaring type for the inverse model part of a FK.
        // Most of the time, there is a proper managed type, but not for basic collections.
        // Since the declaring type is needed for certain operations, we use the one from the target side of the FK
        keyDeclaringType = declaringType;
    }
    if (isReferenceToPrimaryKey) {
        fkTarget = collectionDescriptor.getOwnerEntityPersister().getIdentifierMapping();
    } else {
        fkTarget = declaringType.findAttributeMapping(lhsPropertyName);
    }
    if (keyType instanceof BasicType) {
        assert bootValueMappingKey.getColumnSpan() == 1;
        assert fkTarget instanceof BasicValuedModelPart;
        final BasicValuedModelPart simpleFkTarget = (BasicValuedModelPart) fkTarget;
        final String tableExpression = getTableIdentifierExpression(bootValueMappingKey.getTable(), creationProcess);
        final SelectableMapping keySelectableMapping = SelectableMappingImpl.from(tableExpression, bootValueMappingKey.getSelectables().get(0), (JdbcMapping) keyType, dialect, creationProcess.getSqmFunctionRegistry());
        attributeMapping.setForeignKeyDescriptor(new SimpleForeignKeyDescriptor(keyDeclaringType, simpleFkTarget, null, keySelectableMapping, simpleFkTarget, isReferenceToPrimaryKey, ((SimpleValue) bootValueMappingKey).isConstrained()));
    } else if (fkTarget instanceof EmbeddableValuedModelPart) {
        final EmbeddedForeignKeyDescriptor embeddedForeignKeyDescriptor = buildEmbeddableForeignKeyDescriptor((EmbeddableValuedModelPart) fkTarget, bootValueMapping, keyDeclaringType, collectionDescriptor.getAttributeMapping(), false, dialect, creationProcess);
        attributeMapping.setForeignKeyDescriptor(embeddedForeignKeyDescriptor);
    } else {
        throw new NotYetImplementedFor6Exception("Support for " + fkTarget.getClass() + " foreign keys not yet implemented: " + bootValueMapping.getRole());
    }
}
Also used : ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) KeyValue(org.hibernate.mapping.KeyValue) BasicType(org.hibernate.type.BasicType) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) SimpleValue(org.hibernate.mapping.SimpleValue) BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) CollectionMappingType(org.hibernate.metamodel.mapping.CollectionMappingType) JavaType(org.hibernate.type.descriptor.java.JavaType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception)

Example 4 with ModelPart

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

the class AbstractDomainPath method addSortSpecification.

private void addSortSpecification(EmbeddableValuedModelPart embeddableValuedModelPart, QuerySpec ast, TableGroup tableGroup, String collation, String modelPartName, SortOrder sortOrder, NullPrecedence nullPrecedence, SqlAstCreationState creationState) {
    if (embeddableValuedModelPart.getFetchableName().equals(modelPartName) || ELEMENT_TOKEN.equals(modelPartName)) {
        embeddableValuedModelPart.forEachSelectable((columnIndex, selection) -> {
            addSortSpecification(selection, ast, tableGroup, collation, sortOrder, nullPrecedence, creationState);
        });
    } else {
        ModelPart subPart = embeddableValuedModelPart.findSubPart(modelPartName, null);
        assert subPart instanceof BasicValuedModelPart;
        addSortSpecification((BasicValuedModelPart) subPart, ast, tableGroup, collation, sortOrder, nullPrecedence, creationState);
    }
}
Also used : BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart)

Example 5 with ModelPart

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

the class PluralAttributePath method resolvePathPart.

@Override
public DomainPath resolvePathPart(String name, String identifier, boolean isTerminal, TranslationContext translationContext) {
    final ModelPart subPart = pluralAttributeMapping.findSubPart(name, null);
    if (subPart != null) {
        if (subPart instanceof CollectionPart) {
            return new CollectionPartPath(this, (CollectionPart) subPart);
        }
        if (subPart instanceof EmbeddableValuedModelPart) {
            return new DomainPathContinuation(navigablePath.append(name), this, subPart);
        }
        if (subPart instanceof ToOneAttributeMapping) {
            return new FkDomainPathContinuation(navigablePath.append(name), this, (ToOneAttributeMapping) subPart);
        }
        // leaf case:
        final CollectionPartPath elementPath = new CollectionPartPath(this, pluralAttributeMapping.getElementDescriptor());
        return (DomainPath) elementPath.resolvePathPart(name, identifier, isTerminal, translationContext);
    }
    if (pluralAttributeMapping.getElementDescriptor() instanceof EmbeddableValuedModelPart) {
        final EmbeddableValuedModelPart elementDescriptor = (EmbeddableValuedModelPart) pluralAttributeMapping.getElementDescriptor();
        final ModelPart elementSubPart = elementDescriptor.findSubPart(name, null);
        if (elementSubPart != null) {
            // create the CollectionSubPath to use as the `lhs` for the element sub-path
            final CollectionPartPath elementPath = new CollectionPartPath(this, (CollectionPart) elementDescriptor);
            return new DomainPathContinuation(elementPath.getNavigablePath().append(name), this, elementSubPart);
        }
    }
    if (pluralAttributeMapping.getIndexDescriptor() instanceof EmbeddableValuedModelPart) {
        final EmbeddableValuedModelPart indexDescriptor = (EmbeddableValuedModelPart) pluralAttributeMapping.getIndexDescriptor();
        final ModelPart indexSubPart = indexDescriptor.findSubPart(name, null);
        if (indexSubPart != null) {
            // create the CollectionSubPath to use as the `lhs` for the element sub-path
            final CollectionPartPath indexPath = new CollectionPartPath(this, (CollectionPart) indexDescriptor);
            return new DomainPathContinuation(indexPath.getNavigablePath().append(name), this, indexSubPart);
        }
    }
    return null;
}
Also used : AbstractDomainPath(org.hibernate.metamodel.mapping.internal.AbstractDomainPath) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart)

Aggregations

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