Search in sources :

Example 6 with JoinFragment

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

the class AbstractEntityPersister method renderSelect.

protected String renderSelect(final int[] tableNumbers, final int[] columnNumbers, final int[] formulaNumbers) {
    //get 'em in the right order (not that it really matters)
    Arrays.sort(tableNumbers);
    //render the where and from parts
    int drivingTable = tableNumbers[0];
    final String drivingAlias = generateTableAlias(getRootAlias(), drivingTable);
    //we *could* regerate this inside each called method!
    final String where = createWhereByKey(drivingTable, drivingAlias);
    final String from = createFrom(drivingTable, drivingAlias);
    //now render the joins
    JoinFragment jf = createJoin(tableNumbers, drivingAlias);
    //now render the select clause
    SelectFragment selectFragment = createSelect(columnNumbers, formulaNumbers);
    //now tie it all together
    Select select = new Select(getFactory().getDialect());
    select.setSelectClause(selectFragment.toFragmentString().substring(2));
    select.setFromClause(from);
    select.setWhereClause(where);
    select.setOuterJoins(jf.toFromFragmentString(), jf.toWhereFragmentString());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("sequential select " + getEntityName());
    }
    return select.toStatementString();
}
Also used : SelectFragment(org.hibernate.sql.SelectFragment) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Example 7 with JoinFragment

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

the class OneToManyJoinWalker method initStatementString.

private void initStatementString(final OuterJoinLoadable elementPersister, final String alias, final int batchSize, final String subquery) throws MappingException {
    final int joins = countEntityPersisters(associations);
    suffixes = BasicLoader.generateSuffixes(joins + 1);
    final int collectionJoins = countCollectionPersisters(associations) + 1;
    collectionSuffixes = BasicLoader.generateSuffixes(joins + 1, collectionJoins);
    StringBuilder whereString = whereString(alias, oneToManyPersister.getKeyColumnNames(), subquery, batchSize);
    String filter = oneToManyPersister.filterFragment(alias, getLoadQueryInfluencers().getEnabledFilters());
    whereString.insert(0, StringHelper.moveAndToBeginning(filter));
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setSelectClause(oneToManyPersister.selectFragment(null, null, alias, suffixes[joins], collectionSuffixes[0], true) + selectString(associations)).setFromClause(elementPersister.fromTableFragment(alias) + elementPersister.fromJoinFragment(alias, true, true)).setWhereClause(whereString.toString()).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString() + elementPersister.whereJoinFragment(alias, true, true));
    select.setOrderByClause(orderBy(associations, oneToManyPersister.getSQLOrderByString(alias)));
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("load one-to-many " + oneToManyPersister.getRole());
    }
    sql = select.toStatementString();
}
Also used : Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Example 8 with JoinFragment

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

the class JoinProcessor method addJoinNodes.

private void addJoinNodes(QueryNode query, JoinSequence join, FromElement fromElement) {
    JoinFragment joinFragment = join.toJoinFragment(walker.getEnabledFilters(), fromElement.useFromFragment() || fromElement.isDereferencedBySuperclassOrSubclassProperty(), fromElement.getWithClauseFragment());
    String frag = joinFragment.toFromFragmentString();
    String whereFrag = joinFragment.toWhereFragmentString();
    // to FROM_FRAGMENT
    if (fromElement.getType() == JOIN_FRAGMENT && (join.isThetaStyle() || StringHelper.isNotEmpty(whereFrag))) {
        fromElement.setType(FROM_FRAGMENT);
        // this is used during SqlGenerator processing
        fromElement.getJoinSequence().setUseThetaStyle(true);
    }
    // If there is a FROM fragment and the FROM element is an explicit, then add the from part.
    if (fromElement.useFromFragment() || (fromElement.getFromClause().isSubQuery() && fromElement.isDereferencedBySuperclassOrSubclassProperty())) /*&& StringHelper.isNotEmpty( frag )*/
    {
        String fromFragment = processFromFragment(frag, join).trim();
        LOG.debugf("Using FROM fragment [%s]", fromFragment);
        processDynamicFilterParameters(fromFragment, fromElement, walker);
    }
    syntheticAndFactory.addWhereFragment(joinFragment, whereFrag, query, fromElement, walker);
}
Also used : JoinFragment(org.hibernate.sql.JoinFragment)

Example 9 with JoinFragment

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

the class AbstractEntityPersister method createJoin.

protected JoinFragment createJoin(int[] tableNumbers, String drivingAlias) {
    final String[] keyCols = StringHelper.qualify(drivingAlias, getSubclassTableKeyColumns(tableNumbers[0]));
    final JoinFragment jf = getFactory().getDialect().createOuterJoinFragment();
    // IMPL NOTE : notice that we skip the first table; it is the driving table!
    for (int i = 1; i < tableNumbers.length; i++) {
        final int j = tableNumbers[i];
        jf.addJoin(getSubclassTableName(j), generateTableAlias(getRootAlias(), j), keyCols, getSubclassTableKeyColumns(j), isInverseSubclassTable(j) || isNullableSubclassTable(j) ? JoinType.LEFT_OUTER_JOIN : JoinType.INNER_JOIN);
    }
    return jf;
}
Also used : 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