Search in sources :

Example 6 with JoinType

use of org.hibernate.sql.JoinType in project hibernate-orm by hibernate.

the class AbstractEntityPersister method createJoin.

protected JoinFragment createJoin(String name, boolean innerJoin, boolean includeSubclasses, Set<String> treatAsDeclarations) {
    // IMPL NOTE : all joins join to the pk of the driving table
    final String[] idCols = StringHelper.qualify(name, getIdentifierColumnNames());
    final JoinFragment join = getFactory().getDialect().createOuterJoinFragment();
    final int tableSpan = getSubclassTableSpan();
    // IMPL NOTE : notice that we skip the first table; it is the driving table!
    for (int j = 1; j < tableSpan; j++) {
        final JoinType joinType = determineSubclassTableJoinType(j, innerJoin, includeSubclasses, treatAsDeclarations);
        if (joinType != null && joinType != JoinType.NONE) {
            join.addJoin(getSubclassTableName(j), generateTableAlias(name, j), idCols, getSubclassTableKeyColumns(j), joinType);
        }
    }
    return join;
}
Also used : JoinType(org.hibernate.sql.JoinType) JoinFragment(org.hibernate.sql.JoinFragment)

Example 7 with JoinType

use of org.hibernate.sql.JoinType in project hibernate-orm by hibernate.

the class CriteriaQueryTranslator method createAssociationPathCriteriaMap.

private void createAssociationPathCriteriaMap() {
    final Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
    while (iter.hasNext()) {
        CriteriaImpl.Subcriteria crit = iter.next();
        String wholeAssociationPath = getWholeAssociationPath(crit);
        Object old = associationPathCriteriaMap.put(wholeAssociationPath, crit);
        if (old != null) {
            throw new QueryException("duplicate association path: " + wholeAssociationPath);
        }
        JoinType joinType = crit.getJoinType();
        old = associationPathJoinTypesMap.put(wholeAssociationPath, joinType);
        if (old != null) {
            // TODO : not so sure this is needed...
            throw new QueryException("duplicate association path: " + wholeAssociationPath);
        }
        if (crit.getWithClause() != null) {
            this.withClauseMap.put(wholeAssociationPath, crit.getWithClause());
        }
    }
}
Also used : QueryException(org.hibernate.QueryException) CriteriaImpl(org.hibernate.internal.CriteriaImpl) JoinType(org.hibernate.sql.JoinType)

Example 8 with JoinType

use of org.hibernate.sql.JoinType in project hibernate-orm by hibernate.

the class JoinWalker method walkCollectionTree.

/**
	 * For a collection role, return a list of associations to be fetched by outerjoin
	 */
