Search in sources :

Example 1 with ListPersistentAttribute

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

the class SqmPluralValuedSimplePath method resolveIndexedAccess.

@Override
public SqmPath<?> resolveIndexedAccess(SqmExpression<?> selector, boolean isTerminal, SqmCreationState creationState) {
    final SqmPathRegistry pathRegistry = creationState.getCurrentProcessingState().getPathRegistry();
    final String alias = selector.toHqlString();
    final NavigablePath navigablePath = getNavigablePath().getParent().append(getNavigablePath().getUnaliasedLocalName(), alias).append(CollectionPart.Nature.ELEMENT.getName());
    final SqmFrom<?, ?> indexedPath = pathRegistry.findFromByPath(navigablePath);
    if (indexedPath != null) {
        return indexedPath;
    }
    SqmFrom<?, ?> path = pathRegistry.findFromByPath(navigablePath.getParent());
    if (path == null) {
        final PluralPersistentAttribute<?, ?, E> referencedPathSource = getReferencedPathSource();
        final SqmFrom<?, Object> parent = pathRegistry.resolveFrom(getLhs());
        final SqmQualifiedJoin<Object, ?> join;
        final SqmExpression<?> index;
        if (referencedPathSource instanceof ListPersistentAttribute<?, ?>) {
            // noinspection unchecked
            join = new SqmListJoin<>(parent, (ListPersistentAttribute<Object, ?>) referencedPathSource, alias, SqmJoinType.INNER, false, parent.nodeBuilder());
            index = ((SqmListJoin<?, ?>) join).index();
        } else if (referencedPathSource instanceof MapPersistentAttribute<?, ?, ?>) {
            // noinspection unchecked
            join = new SqmMapJoin<>(parent, (MapPersistentAttribute<Object, ?, ?>) referencedPathSource, alias, SqmJoinType.INNER, false, parent.nodeBuilder());
            index = ((SqmMapJoin<?, ?, ?>) join).key();
        } else {
            throw new SemanticException("Index access is only supported on list or map attributes: " + getNavigablePath());
        }
        join.setJoinPredicate(creationState.getCreationContext().getNodeBuilder().equal(index, selector));
        parent.addSqmJoin(join);
        pathRegistry.register(path = join);
    }
    final SqmIndexedCollectionAccessPath<Object> result = new SqmIndexedCollectionAccessPath<>(navigablePath, path, selector);
    pathRegistry.register(result);
    return result;
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) ListPersistentAttribute(org.hibernate.metamodel.model.domain.ListPersistentAttribute) SqmPathRegistry(org.hibernate.query.hql.spi.SqmPathRegistry) SemanticException(org.hibernate.query.SemanticException)

Aggregations

ListPersistentAttribute (org.hibernate.metamodel.model.domain.ListPersistentAttribute)1 SemanticException (org.hibernate.query.SemanticException)1 SqmPathRegistry (org.hibernate.query.hql.spi.SqmPathRegistry)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1