Search in sources :

Example 1 with JoinFragment

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

the class AbstractEntityJoinWalker method initStatementString.

private void initStatementString(final String projection, final String condition, final String orderBy, final String groupBy, final LockOptions lockOptions) throws MappingException {
    final int joins = countEntityPersisters(associations);
    suffixes = BasicLoader.generateSuffixes(joins + 1);
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setLockOptions(lockOptions).setSelectClause(projection == null ? persister.selectFragment(alias, suffixes[joins]) + selectString(associations) : projection).setFromClause(getDialect().appendLockHint(lockOptions, persister.fromTableFragment(alias)) + persister.fromJoinFragment(alias, true, true)).setWhereClause(condition).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString() + getWhereFragment()).setOrderByClause(orderBy(associations, orderBy)).setGroupByClause(groupBy);
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment(getComment());
    }
    sql = select.toStatementString();
}
Also used : Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Example 2 with JoinFragment

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

the class JoinWalker method mergeOuterJoins.

/**
	 * Generate a sequence of <tt>LEFT OUTER JOIN</tt> clauses for the given associations.
	 */
protected final JoinFragment mergeOuterJoins(List associations) throws MappingException {
    JoinFragment outerjoin = getDialect().createOuterJoinFragment();
    Iterator iter = associations.iterator();
    OuterJoinableAssociation last = null;
    while (iter.hasNext()) {
        final OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
        if (last != null && last.isManyToManyWith(oj)) {
            oj.addManyToManyJoin(outerjoin, (QueryableCollection) last.getJoinable());
        } else {
            oj.addJoins(outerjoin);
        }
        last = oj;
    }
    last = null;
    return outerjoin;
}
Also used : Iterator(java.util.Iterator) JoinFragment(org.hibernate.sql.JoinFragment)

Example 3 with JoinFragment

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

the class BasicCollectionJoinWalker method initStatementString.

private void initStatementString(final String alias, final int batchSize, final String subquery) throws MappingException {
    final int joins = countEntityPersisters(associations);
    final int collectionJoins = countCollectionPersisters(associations) + 1;
    suffixes = BasicLoader.generateSuffixes(joins);
    collectionSuffixes = BasicLoader.generateSuffixes(joins, collectionJoins);
    StringBuilder whereString = whereString(alias, collectionPersister.getKeyColumnNames(), subquery, batchSize);
    String manyToManyOrderBy = "";
    String filter = collectionPersister.filterFragment(alias, getLoadQueryInfluencers().getEnabledFilters());
    if (collectionPersister.isManyToMany()) {
        // from the collection of associations, locate OJA for the
        // ManyToOne corresponding to this persister to fully
        // define the many-to-many; we need that OJA so that we can
        // use its alias here
        // TODO : is there a better way here?
        Iterator itr = associations.iterator();
        AssociationType associationType = (AssociationType) collectionPersister.getElementType();
        while (itr.hasNext()) {
            OuterJoinableAssociation oja = (OuterJoinableAssociation) itr.next();
            if (oja.getJoinableType() == associationType) {
                // we found it
                filter += collectionPersister.getManyToManyFilterFragment(oja.getRHSAlias(), getLoadQueryInfluencers().getEnabledFilters());
                manyToManyOrderBy += collectionPersister.getManyToManyOrderByString(oja.getRHSAlias());
            }
        }
    }
    whereString.insert(0, StringHelper.moveAndToBeginning(filter));
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setSelectClause(collectionPersister.selectFragment(alias, collectionSuffixes[0]) + selectString(associations)).setFromClause(collectionPersister.getTableName(), alias).setWhereClause(whereString.toString()).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString());
    select.setOrderByClause(orderBy(associations, mergeOrderings(collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy)));
    if (getFactory().getSettings().isCommentsEnabled()) {
        select.setComment("load collection " + collectionPersister.getRole());
    }
    sql = select.toStatementString();
}
Also used : AssociationType(org.hibernate.type.AssociationType) Iterator(java.util.Iterator) Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment) OuterJoinableAssociation(org.hibernate.loader.OuterJoinableAssociation)

Example 4 with JoinFragment

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

the class LoadQueryJoinAndFetchProcessor method processQuerySpaceJoins.

public void processQuerySpaceJoins(QuerySpace querySpace, SelectStatementBuilder selectStatementBuilder) {
    LOG.debug("processing queryspace " + querySpace.getUid());
    final JoinFragment joinFragment = factory.getDialect().createOuterJoinFragment();
    processQuerySpaceJoins(querySpace, joinFragment);
    selectStatementBuilder.setOuterJoins(joinFragment.toFromFragmentString(), joinFragment.toWhereFragmentString());
}
Also used : JoinFragment(org.hibernate.sql.JoinFragment)

Example 5 with JoinFragment

use of org.hibernate.sql.JoinFragment 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)

Aggregations

JoinFragment (org.hibernate.sql.JoinFragment)9 Select (org.hibernate.sql.Select)4 Iterator (java.util.Iterator)2 OuterJoinableAssociation (org.hibernate.loader.OuterJoinableAssociation)1 JoinType (org.hibernate.sql.JoinType)1 SelectFragment (org.hibernate.sql.SelectFragment)1 SimpleSelect (org.hibernate.sql.SimpleSelect)1 AssociationType (org.hibernate.type.AssociationType)1