Search in sources :

Example 1 with OuterJoinableAssociation

use of org.hibernate.loader.OuterJoinableAssociation 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)

Aggregations

Iterator (java.util.Iterator)1 OuterJoinableAssociation (org.hibernate.loader.OuterJoinableAssociation)1 JoinFragment (org.hibernate.sql.JoinFragment)1 Select (org.hibernate.sql.Select)1 AssociationType (org.hibernate.type.AssociationType)1