&& rowSelection.definesLimits(); final boolean needsLimit = hasLimit && translators.length > 1; if ( needsLimit ) { LOG.needsLimit(); final RowSelection selection = new RowSelection(); selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() ); selection.setTimeout( queryParameters.getRowSelection().getTimeout() ); queryParametersToUse = queryParameters.createCopyUsing( selection ); if ( needsLimit ) { 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 ) ) {
public QueryParameters getQueryParameters() { final RowSelection selection = new RowSelection(); selection.setFirstRow( rootCriteria.getFirstResult() ); selection.setMaxRows( rootCriteria.getMaxResults() ); selection.setTimeout( rootCriteria.getTimeout() ); selection.setFetchSize( rootCriteria.getFetchSize() );
/** * If we're able to guess a likely size of the results we can optimize allocation * of our datastructures. * Essentially if we detect the user is not using pagination, we attempt to use the FetchSize * as a reasonable hint. If fetch size is not being set either, it is reasonable to expect * that we're going to have a single hit. In such a case it would be tempting to return a constant * of value one, but that's dangerous as it doesn't scale up appropriately for example * with an ArrayList if the guess is wrong. * * @param rowSelection * @return a reasonable size to use for allocation */ @SuppressWarnings("UnnecessaryUnboxing") private int guessResultSize(RowSelection rowSelection) { if ( rowSelection != null ) { final int maxReasonableAllocation = rowSelection.getFetchSize() != null ? rowSelection.getFetchSize().intValue() : 100; if ( rowSelection.getMaxRows() != null && rowSelection.getMaxRows().intValue() > 0 ) { return Math.min( maxReasonableAllocation, rowSelection.getMaxRows().intValue() ); } else if ( rowSelection.getFetchSize() != null && rowSelection.getFetchSize().intValue() > 0 ) { return rowSelection.getFetchSize().intValue(); } } return 7;//magic number guessed as a reasonable default. }
private void fillInNamedQueryBuilder(NamedQueryDefinitionBuilder builder, org.hibernate.query.Query query) { builder.setQuery( query.getQueryString() ) .setComment( query.getComment() ) .setCacheable( query.isCacheable() ) .setCacheRegion( query.getCacheRegion() ) .setCacheMode( query.getCacheMode() ) .setReadOnly( query.isReadOnly() ) .setFlushMode( query.getHibernateFlushMode() ); if ( query.getQueryOptions().getFirstRow() != null ) { builder.setFirstResult( query.getQueryOptions().getFirstRow() ); } if ( query.getQueryOptions().getMaxRows() != null ) { builder.setMaxResults( query.getQueryOptions().getMaxRows() ); } if ( query.getQueryOptions().getTimeout() != null ) { builder.setTimeout( query.getQueryOptions().getTimeout() ); } if ( query.getQueryOptions().getFetchSize() != null ) { builder.setFetchSize( query.getQueryOptions().getFetchSize() ); } }
final Integer maxRows; if ( selection != null ) { firstRow = selection.getFirstRow(); maxRows = selection.getMaxRows();
Serializable collectionKey = null; LockOptions lockOptions = new LockOptions(); RowSelection rowSelection = new RowSelection(); boolean readOnly = false; // TODO: readonly? boolean cacheable = false; // TODO: cacheable? if (rowSelection.getFirstRow() == null || rowSelection.getFirstRow() < 1) { rowSelection.setFirstRow(original.getFirstRow()); } else if (original.getFirstRow() != null && original.getFirstRow() > 0 && !original.getFirstRow().equals(rowSelection.getFirstRow())) { throw new IllegalStateException("Multiple row selections not allowed!"); if (rowSelection.getMaxRows() == null || rowSelection.getMaxRows() == Integer.MAX_VALUE) { rowSelection.setMaxRows(original.getMaxRows()); } else if (original.getMaxRows() != null && original.getMaxRows() != Integer.MAX_VALUE && !original.getMaxRows().equals(rowSelection.getMaxRows())) { throw new IllegalStateException("Multiple row selections not allowed!"); if (rowSelection.getFetchSize() == null) { rowSelection.setFetchSize(original.getFetchSize()); } else if (original.getFetchSize() != null && !original.getFetchSize().equals(rowSelection.getFetchSize())) { throw new IllegalStateException("Multiple row selections not allowed!"); if (rowSelection.getTimeout() == null) { rowSelection.setTimeout(original.getTimeout()); } else if (original.getTimeout() != null && !original.getTimeout().equals(rowSelection.getTimeout())) { throw new IllegalStateException("Multiple row selections not allowed!");
/** * Is a max row limit indicated? * * @param selection The row selection options * * @return Whether a max row limit was indicated */ public static boolean hasMaxRows(RowSelection selection) { return selection != null && selection.getMaxRows() != null && selection.getMaxRows() > 0; }
/** * Retrieve the indicated first row for pagination * * @param selection The row selection options * * @return The first row */ public static int getFirstRow(RowSelection selection) { return ( selection == null || selection.getFirstRow() == null ) ? 0 : selection.getFirstRow(); }
public JoinFetchUsage(QueryParameters queryParameters) { boolean collect = containsCollectionFetches(); boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits(); if (collect && hasLimit) { firstRow = queryParameters.getRowSelection().getFirstRow(); maxRows = queryParameters.getRowSelection().getMaxRows(); time = System.currentTimeMillis(); } }
@SuppressWarnings("unchecked") @Override public final List<Long> list( SessionImplementor session, QueryParameters queryParameters) throws HibernateException { boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits(); if (hasLimit) { RowSelection selection = new RowSelection(); selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() ); selection.setTimeout( queryParameters.getRowSelection().getTimeout() ); queryParameters = queryParameters.createCopyUsing(selection); } return super.list(session, queryParameters); }
private int selectRecordsByMaxAge(Session session, String tempTable, Date minValue, Dialect dialect) { // fill temporary table, we don't need to join task on object on // container, oid and id is already in task table StringBuilder selectSB = new StringBuilder(); selectSB.append("select a.id as id from ").append(RAuditEventRecord.TABLE_NAME).append(" a"); selectSB.append(" where a.").append(RAuditEventRecord.COLUMN_TIMESTAMP).append(" < ###TIME###"); String selectString = selectSB.toString(); // batch size RowSelection rowSelection = new RowSelection(); rowSelection.setMaxRows(CLEANUP_AUDIT_BATCH_SIZE); LimitHandler limitHandler = dialect.getLimitHandler(); selectString = limitHandler.processSql(selectString, rowSelection); // replace ? -> batch size, $ -> ? // Sorry for that .... I just don't know how to write this query in HQL, // nor I'm not sure if limiting max size in // compound insert into ... select ... query via query.setMaxSize() // would work - TODO write more nicely if anybody knows how) selectString = selectString.replace("?", String.valueOf(CLEANUP_AUDIT_BATCH_SIZE)); selectString = selectString.replace("###TIME###", "?"); String queryString = "insert into " + tempTable + " " + selectString; LOGGER.trace("Query string = {}", queryString); NativeQuery query = session.createNativeQuery(queryString); query.setParameter(1, new Timestamp(minValue.getTime())); return query.executeUpdate(); }
@Override public String processSql(String sql, RowSelection selection) { final boolean hasOffset = LimitHelper.hasFirstRow(selection); return sql + (hasOffset ? " limit ({?" + selection.getFetchSize() + "})" + " offset ({?" + selection.getFirstRow() + "})" : " limit ({?" + selection.getFetchSize() + "})"); }
protected void collectHints(Map<String, Object> hints) { if ( getQueryOptions().getTimeout() != null ) { hints.put( HINT_TIMEOUT, getQueryOptions().getTimeout() ); hints.put( SPEC_HINT_TIMEOUT, getQueryOptions().getTimeout() * 1000 ); putIfNotNull( hints, HINT_FETCH_SIZE, getQueryOptions().getFetchSize() ); putIfNotNull( hints, HINT_FLUSH_MODE, getHibernateFlushMode() );
RowSelection selection = new RowSelection(); selection.setFetchSize(basePage.getPageSize()); selection.setFirstRow(basePage.getBeginNumber()); return dialect.getLimitHandler().processSql(buf.toString(), selection);
/** * Set the maximum number of query results to be retrieved. A value less than * or equal to 0 is considered uninitialized, resulting in no limit on the number * of results. * * @param maxResults - the maximum number of query results * @return {@code this}, for method chaining * * @deprecated {@link #setMaxResults(int)} should be used instead. */ @Deprecated default Query setHibernateMaxResults(int maxResults) { // maxResults <= 0 is the same as uninitialized (with no limit), if ( maxResults <= 0 ) { // treat zero and negatives specifically as meaning no limit... getQueryOptions().setMaxRows( null ); } else { getQueryOptions().setMaxRows( maxResults ); } return this; }
public AbstractQueryImpl( String queryString, FlushMode flushMode, SessionImplementor session, ParameterMetadata parameterMetadata) { this.session = session; this.queryString = queryString; this.selection = new RowSelection(); this.flushMode = flushMode; this.cacheMode = null; this.parameterMetadata = parameterMetadata; }
@Override public Integer getTimeout() { return queryOptions.getTimeout(); }
@Override @SuppressWarnings("unchecked") public QueryImplementor setFetchSize(int fetchSize) { queryOptions.setFetchSize( fetchSize ); return this; }
/** * Coordinates the efforts to perform a scroll across all the included query translators. * * @param queryParameters The query parameters * @param session The session * * @return The query result iterator * * @throws HibernateException Indicates a problem performing the query */ public ScrollableResultsImplementor performScroll( QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException { if ( traceEnabled ) { LOG.tracev( "Iterate: {0}", getSourceQuery() ); queryParameters.traceParameters( session.getFactory() ); } if ( translators.length != 1 ) { throw new QueryException( "implicit polymorphism not supported for scroll() queries" ); } if ( queryParameters.getRowSelection().definesLimits() && translators[0].containsCollectionFetches() ) { throw new QueryException( "firstResult/maxResults not supported in conjunction with scroll() of a query containing collection fetches" ); } return translators[0].scroll( queryParameters, session ); }
@Override public Integer getFetchSize() { return queryOptions.getFetchSize(); }