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:kr.debop4j.data.hibernate.repository.impl.HibernateDao.java

License:Apache License

@Override
public ScrollableResults getScroll(DetachedCriteria dc) {
    return getScroll(dc, ScrollMode.FORWARD_ONLY);
}

From source file:kr.debop4j.data.hibernate.repository.impl.HibernateDao.java

License:Apache License

@Override
public ScrollableResults getScroll(Criteria criteria) {
    return getScroll(criteria, ScrollMode.FORWARD_ONLY);
}

From source file:kr.debop4j.data.hibernate.repository.impl.HibernateDao.java

License:Apache License

@Override
public ScrollableResults getScroll(Query query, HibernateParameter... parameters) {
    return getScroll(query, ScrollMode.FORWARD_ONLY, parameters);
}

From source file:kr.debop4j.data.ogm.dao.HibernateOgmDao.java

License:Apache License

@Override
public void indexAll(Class<?> clazz, int batchSize) {
    if (isDebugEnabled)
        log.debug("[{}]?  ?  ??? ...", clazz);

    clearIndex(clazz);//  ww w.  ja v a  2s . co  m

    if (batchSize < DEFAUALT_BATCH_SIZE)
        batchSize = DEFAUALT_BATCH_SIZE;

    FullTextSession fts = getFullTextSession();

    FlushMode currentFlushMode = fts.getFlushMode();
    CacheMode currentCacheMode = fts.getCacheMode();
    fts.setFlushMode(FlushMode.MANUAL);
    fts.setCacheMode(CacheMode.IGNORE);

    try {
        Transaction tx = fts.beginTransaction();
        ScrollableResults results = fts.createCriteria(clazz).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            fts.index(results.get(0));
            if (++index % batchSize == 0) {
                fts.flushToIndexes();
                fts.clear();
                if (isTraceEnabled)
                    log.trace("?? . index=[{}]", index);
            }
        }
        fts.flushToIndexes();
        tx.commit();

        log.info("[{}]?   [{}]   ??? !!!", clazz,
                index);
    } finally {
        fts.setFlushMode(currentFlushMode);
        fts.setCacheMode(currentCacheMode);
    }
}

From source file:kr.debop4j.search.dao.HibernateSearchDao.java

License:Apache License

@Override
public void indexAll(Class<?> clazz, int batchSize) {
    if (log.isDebugEnabled())
        log.debug("[{}]?  ?  ??? ...", clazz);

    clearIndex(clazz);/*from  w  w w.ja va2 s .c o m*/

    if (batchSize < BATCH_SIZE)
        batchSize = BATCH_SIZE;

    final FullTextSession fts = getFullTextSession();

    FlushMode currentFlushMode = fts.getFlushMode();
    CacheMode currentCacheMode = fts.getCacheMode();
    fts.setFlushMode(FlushMode.MANUAL);
    fts.setCacheMode(CacheMode.IGNORE);

    try {
        Transaction tx = fts.beginTransaction();
        ScrollableResults results = fts.createCriteria(clazz).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            fts.index(results.get(0));
            if (index % batchSize == 0) {
                fts.flushToIndexes();
                fts.clear();
            }
        }
        fts.flushToIndexes();
        tx.commit();

        if (log.isDebugEnabled())
            log.debug("[{}]?   [{}]   ??? !!!",
                    clazz, index);
    } finally {
        fts.setFlushMode(currentFlushMode);
        fts.setCacheMode(currentCacheMode);
    }
}

From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java

License:Open Source License

/**
 * Execute a batch callback using a named query.
 * /*  w  w w  .j a va  2  s  .  com*/
 * @param queryName the named query name
 * @param parameters the named parameters to pass to the query
 * @param callback the callback
 * @return the number of items processed
 */
protected Integer executeNamedQueryBatchCallback(final String queryName, final Map<String, Object> parameters,
        final BatchCallback<T> callback) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        @SuppressWarnings("unchecked")
        @Override
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(queryName);
            if (parameters != null) {
                for (String paramName : parameters.keySet()) {
                    q.setParameter(paramName, parameters.get(paramName));
                }
            }
            q.setCacheMode(CacheMode.IGNORE);
            ScrollableResults items = q.scroll(ScrollMode.FORWARD_ONLY);
            int count = 0;

            OUTER: while (items.next()) {
                T item = (T) items.get(0);
                BatchCallbackResult action = callback.handle(item);
                switch (action) {
                case DELETE:
                    session.delete(item);
                    break;

                case UPDATE:
                case UPDATE_STOP:
                    store(item);
                    if (action == BatchCallbackResult.UPDATE_STOP) {
                        break OUTER;
                    }
                    break;

                case STOP:
                    break OUTER;

                case CONTINUE:
                    // nothing to do
                    break;
                }
                if (++count % batchFlushCount == 0) {
                    session.flush();
                    session.clear();
                }
            }

            return count;
        }
    });
}