private void walkCollectionTree(final QueryableCollection persister, final String alias, final PropertyPath path, final int currentDepth) throws MappingException {
    if (persister.isOneToMany()) {
        walkEntityTree((OuterJoinLoadable) persister.getElementPersister(), alias, path, currentDepth);
    } else {
        Type type = persister.getElementType();
        if (type.isAssociationType()) {
            // a many-to-many;
            // decrement currentDepth here to allow join across the association table
            // without exceeding MAX_FETCH_DEPTH (i.e. the "currentDepth - 1" bit)
            AssociationType associationType = (AssociationType) type;
            String[] aliasedLhsColumns = persister.getElementColumnNames(alias);
            String[] lhsColumns = persister.getElementColumnNames();
            // if the current depth is 0, the root thing being loaded is the
            // many-to-many collection itself.  Here, it is alright to use
            // an inner join...
            boolean useInnerJoin = currentDepth == 0;
            final JoinType joinType = getJoinType(associationType, persister.getFetchMode(), path, persister.getTableName(), lhsColumns, !useInnerJoin, currentDepth - 1, //operations which cascade as far as the collection also cascade to collection elements
            null);
            addAssociationToJoinTreeIfNecessary(associationType, aliasedLhsColumns, alias, path, currentDepth - 1, joinType);
        } else if (type.isComponentType()) {
            walkCompositeElementTree((CompositeType) type, persister.getElementColumnNames(), persister, alias, path, currentDepth);
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CompositeType(org.hibernate.type.CompositeType)

Example 9 with JoinType

use of org.hibernate.sql.JoinType in project hibernate-orm by hibernate.

the class HqlSqlWalker method createFromJoinElement.

@Override
protected void createFromJoinElement(AST path, AST alias, int joinType, AST fetchNode, AST propertyFetch, AST with) throws SemanticException {
    boolean fetch = fetchNode != null;
    if (fetch && isSubQuery()) {
        throw new QueryException("fetch not allowed in subquery from-elements");
    }
    // the incoming "path" can be either:
    //		1) an implicit join path (join p.address.city)
    // 		2) an entity-join (join com.acme.User)
    //
    // so make the proper interpretation here...
    final EntityPersister entityJoinReferencedPersister = resolveEntityJoinReferencedPersister(path);
    if (entityJoinReferencedPersister != null) {
        // `path` referenced an entity
        final EntityJoinFromElement join = createEntityJoin(entityJoinReferencedPersister, alias, joinType, propertyFetch, with);
        ((FromReferenceNode) path).setFromElement(join);
    } else {
        if (path.getType() != SqlTokenTypes.DOT) {
            throw new SemanticException("Path expected for join!");
        }
        DotNode dot = (DotNode) path;
        JoinType hibernateJoinType = JoinProcessor.toHibernateJoinType(joinType);
        // Tell the dot node about the join type.
        dot.setJoinType(hibernateJoinType);
        dot.setFetch(fetch);
        // Generate an explicit join for the root dot node.   The implied joins will be collected and passed up
        // to the root dot node.
        dot.resolve(true, false, alias == null ? null : alias.getText());
        final FromElement fromElement;
        if (dot.getDataType() != null && dot.getDataType().isComponentType()) {
            if (dot.getDataType().isAnyType()) {
                throw new SemanticException("An AnyType attribute cannot be join fetched");
            // ^^ because the discriminator (aka, the "meta columns") must be known to the SQL in
            // 		a non-parameterized way.
            }
            FromElementFactory factory = new FromElementFactory(getCurrentFromClause(), dot.getLhs().getFromElement(), dot.getPropertyPath(), alias == null ? null : alias.getText(), null, false);
            fromElement = factory.createComponentJoin((CompositeType) dot.getDataType());
        } else {
            fromElement = dot.getImpliedJoin();
            fromElement.setAllPropertyFetch(propertyFetch != null);
            if (with != null) {
                if (fetch) {
                    throw new SemanticException("with-clause not allowed on fetched associations; use filters");
                }
                handleWithFragment(fromElement, with);
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("createFromJoinElement() : " + getASTPrinter().showAsString(fromElement, "-- join tree --"));
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) FromReferenceNode(org.hibernate.hql.internal.ast.tree.FromReferenceNode) QueryException(org.hibernate.QueryException) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) DotNode(org.hibernate.hql.internal.ast.tree.DotNode) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) JoinType(org.hibernate.sql.JoinType) FromElementFactory(org.hibernate.hql.internal.ast.tree.FromElementFactory) SemanticException(antlr.SemanticException) CompositeType(org.hibernate.type.CompositeType)

Example 10 with JoinType

use of org.hibernate.sql.JoinType in project hibernate-orm by hibernate.

the class FromElementFactory method createElementJoin.

FromElement createElementJoin(QueryableCollection queryableCollection) throws SemanticException {
    FromElement elem;
    //TODO: always true for now, but not if we later decide to support elements() in the from clause
    implied = true;
    inElementsFunction = true;
    Type elementType = queryableCollection.getElementType();
    if (!elementType.isEntityType()) {
        throw new IllegalArgumentException("Cannot create element join for a collection of non-entities!");
    }
    this.queryableCollection = queryableCollection;
    SessionFactoryHelper sfh = fromClause.getSessionFactoryHelper();
    FromElement destination = null;
    String tableAlias = null;
    EntityPersister entityPersister = queryableCollection.getElementPersister();
    tableAlias = fromClause.getAliasGenerator().createName(entityPersister.getEntityName());
    String associatedEntityName = entityPersister.getEntityName();
    EntityPersister targetEntityPersister = sfh.requireClassPersister(associatedEntityName);
    // Create the FROM element for the target (the elements of the collection).
    destination = createAndAddFromElement(associatedEntityName, classAlias, targetEntityPersister, (EntityType) queryableCollection.getElementType(), tableAlias);
    // If the join is implied, then don't include sub-classes on the element.
    if (implied) {
        destination.setIncludeSubclasses(false);
    }
    fromClause.addCollectionJoinFromElementByPath(path, destination);
    //		origin.addDestination(destination);
    // Add the query spaces.
    fromClause.getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
    CollectionType type = queryableCollection.getCollectionType();
    String role = type.getRole();
    String roleAlias = origin.getTableAlias();
    String[] targetColumns = sfh.getCollectionElementColumns(role, roleAlias);
    AssociationType elementAssociationType = sfh.getElementAssociationType(type);
    // Create the join element under the from element.
    JoinType joinType = JoinType.INNER_JOIN;
    JoinSequence joinSequence = sfh.createJoinSequence(implied, elementAssociationType, tableAlias, joinType, targetColumns);
    elem = initializeJoin(path, destination, joinSequence, targetColumns, origin, false);
    // The associated entity is implied, but it must be included in the FROM.
    elem.setUseFromFragment(true);
    // The collection alias is the role.
    elem.setCollectionTableAlias(roleAlias);
    return elem;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) CollectionType(org.hibernate.type.CollectionType) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) JoinType(org.hibernate.sql.JoinType) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Aggregations

JoinType (org.hibernate.sql.JoinType)11 CompositeType (org.hibernate.type.CompositeType)6 AssociationType (org.hibernate.type.AssociationType)5 EntityType (org.hibernate.type.EntityType)5 Type (org.hibernate.type.Type)5 CollectionType (org.hibernate.type.CollectionType)3 SemanticException (antlr.SemanticException)2 QueryException (org.hibernate.QueryException)2 JoinSequence (org.hibernate.engine.internal.JoinSequence)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 FetchMode (org.hibernate.FetchMode)1 DotNode (org.hibernate.hql.internal.ast.tree.DotNode)1 EntityJoinFromElement (org.hibernate.hql.internal.ast.tree.EntityJoinFromElement)1 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)1 FromElementFactory (org.hibernate.hql.internal.ast.tree.FromElementFactory)1 FromReferenceNode (org.hibernate.hql.internal.ast.tree.FromReferenceNode)1 SessionFactoryHelper (org.hibernate.hql.internal.ast.util.SessionFactoryHelper)1 CriteriaImpl (org.hibernate.internal.CriteriaImpl)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 JoinFragment (org.hibernate.sql.JoinFragment)1