Search in sources :

Example 11 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class SqlGenerator method fromFragmentSeparator.

@Override
protected void fromFragmentSeparator(AST a) {
    // check two "adjecent" nodes at the top of the from-clause tree
    AST next = a.getNextSibling();
    if (next == null || !hasText(a)) {
        return;
    }
    FromElement left = (FromElement) a;
    FromElement right = (FromElement) next;
    // writes something to the SQL
    while (right != null && !hasText(right)) {
        right = (FromElement) right.getNextSibling();
    }
    if (right == null) {
        return;
    }
    if (!hasText(right)) {
        return;
    }
    if (right.getType() == ENTITY_JOIN) {
        out(" ");
    } else if (right.getRealOrigin() == left || (right.getRealOrigin() != null && right.getRealOrigin() == left.getRealOrigin())) {
        // both right and left reprersent joins originating from the same FromElement
        if (right.getJoinSequence() != null && right.getJoinSequence().isThetaStyle()) {
            writeCrossJoinSeparator();
        } else {
            out(" ");
        }
    } else {
        // these are just two unrelated table references
        writeCrossJoinSeparator();
    }
}
Also used : AST(antlr.collections.AST) FromElement(org.hibernate.hql.internal.ast.tree.FromElement)

Example 12 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class HqlSqlWalker method isNonQualifiedPropertyRef.

@Override
protected boolean isNonQualifiedPropertyRef(AST ident) {
    final String identText = ident.getText();
    if (currentFromClause.isFromElementAlias(identText)) {
        return false;
    }
    List fromElements = currentFromClause.getExplicitFromElements();
    if (fromElements.size() == 1) {
        final FromElement fromElement = (FromElement) fromElements.get(0);
        try {
            LOG.tracev("Attempting to resolve property [{0}] as a non-qualified ref", identText);
            return fromElement.getPropertyMapping(identText).toType(identText) != null;
        } catch (QueryException e) {
        // Should mean that no such property was found
        }
    }
    return false;
}
Also used : QueryException(org.hibernate.QueryException) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class HqlSqlWalker method lookupNonQualifiedProperty.

@Override
protected AST lookupNonQualifiedProperty(AST property) throws SemanticException {
    final FromElement fromElement = (FromElement) currentFromClause.getExplicitFromElements().get(0);
    AST syntheticDotNode = generateSyntheticDotNodeForNonQualifiedPropertyRef(property, fromElement);
    return lookupProperty(syntheticDotNode, false, getCurrentClauseType() == HqlSqlTokenTypes.SELECT);
}
Also used : AST(antlr.collections.AST) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement)

Example 14 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class HqlSqlWalker method createFromFilterElement.

@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
    FromElement fromElement = currentFromClause.addFromElement(filterEntity.getText(), alias);
    FromClause fromClause = fromElement.getFromClause();
    QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
    // Get the names of the columns used to link between the collection
    // owner and the collection elements.
    String[] keyColumnNames = persister.getKeyColumnNames();
    String fkTableAlias = persister.isOneToMany() ? fromElement.getTableAlias() : fromClause.getAliasGenerator().createName(collectionFilterRole);
    JoinSequence join = sessionFactoryHelper.createJoinSequence();
    join.setRoot(persister, fkTableAlias);
    if (!persister.isOneToMany()) {
        join.addJoin((AssociationType) persister.getElementType(), fromElement.getTableAlias(), JoinType.INNER_JOIN, persister.getElementColumnNames(fkTableAlias));
    }
    join.addCondition(fkTableAlias, keyColumnNames, " = ?");
    fromElement.setJoinSequence(join);
    fromElement.setFilter(true);
    LOG.debug("createFromFilterElement() : processed filter FROM element.");
    return fromElement;
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 15 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class EntityGraphQueryHint method getFromElements.

private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
    final List<FromElement> fromElements = new ArrayList<FromElement>();
    for (Object obj : attributeNodes) {
        final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
        final String attributeName = attributeNode.getAttributeName();
        final String className = origin.getClassName();
        // TODO: This is ignored by collection types and probably wrong for entity types.  Presumably it screws
        // with inheritance.
        final String role = className + "." + attributeName;
        final String classAlias = origin.getClassAlias();
        final String originTableAlias = origin.getTableAlias();
        final Type propertyType = origin.getPropertyType(attributeName, attributeName);
        try {
            FromElement fromElement = explicitFetches.get(role);
            boolean explicitFromElement = false;
            if (fromElement == null) {
                if (propertyType.isEntityType()) {
                    final EntityType entityType = (EntityType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
                    fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
                } else if (propertyType.isCollectionType()) {
                    CollectionType collectionType = (CollectionType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
                    fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
                }
            } else {
                explicitFromElement = true;
                fromElement.setInProjectionList(true);
                fromElement.setFetch(true);
            }
            if (fromElement != null) {
                if (!explicitFromElement) {
                    fromElements.add(fromElement);
                }
                // recurse into subgraphs
                for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
                    fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
                }
            }
        } catch (Exception e) {
            throw new QueryException("Could not apply the EntityGraph to the Query!", e);
        }
    }
    return fromElements;
}
Also used : AttributeNode(javax.persistence.AttributeNode) ArrayList(java.util.ArrayList) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) QueryException(org.hibernate.QueryException) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) ImpliedFromElement(org.hibernate.hql.internal.ast.tree.ImpliedFromElement) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) CollectionType(org.hibernate.type.CollectionType) FromElementFactory(org.hibernate.hql.internal.ast.tree.FromElementFactory) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Aggregations

FromElement (org.hibernate.hql.internal.ast.tree.FromElement)22 EntityJoinFromElement (org.hibernate.hql.internal.ast.tree.EntityJoinFromElement)9 Queryable (org.hibernate.persister.entity.Queryable)8 ArrayList (java.util.ArrayList)5 List (java.util.List)5 QueryException (org.hibernate.QueryException)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 AST (antlr.collections.AST)3 Iterator (java.util.Iterator)3 JoinSequence (org.hibernate.engine.internal.JoinSequence)3 DeleteStatement (org.hibernate.hql.internal.ast.tree.DeleteStatement)3 FromClause (org.hibernate.hql.internal.ast.tree.FromClause)3 UpdateStatement (org.hibernate.hql.internal.ast.tree.UpdateStatement)3 FromElementFactory (org.hibernate.hql.internal.ast.tree.FromElementFactory)2 FromReferenceNode (org.hibernate.hql.internal.ast.tree.FromReferenceNode)2 ImpliedFromElement (org.hibernate.hql.internal.ast.tree.ImpliedFromElement)2 QueryNode (org.hibernate.hql.internal.ast.tree.QueryNode)2 TableBasedDeleteHandlerImpl (org.hibernate.hql.spi.id.TableBasedDeleteHandlerImpl)2 TableBasedUpdateHandlerImpl (org.hibernate.hql.spi.id.TableBasedUpdateHandlerImpl)2 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)2