Example usage for org.hibernate ScrollMode FORWARD_ONLY

List of usage examples for org.hibernate ScrollMode FORWARD_ONLY

Introduction

In this page you can find the example usage for org.hibernate ScrollMode FORWARD_ONLY.

Prototype

ScrollMode FORWARD_ONLY

To view the source code for org.hibernate ScrollMode FORWARD_ONLY.

Click Source Link

Document

Requests a scrollable result that is only scrollable forwards.

Usage

From source file:org.j2free.jpa.Controller.java

License:Apache License

/**
 * It is critical that batchSize matches the hibernate.search.worker.batch_size you set
 *
 * @param <T>/* w  ww  .  j  a va2 s  .  c  o m*/
 * @param entityClass
 * @param batchSize
 */
public <T> void hibernateSearchIndex(Class<T> entityClass, int batchSize) {
    FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(getSession());
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);

    ScrollableResults results = fullTextSession.createCriteria(entityClass).setFetchSize(batchSize)
            .scroll(ScrollMode.FORWARD_ONLY);

    try {
        int index = 0;
        while (results.next()) {
            index++;
            fullTextSession.index(results.get(0)); //index each element

            //clear every batchSize since the queue is processed
            if (index % batchSize == 0) {
                fullTextSession.flushToIndexes();
                fullTextSession.clear();
            }
        }
    } finally {
        results.close();
    }
}

From source file:org.jasig.ssp.dao.DirectoryPersonSearchDao.java

License:Apache License

public void exportableSearch(CaseloadCsvWriterHelper csvWriterHelper, PersonSearchRequest personSearchRequest)
        throws IOException {

    StatelessSession openStatelessSession = null;

    try {/*from w  w  w . j  ava  2 s  .c o  m*/
        openStatelessSession = sessionFactory.openStatelessSession();
        Pair<Long, Query> querySet = prepSearchQuery(openStatelessSession, personSearchRequest);

        querySet.getSecond().setResultTransformer(
                new NamespacedAliasToBeanResultTransformer(PersonSearchResult2.class, "person_"));
        Query query = querySet.getSecond().setFetchSize(10).setReadOnly(true);
        ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);

        csvWriterHelper.write(results, -1L);
    } finally {
        if (openStatelessSession != null) {
            try {
                openStatelessSession.close();
            } catch (Exception e) {
                // nothing to do and likely harmless
                LOGGER.info("Failed to close Hibernate StatelessSession", e);
            }
        }
    }

}

From source file:org.jboss.aerogear.unifiedpush.jpa.dao.impl.JPAInstallationDao.java

License:Apache License

@Override
public ResultsStream.QueryBuilder<String> findAllDeviceTokenForVariantIDByCriteria(String variantID,
        List<String> categories, List<String> aliases, List<String> deviceTypes, final int maxResults,
        String lastTokenFromPreviousBatch) {
    // the required part: Join + all tokens for variantID;

    final StringBuilder jpqlString = new StringBuilder(FIND_ALL_DEVICES_FOR_VARIANT_QUERY);
    final Map<String, Object> parameters = new LinkedHashMap<String, Object>();
    parameters.put("variantID", variantID);

    // apend query conditions based on specified message parameters
    appendDynamicQuery(jpqlString, parameters, categories, aliases, deviceTypes);

    // sort on ids so that we can handle paging properly
    if (lastTokenFromPreviousBatch != null) {
        jpqlString.append(" AND installation.deviceToken > :lastTokenFromPreviousBatch");
        parameters.put("lastTokenFromPreviousBatch", lastTokenFromPreviousBatch);
    }/*from  ww  w .  j  a va 2s  . com*/

    jpqlString.append(" ORDER BY installation.deviceToken ASC");

    return new ResultsStream.QueryBuilder<String>() {
        private Integer fetchSize = null;

        @Override
        public ResultsStream.QueryBuilder<String> fetchSize(int fetchSize) {
            this.fetchSize = fetchSize;
            return this;
        }

        @Override
        public ResultsStream<String> executeQuery() {
            Query hibernateQuery = JPAInstallationDao.this.createHibernateQuery(jpqlString.toString());
            hibernateQuery.setMaxResults(maxResults);
            for (Entry<String, Object> parameter : parameters.entrySet()) {
                Object value = parameter.getValue();
                if (value instanceof Collection<?>) {
                    hibernateQuery.setParameterList(parameter.getKey(), (Collection<?>) parameter.getValue());
                } else {
                    hibernateQuery.setParameter(parameter.getKey(), parameter.getValue());
                }

            }
            hibernateQuery.setReadOnly(true);
            if (fetchSize != null) {
                hibernateQuery.setFetchSize(fetchSize);
            }
            final ScrollableResults results = hibernateQuery.scroll(ScrollMode.FORWARD_ONLY);
            return new ResultsStream<String>() {
                @Override
                public boolean next() throws ResultStreamException {
                    return results.next();
                }

                @Override
                public String get() throws ResultStreamException {
                    return (String) results.get()[0];
                }
            };
        }

    };
}

