/** * Get a cursor over all of the results, with the forward-only flag set. * * @param fetchSize the fetch size to use retrieving the results * @param parameterValues the parameter values to use (or null if none). * * @return a forward-only {@link ScrollableResults} */ public ScrollableResults getForwardOnlyCursor(int fetchSize, Map<String, Object> parameterValues) { Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } return query.setFetchSize(fetchSize).scroll(ScrollMode.FORWARD_ONLY); }
/** * Read a page of data, clearing the existing session (if necessary) first, * and creating a new session before executing the query. * * @param page the page to read (starting at 0) * @param pageSize the size of the page or maximum number of items to read * @param fetchSize the fetch size to use * @param parameterValues the parameter values to use (if any, otherwise * null) * @return a collection of items */ public Collection<? extends T> readPage(int page, int pageSize, int fetchSize, Map<String, Object> parameterValues) { clear(); Query query = createQuery(); if (parameterValues != null) { query.setProperties(parameterValues); } @SuppressWarnings("unchecked") List<T> result = query.setFetchSize(fetchSize).setFirstResult(page * pageSize).setMaxResults(pageSize).list(); return result; }
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @param queryObject the Query object to prepare * @see #setCacheQueries * @see #setQueryCacheRegion */ @SuppressWarnings({"rawtypes", "deprecation"}) protected void prepareQuery(org.hibernate.Query queryObject) { if (isCacheQueries()) { queryObject.setCacheable(true); if (getQueryCacheRegion() != null) { queryObject.setCacheRegion(getQueryCacheRegion()); } } if (getFetchSize() > 0) { queryObject.setFetchSize(getFetchSize()); } if (getMaxResults() > 0) { queryObject.setMaxResults(getMaxResults()); } ResourceHolderSupport sessionHolder = (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory()); if (sessionHolder != null && sessionHolder.hasTimeout()) { queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @param queryObject the Query object to prepare * @see #setCacheQueries * @see #setQueryCacheRegion */ @SuppressWarnings({"rawtypes", "deprecation"}) protected void prepareQuery(org.hibernate.Query queryObject) { if (isCacheQueries()) { queryObject.setCacheable(true); if (getQueryCacheRegion() != null) { queryObject.setCacheRegion(getQueryCacheRegion()); } } if (getFetchSize() > 0) { queryObject.setFetchSize(getFetchSize()); } if (getMaxResults() > 0) { queryObject.setMaxResults(getMaxResults()); } ResourceHolderSupport sessionHolder = (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory()); if (sessionHolder != null && sessionHolder.hasTimeout()) { queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }
Query query = session.createQuery(query); query.setReadOnly(true); // MIN_VALUE gives hint to JDBC driver to stream results query.setFetchSize(Integer.MIN_VALUE); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); // iterate over results while (results.next()) { Object row = results.get(); // process row then release reference // you may need to evict() as well } results.close();
public TypedQuery<T> setFetchSize(int fetchSize) { query.setFetchSize(fetchSize); return this; }
@Override public Query setFetchSize(int i) { return query.setFetchSize(i); }
@Override protected void applyFetchSize(int fetchSize) { query.setFetchSize( fetchSize ); }
@Override public Query setFetchSize(final int fetchSize) { return query.setFetchSize(fetchSize); }
@Override public List<ThesaurusConcept> getConceptsWoNotes(String idThesaurus, int startIndex, int limit) { Query query = getCurrentSession().createSQLQuery("select c.* " + "from thesaurus_concept c " + "left join note n on c.identifier = n.conceptid " + "where c.thesaurusid=:pthesaurusid" + " and n.identifier is null").addEntity(ThesaurusConcept.class); query.setParameter("pthesaurusid", idThesaurus); query.setFirstResult(startIndex); query.setFetchSize(limit); query.setMaxResults(limit); return (List<ThesaurusConcept>) query.list(); }
Query q = session.createCriteria(... no offset or limit ...); q.setCacheMode(CacheMode.IGNORE); q.setFetchSize(1000); // experiment with this to optimize performance vs. memory ScrollableResults iterator = query.scroll(ScrollMode.FORWARD_ONLY); while (iterator.next()) { Product p = (Product)iterator.get(); ... // session.evict(p); // an alternative to setting the cache mode above }
Query query = session.createQuery(query); query.setReadOnly(true); // MIN_VALUE gives hint to JDBC driver to stream results query.setFetchSize(Integer.MIN_VALUE); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); // iterate over results while (results.next()) { Object row = results.get(); // process row then release reference // you may need to evict() as well } results.close();
Query query = session.createQuery(query); query.setReadOnly(true); // MIN_VALUE gives hint to JDBC driver to stream results query.setFetchSize(Integer.MIN_VALUE); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); // iterate over results while (results.next()) { Object row = results.get(); // process row then release reference // you may need to flush() as well } results.close();
Query query = session.createQuery(query); query.setReadOnly(true); query.setFetchSize(Integer.MIN_VALUE); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); // iterate over results while (results.next()) { Object row = results.get(); // process row then release reference // you may need to evict() as well } results.close();
Query query = session.createQuery(query); query.setReadOnly(true); query.setFetchSize(50); ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY); // iterate over results while (results.next()) { Object row = results.get(); // process row then release reference // you may need to flush() as well } results.close();
private void initQuery(Query query, NamedQueryDefinition nqd) { query.setCacheable( nqd.isCacheable() ); query.setCacheRegion( nqd.getCacheRegion() ); if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() ); if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() ); if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() ); query.setReadOnly( nqd.isReadOnly() ); if ( nqd.getComment() != null ) query.setComment( nqd.getComment() ); }
private void initQuery(Query query, NamedQueryDefinition nqd) { query.setCacheable( nqd.isCacheable() ); query.setCacheRegion( nqd.getCacheRegion() ); if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() ); if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() ); if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() ); query.setReadOnly( nqd.isReadOnly() ); if ( nqd.getComment() != null ) query.setComment( nqd.getComment() ); }
private void initQuery(Query query, NamedQueryDefinition nqd) { query.setCacheable( nqd.isCacheable() ); query.setCacheRegion( nqd.getCacheRegion() ); if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() ); if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() ); if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() ); query.setReadOnly( nqd.isReadOnly() ); if ( nqd.getComment() != null ) query.setComment( nqd.getComment() ); }
protected org.hibernate.Query createQuery() { parseEjbql(); evaluateAllParameters(); org.hibernate.Query query = getSession().createQuery( getRenderedEjbql() ); setParameters( query, getQueryParameterValues(), 0 ); setParameters( query, getRestrictionParameterValues(), getQueryParameterValues().size() ); if ( getFirstResult()!=null) query.setFirstResult( getFirstResult() ); if ( getMaxResults()!=null) query.setMaxResults( getMaxResults()+1 ); //add one, so we can tell if there is another page if ( getCacheable()!=null ) query.setCacheable( getCacheable() ); if ( getCacheRegion()!=null ) query.setCacheRegion( getCacheRegion() ); if ( getFetchSize()!=null ) query.setFetchSize( getFetchSize() ); return query; }
private void prepareQuery(Query queryObject) { if(getHibernateTemplate().isCacheQueries()) { queryObject.setCacheable(true); if(getHibernateTemplate().getQueryCacheRegion() != null) { queryObject.setCacheRegion(getHibernateTemplate().getQueryCacheRegion()); } } if(getHibernateTemplate().getFetchSize() > 0) { queryObject.setFetchSize(getHibernateTemplate().getFetchSize()); } if(getHibernateTemplate().getMaxResults() > 0) { queryObject.setMaxResults(getHibernateTemplate().getMaxResults()); } }