Example usage for org.hibernate Criteria setFetchSize

List of usage examples for org.hibernate Criteria setFetchSize

Introduction

In this page you can find the example usage for org.hibernate Criteria setFetchSize.

Prototype

public Criteria setFetchSize(int fetchSize);

Source Link

Document

Set a fetch size for the underlying JDBC query.

Usage

From source file:org.jpos.qi.EntityContainer.java

License:Open Source License

@Override
public List<?> getItemIds(int startIndex, int numberOfItems) {
    try {//from  w  ww  . j a  v  a  2s  . c  o m
        return (List<?>) DB.exec((db) -> {
            Criteria crit = getCriteria(db);
            crit.setProjection(Projections.id());
            crit.setFirstResult(startIndex);
            crit.setFetchSize(numberOfItems);
            return crit.list();
        });
    } catch (Exception e) {
        QI.getQI().getLog().error(e);
        return null;
    }

}

From source file:org.onebusaway.transit_data_federation.impl.realtime.BlockLocationRecordDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from  w w  w .  java  2  s .co  m*/
public List<BlockLocationRecord> getBlockLocationRecords(final AgencyAndId blockId, final AgencyAndId tripId,
        final AgencyAndId vehicleId, final long serviceDate, final long fromTime, final long toTime,
        final int recordLimit) {

    return _template.executeFind(new HibernateCallback<List<BlockLocationRecord>>() {

        @Override
        public List<BlockLocationRecord> doInHibernate(Session session)
                throws HibernateException, SQLException {

            Criteria c = session.createCriteria(BlockLocationRecord.class);

            if (blockId != null)
                c.add(Property.forName("blockId").eq(blockId));
            if (tripId != null)
                c.add(Property.forName("tripId").eq(tripId));
            if (vehicleId != null)
                c.add(Property.forName("vehicleId").eq(vehicleId));
            if (serviceDate != 0)
                c.add(Property.forName("serviceDate").eq(serviceDate));
            if (fromTime != 0)
                c.add(Property.forName("time").ge(fromTime));
            if (toTime != 0)
                c.add(Property.forName("time").le(toTime));
            if (recordLimit != 0)
                c.setFetchSize(recordLimit);

            c.addOrder(Order.asc("time"));

            return c.list();
        }
    });
}

From source file:org.openmrs.module.openhmis.commons.api.entity.impl.BaseObjectDataServiceImpl.java

License:Open Source License

/**
 * Updates the specified {@link Criteria} object to retrieve the data specified by the {@link PagingInfo} object.
 * @param pagingInfo The {@link PagingInfo} object that specifies which data should be retrieved.
 * @param criteria The {@link Criteria} to add the paging settings to, or {@code null} to create a new one.
 * @return The {@link Criteria} object with the paging settings applied.
 *///  w w w.  j  a  v  a  2 s  .c om
protected Criteria createPagingCriteria(PagingInfo pagingInfo, Criteria criteria) {
    if (pagingInfo != null && pagingInfo.getPage() > 0 && pagingInfo.getPageSize() > 0) {
        if (criteria == null) {
            criteria = repository.createCriteria(getEntityClass());
        }

        criteria.setFirstResult((pagingInfo.getPage() - 1) * pagingInfo.getPageSize());
        criteria.setMaxResults(pagingInfo.getPageSize());
        criteria.setFetchSize(pagingInfo.getPageSize());
    }

    return criteria;
}

From source file:org.openspaces.persistency.hibernate.iterator.DefaultScrollableDataIterator.java

License:Open Source License