From source file:org.jpos.gl.GLSession.java

License:Open Source License

private void deleteGLTransactions(Journal journal, Date start, Date end)
        throws HibernateException, GLException {
    boolean equalDate = start.equals(end);

    StringBuffer qs = new StringBuffer("from org.jpos.gl.GLTransaction where journal = :journal");
    if (equalDate) {
        qs.append(" and postDate = :date");
    } else {//from w  w  w  .  j av a2 s .c  om
        qs.append(" and postDate >= :start");
        qs.append(" and postDate <= :endDate");
    }
    Query q = session.createQuery(qs.toString());
    q.setLong("journal", journal.getId());
    if (equalDate)
        q.setParameter("date", start);
    else {
        q.setParameter("start", start);
        q.setParameter("endDate", end);
    }
    ScrollableResults sr = q.scroll(ScrollMode.FORWARD_ONLY);
    while (sr.next()) {
        session.delete(sr.get(0));
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

License:Open Source License

/**
 * /*  w ww .java2 s  .co  m*/
 * @throws PersistenceException
 */
public void generateIndex() throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Long total = (Long) entityManager.createQuery("SELECT count(entryId) FROM Document").getSingleResult();
        logger.info("Total Entities to be indexed : " + total);

        if (total > 0) {
            Transaction tx = fullTextSession.beginTransaction();
            fullTextSession.purgeAll(Document.class);
            tx.commit();
            logger.info("Removed all documents.");
            Integer numberOfElements = 100;

            Integer numberOfElementsBeforeGC = 1000;
            String queryJPA = "FROM Document ORDER BY entryId asc";

            org.hibernate.Query query = session.createQuery(queryJPA);
            tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Document document = (Document) results.get(0);
                fullTextSession.index(document);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    logger.info("Initiating Lucene Index Flush... ");
                    fullTextSession.flushToIndexes();
                    fullTextSession.clear();
                    logger.info("Finished the Lucene Index Flush...  Total records on index " + resultNumber
                            + "/" + total);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            fullTextSession.flushToIndexes();
            fullTextSession.clear();
            tx.commit();

            logger.info("Initiating Lucene Index Optimze...");
            SearchFactory searchFactory = fullTextSession.getSearchFactory();
            searchFactory.optimize(Document.class);
            logger.info("Finished Lucene Index Optimze");
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        if (fullTextSession != null) {
            fullTextSession.close();
        }
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

License:Open Source License

/**
 * /* w  w w.jav  a 2s. co  m*/
 * @throws PersistenceException
 */
@Override
public void updateIndex(Date fromDate) throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Query queryTotal = entityManager
                .createQuery("SELECT count(entryId) FROM Document where lastUpdate>=:lastUpdate");
        queryTotal.setParameter("lastUpdate", fromDate);
        Long total = (Long) queryTotal.getSingleResult();
        logger.info("Total Entities to be updated : " + total);

        if (total > 0) {
            Integer numberOfElements = 50;
            Integer numberOfElementsBeforeGC = 1000;
            org.hibernate.Query query = session.createQuery("FROM Document where lastUpdate>=:lastUpdate");
            query.setParameter("lastUpdate", fromDate);

            Transaction tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Document document = (Document) results.get(0);
                fullTextSession.delete(document);
                fullTextSession.index(document);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    flushingFullTextSession(total, resultNumber, fullTextSession);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            flushingFullTextSession(total, resultNumber, fullTextSession);

            /*            logger.info("Initiating Lucene Index Optimze...");
                          SearchFactory searchFactory = fullTextSession.getSearchFactory();
                          searchFactory.optimize(Document.class);
            */
            logger.info("Finished Lucene Index Optimze");

            tx.commit();
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        /*if (fullTextSession.isOpen()) {
           fullTextSession.close();
        }*/
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:org.medici.bia.dao.JpaDao.java

License:Open Source License

/**
 * /* w  ww  .ja v  a2  s . c o m*/
 * @param fromDate
 * @throws PersistenceException
 */
public void updateIndex(Date fromDate) throws PersistenceException {
    Session session = null;
    FullTextSession fullTextSession = null;
    ScrollableResults results = null;
    try {
        EntityManager entityManager = getEntityManager();
        session = ((HibernateEntityManager) entityManager).getSession();
        session = session.getSessionFactory().openSession();
        fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

        Query queryTotal = entityManager
                .createQuery("SELECT count(*) FROM " + entityClass + " where lastUpdate>=:lastUpdate");
        queryTotal.setParameter("lastUpdate", fromDate);
        Long total = (Long) queryTotal.getSingleResult();
        logger.info("Total Entities to be updated : " + total);

        if (total > 0) {
            Integer numberOfElements = 50;
            Integer numberOfElementsBeforeGC = 1000;
            org.hibernate.Query query = session
                    .createQuery("FROM " + entityClass + "  where lastUpdate>=:lastUpdate");
            query.setParameter("lastUpdate", fromDate);

            Transaction tx = fullTextSession.beginTransaction();
            query.setReadOnly(true);
            query.setLockMode("a", LockMode.NONE);
            results = query.scroll(ScrollMode.FORWARD_ONLY);
            Integer resultNumber = 0;
            while (results.next()) {
                Object entityClass = (Object) results.get(0);
                fullTextSession.delete(entityClass);
                fullTextSession.index(entityClass);
                resultNumber++;

                if (resultNumber % numberOfElements == 0) {
                    flushingFullTextSession(total, resultNumber, fullTextSession);
                }

                if (resultNumber % numberOfElementsBeforeGC == 0) {
                    System.gc();
                    logger.info("Invoked Garbage collector to prevent OutOfMemory Errors");
                }
            }

            flushingFullTextSession(total, resultNumber, fullTextSession);
            tx.commit();

            logger.info("Initiating Lucene Index Optimze...");
            SearchFactory searchFactory = fullTextSession.getSearchFactory();
            searchFactory.optimize(entityClass);
            logger.info("Finished Lucene Index Optimze");
        } else {
            logger.info("No Entities found to be indexed.");
        }
        logger.info("Indexing documents terminated without errors.");
    } catch (Throwable throwable) {
        logger.error(throwable);
    } finally {
        if (results != null) {
            results.close();
        }
        if (fullTextSession.isOpen()) {
            fullTextSession.close();
        }
        if (session.isOpen()) {
            session.close();
        }
    }
}

From source file:org.mifos.application.master.persistence.Upgrade1286195484.java

License:Open Source License

@Override
public void upgrade(Connection connection) throws IOException, SQLException {
    conditionalAlter(connection);/*from   ww  w .j a  v a  2s . co  m*/

    Session session = StaticHibernateUtil.getSessionTL();
    ScrollableResults results = session.createQuery("from CustomerAddressDetailEntity")
            .setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
    Query update = session.createQuery(
            "update CustomerAddressDetailEntity set address.phoneNumberStripped = :phoneNumberStripped where "
                    + "customerAddressId = :id");
    Transaction t = session.beginTransaction();
    int modifyCount = 0;
    int resultCount = 0;
    while (results.next()) {
        ++resultCount;
        CustomerAddressDetailEntity address = (CustomerAddressDetailEntity) results.get(0);
        if (address.getAddress().getPhoneNumber() != null && !address.getAddress().getPhoneNumber().isEmpty()) {
            update.setString("phoneNumberStripped", address.getAddress().getPhoneNumberStripped());
            update.setInteger("id", address.getCustomerAddressId());
            update.executeUpdate();
            ++modifyCount;
            if (modifyCount % COMMIT_EVERY == 0) {
                t.commit();
                t = session.beginTransaction();
            }
        }
        if (resultCount % FLUSH_EVERY == 0) {
            session.flush();
            session.clear();
        }
        if (resultCount % PRINT_EVERY == 0) {
            System.out.print(".");
            System.out.flush();
        }
    }
    session.flush();
    t.commit();
}

From source file:org.n52.sos.ds.hibernate.dao.ObservationDAO.java

License:Open Source License

public ScrollableResults getStreamingObservationsFor(GetObservationRequest request, Set<String> features,
        Criterion temporalFilterCriterion, Session session) throws HibernateException, OwsExceptionReport {
    return getObservationCriteriaFor(request, features, temporalFilterCriterion, null, session)
            .setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);
}

From source file:org.n52.sos.ds.hibernate.dao.ObservationDAO.java

License:Open Source License

public ScrollableResults getStreamingObservationsFor(GetObservationRequest request, Set<String> features,
        Session session) throws HibernateException, OwsExceptionReport {
    return getObservationCriteriaFor(request, features, null, null, session).setReadOnly(true)
            .scroll(ScrollMode.FORWARD_ONLY);
}