Search in sources :

Example 16 with JoinSequence

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

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

Example 18 with JoinSequence

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

the class FromElementFactory method createEntityAssociation.

private FromElement createEntityAssociation(String role, String roleAlias, JoinType joinType) throws SemanticException {
    FromElement elem;
    Queryable entityPersister = (Queryable) queryableCollection.getElementPersister();
    String associatedEntityName = entityPersister.getEntityName();
    // Get the class name of the associated entity.
    if (queryableCollection.isOneToMany()) {
        LOG.debugf("createEntityAssociation() : One to many - path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
        JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
        elem = createJoin(associatedEntityName, roleAlias, joinSequence, (EntityType) queryableCollection.getElementType(), false);
    } else {
        LOG.debugf("createManyToMany() : path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
        elem = createManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType) queryableCollection.getElementType(), joinType);
        fromClause.getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());
    }
    elem.setCollectionTableAlias(roleAlias);
    return elem;
}
Also used : EntityType(org.hibernate.type.EntityType) Queryable(org.hibernate.persister.entity.Queryable) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 19 with JoinSequence

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

the class FromElementFactory method createCollection.

public FromElement createCollection(QueryableCollection queryableCollection, String role, JoinType joinType, boolean fetchFlag, boolean indexed) throws SemanticException {
    if (!collection) {
        throw new IllegalStateException("FromElementFactory not initialized for collections!");
    }
    this.inElementsFunction = indexed;
    FromElement elem;
    this.queryableCollection = queryableCollection;
    collectionType = queryableCollection.getCollectionType();
    String roleAlias = fromClause.getAliasGenerator().createName(role);
    // Correlated subqueries create 'special' implied from nodes
    // because correlated subselects can't use an ANSI-style join
    boolean explicitSubqueryFromElement = fromClause.isSubQuery() && !implied;
    if (explicitSubqueryFromElement) {
        String pathRoot = StringHelper.root(path);
        FromElement origin = fromClause.getFromElement(pathRoot);
        if (origin == null || origin.getFromClause() != fromClause) {
            implied = true;
        }
    }
    // super-duper-classic-parser-regression-testing-mojo-magic...
    if (explicitSubqueryFromElement && DotNode.useThetaStyleImplicitJoins) {
        implied = true;
    }
    Type elementType = queryableCollection.getElementType();
    if (elementType.isEntityType()) {
        // A collection of entities...
        elem = createEntityAssociation(role, roleAlias, joinType);
    } else if (elementType.isComponentType()) {
        // A collection of components...
        JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
        elem = createCollectionJoin(joinSequence, roleAlias);
    } else {
        // A collection of scalar elements...
        JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
        elem = createCollectionJoin(joinSequence, roleAlias);
    }
    elem.setRole(role);
    elem.setQueryableCollection(queryableCollection);
    // Don't include sub-classes for implied collection joins or subquery joins.
    if (implied) {
        elem.setIncludeSubclasses(false);
    }
    if (explicitSubqueryFromElement) {
        // Treat explict from elements in sub-queries properly.
        elem.setInProjectionList(true);
    }
    if (fetchFlag) {
        elem.setFetch(true);
    }
    return elem;
}
Also used : 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) 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