protected ScrollableResults createCursor() {
    session = sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setCacheable(false);/*from w  ww. ja  v a 2  s  .c  o  m*/
        criteria.setFlushMode(FlushMode.MANUAL);
        criteria.setFetchSize(fetchSize);
        if (perfromOrderById) {
            ClassMetadata metadata = sessionFactory.getClassMetadata(entityName);
            String idPropName = metadata.getIdentifierPropertyName();
            if (idPropName != null) {
                criteria.addOrder(Order.asc(idPropName));
            }
        }
        if (from >= 0) {
            if (from > 0)
                criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.scroll(ScrollMode.FORWARD_ONLY);
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else if (hQuery != null) {
        Query query = session.createQuery(hQuery);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        query.setCacheMode(CacheMode.IGNORE);
        query.setCacheable(false);
        query.setReadOnly(true);
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else {
        throw new IllegalStateException("No SQLQuery, entity, or Hibernate Query provided");
    }
}

From source file:org.openspaces.persistency.hibernate.iterator.StatelessScrollableDataIterator.java

License:Open Source License

protected ScrollableResults createCursor() {
    session = sessionFactory.openStatelessSession();
    transaction = session.beginTransaction();
    if (entityName != null) {
        Criteria criteria = session.createCriteria(entityName);
        criteria.setFetchSize(fetchSize);
        if (perfromOrderById) {
            ClassMetadata metadata = sessionFactory.getClassMetadata(entityName);
            String idPropName = metadata.getIdentifierPropertyName();
            if (idPropName != null) {
                criteria.addOrder(Order.asc(idPropName));
            }//  w ww  . ja  v a2  s .  co  m
        }
        if (from >= 0) {
            if (from > 0)
                criteria.setFirstResult(from);
            criteria.setMaxResults(size);
        }
        return criteria.scroll(ScrollMode.FORWARD_ONLY);
    } else if (sqlQuery != null) {
        Query query = HibernateIteratorUtils.createQueryFromSQLQuery(sqlQuery, session);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else if (hQuery != null) {
        Query query = session.createQuery(hQuery);
        query.setFetchSize(fetchSize);
        if (from >= 0) {
            if (from > 0)
                query.setFirstResult(from);
            query.setMaxResults(size);
        }
        query.setReadOnly(true);
        return query.scroll(ScrollMode.FORWARD_ONLY);
    } else {
        throw new IllegalStateException("Either SQLQuery or entity must be provided");
    }
}

From source file:org.springframework.orm.hibernate3.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.//from  w w  w.j  av a  2  s  .c o  m
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareCriteria(Criteria criteria) {
    if (isCacheQueries()) {
        criteria.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            criteria.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        criteria.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        criteria.setMaxResults(getMaxResults());
    }
    SessionFactoryUtils.applyTransactionTimeout(criteria, getSessionFactory());
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testExecuteWithFetchSizeAndMaxResults() throws HibernateException {
    Query query1 = mock(Query.class);
    Query query2 = mock(Query.class);
    Criteria criteria = mock(Criteria.class);

    given(session.createQuery("some query")).willReturn(query1);
    given(query1.setFetchSize(10)).willReturn(query1);
    given(query1.setMaxResults(20)).willReturn(query1);
    given(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setFetchSize(10)).willReturn(query2);
    given(query2.setMaxResults(20)).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setFetchSize(10)).willReturn(criteria);
    given(criteria.setMaxResults(20)).willReturn(criteria);

    hibernateTemplate.setFetchSize(10);// w w  w  .ja  v  a2  s. com
    hibernateTemplate.setMaxResults(20);
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override
        public Object doInHibernate(org.hibernate.Session sess) throws HibernateException {
            sess.createQuery("some query");
            sess.getNamedQuery("some query name");
            sess.createCriteria(TestBean.class);
            return null;
        }
    });

    verify(session).flush();
    verify(session).close();
}

From source file:org.springframework.orm.hibernate4.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout./*  w ww .  j av  a 2 s  .  com*/
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
    if (isCacheQueries()) {
        criteria.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            criteria.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        criteria.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        criteria.setMaxResults(getMaxResults());
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(getSessionFactory());
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

From source file:org.springframework.orm.hibernate4.HibernateTemplateTests.java

License:Apache License

@Test
public void testExecuteWithFetchSizeAndMaxResults() {
    Query query1 = mock(Query.class);
    Query query2 = mock(Query.class);
    Criteria criteria = mock(Criteria.class);

    given(session.createQuery("some query")).willReturn(query1);
    given(query1.setFetchSize(10)).willReturn(query1);
    given(query1.setMaxResults(20)).willReturn(query1);
    given(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setFetchSize(10)).willReturn(query2);
    given(query2.setMaxResults(20)).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setFetchSize(10)).willReturn(criteria);
    given(criteria.setMaxResults(20)).willReturn(criteria);

    hibernateTemplate.setFetchSize(10);//from w ww.  ja  v a2  s.co  m
    hibernateTemplate.setMaxResults(20);
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override
        public Object doInHibernate(Session sess) {
            sess.createQuery("some query");
            sess.getNamedQuery("some query name");
            sess.createCriteria(TestBean.class);
            return null;
        }
    });
}

From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4DaoSupport.java

License:Apache License

/**
 * @description ??Criteria//ww  w .j a v  a 2s.  c  o m
 * @author <a href="mailto:code727@gmail.com">?</a> 
 * @param criteria
 */
protected void prepareCriteria(Criteria criteria) {
    if (this.cacheConfiguration != null) {
        if (this.cacheConfiguration.isCacheQueries()) {
            criteria.setCacheable(true);
            if (StringUtils.isNotBlank(this.cacheConfiguration.getQueryCacheRegion())) {
                criteria.setCacheRegion(this.cacheConfiguration.getQueryCacheRegion());
            }
        }

        if (this.cacheConfiguration.getFetchSize() > 0)
            criteria.setFetchSize(this.cacheConfiguration.getFetchSize());

        if (this.cacheConfiguration.getMaxResults() > 0)
            criteria.setMaxResults(this.cacheConfiguration.getMaxResults());
    }
}