Search in sources :

Example 6 with JoinSequence

use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.

the class SessionFactoryHelper method createJoinSequence.

/**
	 * Generate a join sequence representing the given association type.
	 *
	 * @param implicit Should implicit joins (theta-style) or explicit joins (ANSI-style) be rendered
	 * @param associationType The type representing the thing to be joined into.
	 * @param tableAlias The table alias to use in qualifying the join conditions
	 * @param joinType The type of join to render (inner, outer, etc);  see {@link org.hibernate.sql.JoinFragment}
	 * @param columns The columns making up the condition of the join.
	 *
	 * @return The generated join sequence.
	 */
public JoinSequence createJoinSequence(boolean implicit, AssociationType associationType, String tableAlias, JoinType joinType, String[] columns) {
    JoinSequence joinSequence = createJoinSequence();
    // Implicit joins use theta style (WHERE pk = fk), explicit joins use JOIN (afterQuery from)
    joinSequence.setUseThetaStyle(implicit);
    joinSequence.addJoin(associationType, tableAlias, joinType, columns);
    return joinSequence;
}
Also used : JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 7 with JoinSequence

use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.

the class MapKeyEntityFromElement method buildKeyJoin.

public static MapKeyEntityFromElement buildKeyJoin(FromElement collectionFromElement) {
    final HqlSqlWalker walker = collectionFromElement.getWalker();
    final SessionFactoryHelper sfh = walker.getSessionFactoryHelper();
    final SessionFactoryImplementor sf = sfh.getFactory();
    final QueryableCollection collectionPersister = collectionFromElement.getQueryableCollection();
    final Type indexType = collectionPersister.getIndexType();
    if (indexType == null) {
        throw new IllegalArgumentException("Given collection is not indexed");
    }
    if (!indexType.isEntityType()) {
        throw new IllegalArgumentException("Given collection does not have an entity index");
    }
    final EntityType indexEntityType = (EntityType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexEntityType.getAssociatedJoinable(sf);
    final String rhsAlias = walker.getAliasGenerator().createName(indexEntityPersister.getEntityName());
    final boolean useThetaJoin = collectionFromElement.getJoinSequence().isThetaStyle();
    MapKeyEntityFromElement join = new MapKeyEntityFromElement(useThetaJoin);
    join.initialize(HqlSqlTokenTypes.JOIN_FRAGMENT, ((Joinable) indexEntityPersister).getTableName());
    join.initialize(collectionFromElement.getWalker());
    join.initializeEntity(collectionFromElement.getFromClause(), indexEntityPersister.getEntityName(), indexEntityPersister, indexEntityType, "<map-key-join-" + collectionFromElement.getClassAlias() + ">", rhsAlias);
    //		String[] joinColumns = determineJoinColuns( collectionPersister, joinTableAlias );
    // todo : assumes columns, no formulas
    String[] joinColumns = collectionPersister.getIndexColumnNames(collectionFromElement.getCollectionTableAlias());
    JoinSequence joinSequence = sfh.createJoinSequence(useThetaJoin, indexEntityType, rhsAlias, //				JoinType.INNER_JOIN,
    collectionFromElement.getJoinSequence().getFirstJoin().getJoinType(), joinColumns);
    join.setJoinSequence(joinSequence);
    join.setOrigin(collectionFromElement, true);
    join.setColumns(joinColumns);
    join.setUseFromFragment(collectionFromElement.useFromFragment());
    join.setUseWhereFragment(collectionFromElement.useWhereFragment());
    walker.addQuerySpaces(indexEntityPersister.getQuerySpaces());
    return join;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) HqlSqlWalker(org.hibernate.hql.internal.ast.HqlSqlWalker) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 8 with JoinSequence

use of org.hibernate.engine.internal.JoinSequence 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)

Example 9 with JoinSequence

use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.

the class PathExpressionParser method prepareForIndex.

private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
    QueryableCollection collPersister = q.getCollectionPersister(collectionRole);
    if (!collPersister.hasIndex()) {
        throw new QueryException("unindexed collection beforeQuery []: " + path);
    }
    String[] indexCols = collPersister.getIndexColumnNames();
    if (indexCols.length != 1) {
        throw new QueryException("composite-index appears in []: " + path);
    }
    //String[] keyCols = collPersister.getKeyColumnNames();
    JoinSequence fromJoins = new JoinSequence(q.getFactory()).setUseThetaStyle(useThetaStyleJoin).setRoot(collPersister, collectionName).setNext(joinSequence.copy());
    if (!continuation) {
        addJoin(collectionName, collPersister.getCollectionType());
    }
    //TODO: get SQL rendering out of here
    joinSequence.addCondition(collectionName + '.' + indexCols[0] + " = ");
    CollectionElement elem = new CollectionElement();
    elem.elementColumns = collPersister.getElementColumnNames(collectionName);
    elem.elementType = collPersister.getElementType();
    elem.isOneToMany = collPersister.isOneToMany();
    elem.alias = collectionName;
    elem.joinSequence = joinSequence;
    collectionElements.addLast(elem);
    setExpectingCollectionIndex();
    q.addCollection(collectionName, collectionRole);
    q.addFromJoinOnly(collectionName, fromJoins);
}
Also used : QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 10 with JoinSequence

use of org.hibernate.engine.internal.JoinSequence in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method addFromClass.

void addFromClass(String name, Queryable classPersister) throws QueryException {
    JoinSequence joinSequence = new JoinSequence(getFactory()).setRoot(classPersister, name);
    //crossJoins.add(name);
    addFrom(name, classPersister.getEntityName(), joinSequence);
}
Also used : JoinSequence(org.hibernate.engine.internal.JoinSequence)

Aggregations

JoinSequence (org.hibernate.engine.internal.JoinSequence)19 EntityType (org.hibernate.type.EntityType)7 Type (org.hibernate.type.Type)7 QueryException (org.hibernate.QueryException)6 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)5 SessionFactoryHelper (org.hibernate.hql.internal.ast.util.SessionFactoryHelper)4 AssociationType (org.hibernate.type.AssociationType)4 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 MappingException (org.hibernate.MappingException)2 FromClause (org.hibernate.hql.internal.ast.tree.FromClause)2 ImpliedFromElement (org.hibernate.hql.internal.ast.tree.ImpliedFromElement)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 CompositeType (org.hibernate.type.CompositeType)2 RecognitionException (antlr.RecognitionException)1 SemanticException (antlr.SemanticException)1 AST (antlr.collections.AST)1