Search in sources :

Example 1 with MutableQueryOptions

use of org.hibernate.query.spi.MutableQueryOptions in project hibernate-orm by hibernate.

the class QuerySqmImpl method doList.

protected List<R> doList() {
    verifySelect();
    getSession().prepareForQueryExecution(requiresTxn(getQueryOptions().getLockOptions().findGreatestLockMode()));
    final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
    final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
    final boolean hasLimit = hasLimit(sqmStatement, getQueryOptions());
    final boolean needsDistinct = containsCollectionFetches && (sqmStatement.usesDistinct() || hasAppliedGraph(getQueryOptions()) || hasLimit);
    final DomainQueryExecutionContext executionContextToUse;
    if (hasLimit && containsCollectionFetches) {
        boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
        if (fail) {
            throw new HibernateException("firstResult/maxResults specified with collection fetch. " + "In memory pagination was about to be applied. " + "Failing because 'Fail on pagination over collection fetch' is enabled.");
        } else {
            QueryLogging.QUERY_MESSAGE_LOGGER.firstOrMaxResultsSpecifiedWithCollectionFetch();
        }
        final MutableQueryOptions originalQueryOptions = getQueryOptions();
        final QueryOptions normalizedQueryOptions = omitSqlQueryOptions(originalQueryOptions, true, false);
        if (originalQueryOptions == normalizedQueryOptions) {
            executionContextToUse = this;
        } else {
            executionContextToUse = new DelegatingDomainQueryExecutionContext(this) {

                @Override
                public QueryOptions getQueryOptions() {
                    return normalizedQueryOptions;
                }
            };
        }
    } else {
        executionContextToUse = this;
    }
    final List<R> list = resolveSelectQueryPlan().performList(executionContextToUse);
    if (needsDistinct) {
        int includedCount = -1;
        // NOTE : firstRow is zero-based
        final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null ? getIntegerLiteral(sqmStatement.getOffset(), 0) : getQueryOptions().getLimit().getFirstRow();
        final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null ? getMaxRows(sqmStatement, list.size()) : getQueryOptions().getLimit().getMaxRows();
        final List<R> tmp = new ArrayList<>(list.size());
        final IdentitySet<Object> distinction = new IdentitySet<>(list.size());
        for (final R result : list) {
            if (!distinction.add(result)) {
                continue;
            }
            includedCount++;
            if (includedCount < first) {
                continue;
            }
            tmp.add(result);
            // NOTE : ( max - 1 ) because first is zero-based while max is not...
            if (max >= 0 && (includedCount - first) >= (max - 1)) {
                break;
            }
        }
        return tmp;
    }
    return list;
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) HibernateException(org.hibernate.HibernateException) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlOmittingQueryOptions.omitSqlQueryOptions(org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOptions) MutableQueryOptions(org.hibernate.query.spi.MutableQueryOptions) EntityGraphQueryHint(org.hibernate.engine.query.spi.EntityGraphQueryHint) DelegatingDomainQueryExecutionContext(org.hibernate.query.internal.DelegatingDomainQueryExecutionContext) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) MutableQueryOptions(org.hibernate.query.spi.MutableQueryOptions) DelegatingDomainQueryExecutionContext(org.hibernate.query.internal.DelegatingDomainQueryExecutionContext)

Example 2 with MutableQueryOptions

use of org.hibernate.query.spi.MutableQueryOptions in project hibernate-orm by hibernate.

the class SqmSelectionQueryImpl method doList.

protected List<R> doList() {
    getSession().prepareForQueryExecution(requiresTxn(getQueryOptions().getLockOptions().findGreatestLockMode()));
    final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) getSqmStatement();
    final boolean containsCollectionFetches = sqmStatement.containsCollectionFetches();
    final boolean hasLimit = hasLimit(sqmStatement, getQueryOptions());
    final boolean needsDistinct = containsCollectionFetches && (sqmStatement.usesDistinct() || hasAppliedGraph(getQueryOptions()) || hasLimit);
    final DomainQueryExecutionContext executionContextToUse;
    if (hasLimit && containsCollectionFetches) {
        boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled();
        if (fail) {
            throw new HibernateException("firstResult/maxResults specified with collection fetch. " + "In memory pagination was about to be applied. " + "Failing because 'Fail on pagination over collection fetch' is enabled.");
        } else {
            QueryLogging.QUERY_MESSAGE_LOGGER.firstOrMaxResultsSpecifiedWithCollectionFetch();
        }
        final MutableQueryOptions originalQueryOptions = getQueryOptions();
        final QueryOptions normalizedQueryOptions = omitSqlQueryOptions(originalQueryOptions, true, false);
        if (originalQueryOptions == normalizedQueryOptions) {
            executionContextToUse = this;
        } else {
            executionContextToUse = new DelegatingDomainQueryExecutionContext(this) {

                @Override
                public QueryOptions getQueryOptions() {
                    return normalizedQueryOptions;
                }
            };
        }
    } else {
        executionContextToUse = this;
    }
    final List<R> list = resolveQueryPlan().performList(executionContextToUse);
    if (needsDistinct) {
        int includedCount = -1;
        // NOTE : firstRow is zero-based
        final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null ? getIntegerLiteral(sqmStatement.getOffset(), 0) : getQueryOptions().getLimit().getFirstRow();
        final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null ? getMaxRows(sqmStatement, list.size()) : getQueryOptions().getLimit().getMaxRows();
        final List<R> tmp = new ArrayList<>(list.size());
        final IdentitySet<Object> distinction = new IdentitySet<>(list.size());
        for (final R result : list) {
            if (!distinction.add(result)) {
                continue;
            }
            includedCount++;
            if (includedCount < first) {
                continue;
            }
            tmp.add(result);
            // NOTE : ( max - 1 ) because first is zero-based while max is not...
            if (max >= 0 && (includedCount - first) >= (max - 1)) {
                break;
            }
        }
        return tmp;
    }
    return list;
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) HibernateException(org.hibernate.HibernateException) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) MutableQueryOptions(org.hibernate.query.spi.MutableQueryOptions) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlOmittingQueryOptions.omitSqlQueryOptions(org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOptions) DelegatingDomainQueryExecutionContext(org.hibernate.query.internal.DelegatingDomainQueryExecutionContext) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) MutableQueryOptions(org.hibernate.query.spi.MutableQueryOptions) DelegatingDomainQueryExecutionContext(org.hibernate.query.internal.DelegatingDomainQueryExecutionContext)

Aggregations

ArrayList (java.util.ArrayList)2 HibernateException (org.hibernate.HibernateException)2 IdentitySet (org.hibernate.internal.util.collections.IdentitySet)2 DelegatingDomainQueryExecutionContext (org.hibernate.query.internal.DelegatingDomainQueryExecutionContext)2 DomainQueryExecutionContext (org.hibernate.query.spi.DomainQueryExecutionContext)2 MutableQueryOptions (org.hibernate.query.spi.MutableQueryOptions)2 QueryOptions (org.hibernate.query.spi.QueryOptions)2 SqlOmittingQueryOptions.omitSqlQueryOptions (org.hibernate.query.spi.SqlOmittingQueryOptions.omitSqlQueryOptions)2 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)2 EntityGraphQueryHint (org.hibernate.engine.query.spi.EntityGraphQueryHint)1