Java Code Examples for org.hibernate.engine.spi.QueryParameters#createCopyUsing()

The following examples show how to use org.hibernate.engine.spi.QueryParameters#createCopyUsing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactiveQueryTranslatorImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
public CompletionStage<List<Object>> reactiveList(SharedSessionContractImplementor session, QueryParameters queryParameters) throws HibernateException {
	// Delegate to the QueryLoader...
	errorIfDML();

	final QueryNode query = (QueryNode) getSqlAST();
	final boolean hasLimit =
			queryParameters.getRowSelection() != null
					&& queryParameters.getRowSelection().definesLimits();
	final boolean needsDistincting =
			( query.getSelectClause().isDistinct() || getEntityGraphQueryHint() != null || hasLimit )
					&& containsCollectionFetches();

	QueryParameters queryParametersToUse;
	if ( hasLimit && containsCollectionFetches() ) {
		boolean fail = session.getFactory().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 {
			LOG.firstOrMaxResultsSpecifiedWithCollectionFetch();
		}
		RowSelection selection = new RowSelection();
		selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
		selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
		queryParametersToUse = queryParameters.createCopyUsing( selection );
	}
	else {
		queryParametersToUse = queryParameters;
	}

	return queryLoader.reactiveList( (SessionImplementor) session, queryParametersToUse )
			.thenApply(results -> {
				if ( needsDistincting ) {
					int includedCount = -1;
					// NOTE : firstRow is zero-based
					int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null
							? 0
							: queryParameters.getRowSelection().getFirstRow();
					int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null
							? -1
							: queryParameters.getRowSelection().getMaxRows();
					List<Object> tmp = new ArrayList<>();
					IdentitySet distinction = new IdentitySet();
					for ( final Object result : results ) {
						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 results;
			});
}
 
Example 2
Source File: HQLQueryPlan.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Coordinates the efforts to perform a list across all the included query translators.
 *
 * @param queryParameters The query parameters
 * @param session The session
 *
 * @return The query result list
 *
 * @throws HibernateException Indicates a problem performing the query
 */
@SuppressWarnings("unchecked")
public List performList(
		QueryParameters queryParameters,
		SharedSessionContractImplementor session) throws HibernateException {
	if ( traceEnabled ) {
		LOG.tracev( "Find: {0}", getSourceQuery() );
		queryParameters.traceParameters( session.getFactory() );
	}

	final RowSelection rowSelection = queryParameters.getRowSelection();
	final boolean hasLimit = rowSelection != null
			&& rowSelection.definesLimits();
	final boolean needsLimit = hasLimit && translators.length > 1;

	final QueryParameters queryParametersToUse;
	if ( needsLimit ) {
		LOG.needsLimit();
		final RowSelection selection = new RowSelection();
		selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
		selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
		queryParametersToUse = queryParameters.createCopyUsing( selection );
	}
	else {
		queryParametersToUse = queryParameters;
	}

	//fast path to avoid unnecessary allocation and copying
	if ( translators.length == 1 ) {
		return translators[0].list( session, queryParametersToUse );
	}
	final int guessedResultSize = guessResultSize( rowSelection );
	final List combinedResults = new ArrayList( guessedResultSize );
	final IdentitySet distinction;
	if ( needsLimit ) {
		distinction = new IdentitySet( guessedResultSize );
	}
	else {
		distinction = null;
	}
	int includedCount = -1;
	translator_loop:
	for ( QueryTranslator translator : translators ) {
		final List tmp = translator.list( session, queryParametersToUse );
		if ( needsLimit ) {
			// NOTE : firstRow is zero-based
			final int first = queryParameters.getRowSelection().getFirstRow() == null
					? 0
					: queryParameters.getRowSelection().getFirstRow();
			final int max = queryParameters.getRowSelection().getMaxRows() == null
					? -1
					: queryParameters.getRowSelection().getMaxRows();
			for ( final Object result : tmp ) {
				if ( !distinction.add( result ) ) {
					continue;
				}
				includedCount++;
				if ( includedCount < first ) {
					continue;
				}
				combinedResults.add( result );
				if ( max >= 0 && includedCount > max ) {
					// break the outer loop !!!
					break translator_loop;
				}
			}
		}
		else {
			combinedResults.addAll( tmp );
		}
	}
	return combinedResults;
}
 
Example 3
Source File: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List list(SharedSessionContractImplementor session, QueryParameters queryParameters)
		throws HibernateException {
	// Delegate to the QueryLoader...
	errorIfDML();

	final QueryNode query = (QueryNode) sqlAst;
	final boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits();
	final boolean needsDistincting = (
			query.getSelectClause().isDistinct() ||
			getEntityGraphQueryHint() != null ||
			hasLimit )
	&& containsCollectionFetches();

	QueryParameters queryParametersToUse;
	if ( hasLimit && containsCollectionFetches() ) {
		boolean fail = session.getFactory().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 {
			LOG.firstOrMaxResultsSpecifiedWithCollectionFetch();
		}
		RowSelection selection = new RowSelection();
		selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
		selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
		queryParametersToUse = queryParameters.createCopyUsing( selection );
	}
	else {
		queryParametersToUse = queryParameters;
	}

	List results = queryLoader.list( session, queryParametersToUse );

	if ( needsDistincting ) {
		int includedCount = -1;
		// NOTE : firstRow is zero-based
		int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null
					? 0
					: queryParameters.getRowSelection().getFirstRow();
		int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null
					? -1
					: queryParameters.getRowSelection().getMaxRows();
		List tmp = new ArrayList();
		IdentitySet distinction = new IdentitySet();
		for ( final Object result : results ) {
			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;
			}
		}
		results = tmp;
	}

	return results;
}