From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java

License:Open Source License

/**
 * Execute a batch callback against a StatelessSession using a named query.
 * // www.j a v a 2  s .c  o  m
 * <p>The DELETE, UPDATE, and UPDATE_STOP {@link BatchCallbackResult}
 * values are not supported in this operation, and will throw an 
 * <code>UnsupportedOperationException</code> if returned by the 
 * {@link BatchCallback} instance passed to this method.</p>
 * 
 * @param criteriaBuilder the criteria builder
 * @param callback the callback
 * @param options the options
 * @return the number of items processed
 */
@SuppressWarnings("unchecked")
protected Integer executeStatelessCriteriaBatchCallback(final CriteriaBuilder criteriaBuilder,
        final BatchCallback<T> callback, final BatchOptions options) {
    StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
    Transaction tx = session.beginTransaction();
    try {
        Criteria criteria = session.createCriteria(getType());
        criteria.setFetchSize(options.getBatchSize());
        criteriaBuilder.buildCriteria(criteria);
        ScrollableResults items = criteria.scroll(ScrollMode.FORWARD_ONLY);
        int count = 0;

        OUTER: while (items.next()) {
            T item = (T) items.get(0);
            BatchCallbackResult action = callback.handle(item);
            switch (action) {
            case DELETE:
            case UPDATE:
            case UPDATE_STOP:
                throw new UnsupportedOperationException("Action " + action + " not possible during "
                        + options.getMode() + " mode batch processing");

            case STOP:
                break OUTER;

            case CONTINUE:
                // nothing to do
                break;
            }
        }
        tx.commit();
        return count;
    } catch (RuntimeException e) {
        tx.rollback();
        throw e;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java

License:Open Source License

/**
 * Execute a batch callback against a normal Hibernate Session using a named query.
 * /*from www.  j  av  a  2 s. c om*/
 * @param criteriaBuilder the criteria builder
 * @param callback the callback
 * @param options the options
 * @return the number of items processed
 */
protected Integer executeLiveCriteriaBatchCallback(final CriteriaBuilder criteriaBuilder,
        final BatchCallback<T> callback, final BatchOptions options) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        @SuppressWarnings("unchecked")
        @Override
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(type);
            criteria.setFetchSize(options.getBatchSize());
            criteriaBuilder.buildCriteria(criteria);
            ScrollableResults items = criteria.scroll(ScrollMode.FORWARD_ONLY);
            int count = 0;

            OUTER: while (items.next()) {
                T item = (T) items.get(0);
                BatchCallbackResult action = callback.handle(item);
                switch (action) {
                case DELETE:
                    session.delete(item);
                    break;

                case UPDATE:
                case UPDATE_STOP:
                    store(item);
                    if (action == BatchCallbackResult.UPDATE_STOP) {
                        break OUTER;
                    }
                    break;

                case STOP:
                    break OUTER;

                case CONTINUE:
                    // nothing to do
                    break;
                }
                if (++count % 20 == 0) {
                    session.flush();
                    session.clear();
                }
            }

            return count;
        }
    });
}

From source file:magoffin.matt.ma2.dao.hbm.HibernateAlbumDao.java

License:Open Source License

public int reassignAlbumsUsingTheme(final Theme oldTheme, final Theme newTheme) {
    return getHibernateTemplate().execute(new HibernateCallback<Integer>() {

        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            ScrollableResults albums = session.getNamedQuery(QUERY_ALBUMS_FOR_THEME_ID)
                    .setLong("themeId", oldTheme.getThemeId()).setCacheMode(CacheMode.IGNORE)
                    .scroll(ScrollMode.FORWARD_ONLY);
            int count = 0;
            while (albums.next()) {
                Album album = (Album) albums.get(0);
                album.setTheme(newTheme);
                if (++count % 20 == 0) {
                    session.flush();/*ww w  .  j  a  v a 2  s.  c o  m*/
                    session.clear();
                }
            }
            return count;
        }
    });
}

From source file:mitm.application.djigzo.impl.hibernate.UserDAO.java

License:Open Source License

public CloseableIterator<String> getEmailIterator(Integer firstResult, Integer maxResults,
        SortDirection sortDirection) {/*from   ww w .j  ava  2  s  . com*/
    if (sortDirection == null) {
        sortDirection = SortDirection.ASC;
    }

    /*
     * We will use HQL because I do not (yet?) know how to return just a field instead of 
     * an object when using the Criteria API.
     */
    final String hql = "select n.email from " + entityName + " n order by n.email " + sortDirection;

    Query query = createQuery(hql);

    if (firstResult != null) {
        query.setFirstResult(firstResult);
    }

    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }

    ScrollableResults scrollableResults = query.scroll(ScrollMode.FORWARD_ONLY);

    return new EmailIterator(scrollableResults);
}