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;
}
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;
}
Aggregations