Search in sources :

Example 16 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class JoinWalker method orderBy.

/**
	 * Get the order by string required for collection fetching
	 */
protected static String orderBy(List associations) throws MappingException {
    StringBuilder buf = new StringBuilder();
    Iterator iter = associations.iterator();
    OuterJoinableAssociation last = null;
    while (iter.hasNext()) {
        OuterJoinableAssociation oj = (OuterJoinableAssociation) iter.next();
        if (oj.getJoinType() == JoinType.LEFT_OUTER_JOIN) {
            // why does this matter?
            if (oj.getJoinable().isCollection()) {
                final QueryableCollection queryableCollection = (QueryableCollection) oj.getJoinable();
                if (queryableCollection.hasOrdering()) {
                    final String orderByString = queryableCollection.getSQLOrderByString(oj.getRHSAlias());
                    buf.append(orderByString).append(", ");
                }
            } else {
                // many-to-many defined order-by...
                if (last != null && last.getJoinable().isCollection()) {
                    final QueryableCollection queryableCollection = (QueryableCollection) last.getJoinable();
                    if (queryableCollection.isManyToMany() && last.isManyToManyWith(oj)) {
                        if (queryableCollection.hasManyToManyOrdering()) {
                            final String orderByString = queryableCollection.getManyToManyOrderByString(oj.getRHSAlias());
                            buf.append(orderByString).append(", ");
                        }
                    }
                }
            }
        }
        last = oj;
    }
    if (buf.length() > 0) {
        buf.setLength(buf.length() - 2);
    }
    return buf.toString();
}
Also used : Iterator(java.util.Iterator) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 17 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class JoinWalker method initPersisters.

protected void initPersisters(final List associations, final LockOptions lockOptions, final AssociationInitCallback callback) throws MappingException {
    final int joins = countEntityPersisters(associations);
    final int collections = countCollectionPersisters(associations);
    collectionOwners = collections == 0 ? null : new int[collections];
    collectionPersisters = collections == 0 ? null : new CollectionPersister[collections];
    collectionSuffixes = BasicLoader.generateSuffixes(joins + 1, collections);
    this.lockOptions = lockOptions;
    persisters = new Loadable[joins];
    aliases = new String[joins];
    owners = new int[joins];
    ownerAssociationTypes = new EntityType[joins];
    lockModeArray = ArrayHelper.fillArray(lockOptions.getLockMode(), joins);
    int i = 0;
    int j = 0;
    for (Object association : associations) {
        final OuterJoinableAssociation oj = (OuterJoinableAssociation) association;
        if (!oj.isCollection()) {
            persisters[i] = (Loadable) oj.getJoinable();
            aliases[i] = oj.getRHSAlias();
            owners[i] = oj.getOwner(associations);
            ownerAssociationTypes[i] = (EntityType) oj.getJoinableType();
            callback.associationProcessed(oj, i);
            i++;
        } else {
            QueryableCollection collPersister = (QueryableCollection) oj.getJoinable();
            if (oj.getJoinType() == JoinType.LEFT_OUTER_JOIN && !oj.hasRestriction()) {
                //it must be a collection fetch
                collectionPersisters[j] = collPersister;
                collectionOwners[j] = oj.getOwner(associations);
                j++;
            }
            if (collPersister.isOneToMany()) {
                persisters[i] = (Loadable) collPersister.getElementPersister();
                aliases[i] = oj.getRHSAlias();
                callback.associationProcessed(oj, i);
                i++;
            }
        }
    }
    if (ArrayHelper.isAllNegative(owners)) {
        owners = null;
    }
    if (collectionOwners != null && ArrayHelper.isAllNegative(collectionOwners)) {
        collectionOwners = null;
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 18 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class SizeExpression method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
    final String role = entityName + '.' + criteriaQuery.getPropertyName(propertyName);
    final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role);
    final String[] fk = cp.getKeyColumnNames();
    final String[] pk = ((Loadable) cp.getOwnerEntityPersister()).getIdentifierColumnNames();
    final ConditionFragment subQueryRestriction = new ConditionFragment().setTableAlias(criteriaQuery.getSQLAlias(criteria, propertyName)).setCondition(pk, fk);
    return String.format(Locale.ROOT, "? %s (select count(*) from %s where %s)", op, cp.getTableName(), subQueryRestriction.toFragmentString());
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 19 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class AbstractEmptinessExpression method toSqlString.

@Override
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
    final String actualPropertyName = criteriaQuery.getPropertyName(propertyName);
    final String sqlAlias = criteriaQuery.getSQLAlias(criteria, propertyName);
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final QueryableCollection collectionPersister = getQueryableCollection(entityName, actualPropertyName, factory);
    final String[] collectionKeys = collectionPersister.getKeyColumnNames();
    final String[] ownerKeys = ((Loadable) factory.getEntityPersister(entityName)).getIdentifierColumnNames();
    final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where " + new ConditionFragment().setTableAlias(sqlAlias).setCondition(ownerKeys, collectionKeys).toFragmentString() + ")";
    return excludeEmpty() ? "exists " + innerSelect : "not exists " + innerSelect;
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 20 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class PathExpressionParser method prepareForIndex.

private void prepareForIndex(QueryTranslatorImpl q) throws QueryException {
    QueryableCollection collPersister = q.getCollectionPersister(collectionRole);
    if (!collPersister.hasIndex()) {
        throw new QueryException("unindexed collection beforeQuery []: " + path);
    }
    String[] indexCols = collPersister.getIndexColumnNames();
    if (indexCols.length != 1) {
        throw new QueryException("composite-index appears in []: " + path);
    }
    //String[] keyCols = collPersister.getKeyColumnNames();
    JoinSequence fromJoins = new JoinSequence(q.getFactory()).setUseThetaStyle(useThetaStyleJoin).setRoot(collPersister, collectionName).setNext(joinSequence.copy());
    if (!continuation) {
        addJoin(collectionName, collPersister.getCollectionType());
    }
    //TODO: get SQL rendering out of here
    joinSequence.addCondition(collectionName + '.' + indexCols[0] + " = ");
    CollectionElement elem = new CollectionElement();
    elem.elementColumns = collPersister.getElementColumnNames(collectionName);
    elem.elementType = collPersister.getElementType();
    elem.isOneToMany = collPersister.isOneToMany();
    elem.alias = collectionName;
    elem.joinSequence = joinSequence;
    collectionElements.addLast(elem);
    setExpectingCollectionIndex();
    q.addCollection(collectionName, collectionRole);
    q.addFromJoinOnly(collectionName, fromJoins);
}
Also used : QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Aggregations

QueryableCollection (org.hibernate.persister.collection.QueryableCollection)33 Type (org.hibernate.type.Type)10 QueryException (org.hibernate.QueryException)9 AssociationType (org.hibernate.type.AssociationType)7 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 Joinable (org.hibernate.persister.entity.Joinable)6 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 HashMap (java.util.HashMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)4 EntityType (org.hibernate.type.EntityType)4 SemanticException (antlr.SemanticException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Map (java.util.Map)3 Session (org.hibernate.Session)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3