Example usage for org.hibernate Query scroll

List of usage examples for org.hibernate Query scroll

Introduction

In this page you can find the example usage for org.hibernate Query scroll.

Prototype

ScrollableResults scroll();

Source Link

Document

Return the query results as ScrollableResults.

Usage

From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
static public List<Object[]> findMaxSubject() throws HibernateException {
    Session s = null;/*from w  w  w .  j a va  2 s  .c  om*/
    try {
        s = openSession();

        //      Query q = s.createQuery("select grp.sc, grp.sub from ( " +
        //
        //      " select count(subject) as sub, subject from "
        //          + AuditEvent.class.getName() + " GROUP BY subject) AS grp");

        Query q = s.createQuery("select count(ae.subject), ae.subject FROM " + AE_CLASS + " AS ae "
                + "GROUP BY ae.subject ORDER BY count(ae.subject) desc");
        ScrollableResults sr = q.scroll();
        List<Object[]> l = new ArrayList<Object[]>();
        int i = 0;
        while (sr.next()) {
            System.out.println(i++);
            Object[] oa = sr.get();
            l.add(oa);
        }

        return l;
    } finally {
        close(s);
    }
}

From source file:com.algoTrader.entity.marketData.BarDaoBase.java

/**
 * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
 * @param queryObject/* w  ww  .j av a 2 s  . co m*/
 * @param transform
 * @param pageNumber
 * @param pageSize
 * @return PaginationResult
 */
@SuppressWarnings({ "unchecked" })
protected PaginationResult getPaginationResult(final Query queryObject, final int transform, int pageNumber,
        int pageSize) {
    try {
        final ScrollableResults scrollableResults = queryObject.scroll();
        scrollableResults.last();
        int totalCount = scrollableResults.getRowNumber();
        totalCount = totalCount >= 0 ? totalCount + 1 : 0;
        if (pageNumber > 0 && pageSize > 0) {
            queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            queryObject.setMaxResults(pageSize);
        }
        // Unchecked transformation because Set object is reused, cannot be strongly typed.
        Set results = new LinkedHashSet(queryObject.list());
        transformEntities(transform, results);
        return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.appeligo.alerts.PendingAlert.java

License:Apache License

@SuppressWarnings("unchecked")
public static PendingAlert getNextAlert() {
    Permissions.checkUser(Permissions.SUPERUSER);
    Session session = getSession();/* ww w.j av  a 2  s  . co  m*/
    Query query = session.getNamedQuery("PendingAlert.getNextAlerts");
    ScrollableResults results = query.scroll();
    results.beforeFirst();
    PendingAlert pendingAlert = null;
    if (results.next()) {
        pendingAlert = (PendingAlert) results.get(0);
    }
    results.close();
    return pendingAlert;
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/*  www .  j  a va2 s.  c om*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void applyLabel(Session hsession, String repositoryName, Integer label, Integer[] idints)
        throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        if ((label == null) || (label.intValue() <= 0)) {
            return;
        }

        Users user = getUser(hsession, repositoryName);

        Criteria crit = hsession.createCriteria(Label.class);
        crit.add(Restrictions.eq("labIdint", label));
        crit.add(Restrictions.eq("users", user));

        Label hlabel = (Label) crit.uniqueResult();

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            LabMes labMes = new LabMes(new LabMesId(message, hlabel));
            hsession.saveOrUpdate(labMes);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from   w  w  w .  j a v a 2 s .co  m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param mids DOCUMENT ME!
 * @param flag DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void flagFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        Users user = getUser(hsession, repositoryName);

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            if (message.isMesFlagged()) {
                message.setMesFlagged(false);
            } else {
                message.setMesFlagged(true);
            }

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!/* ww w. j  a  va2 s. c  om*/
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void deleteFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        Users user = getUser(hsession, repositoryName);

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            message.setMesBox(this.folderTrash);

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from   w  w w .  j  av  a 2  s  . c  om
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param idints DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public void restoreFiles(Session hsession, String repositoryName, Integer[] idints) throws FilesException {
    if ((idints == null) || (idints.length == 0)) {
        throw new FilesException("ErrorMessages.messages.selection.null");
    }

    try {
        Users user = getUser(hsession, repositoryName);

        Query query = hsession.getNamedQuery("messages-by-attachments");
        query.setParameterList("idints", idints);
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Message message = (Message) scroll.get(0);

            message.setMesBox(this.folderFiles);

            hsession.update(message);
            hsession.flush();
        }

        hsession.flush();
    } catch (Exception e) {
        throw new FilesException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}

From source file:com.duroty.application.files.manager.FilesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from  w w  w .  j  av a 2s.  co  m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws FilesException DOCUMENT ME!
 */
public Counters getInfoCounters(Session hsession, String repositoryName) throws FilesException {
    Counters counters = new Counters();

    try {
        Users user = getUser(hsession, repositoryName);

        Query query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderInbox);
        query.setInteger("user", user.getUseIdint());
        counters.setInbox(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderSpam);
        query.setInteger("user", user.getUseIdint());
        counters.setSpam(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("group-count-new-messages-by-label");
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Integer idint = (Integer) scroll.get(0);
            Integer count = (Integer) scroll.get(1);

            counters.addLabel(idint.intValue(), count.intValue());
        }

        counters.setQuota(getQuotaLayer(hsession, user));
    } catch (Exception ex) {
        return null;
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }

    return counters;
}

From source file:com.duroty.application.mail.manager.MailManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from  w  w w .j av a  2  s .c  o m
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public Counters getInfoCounters(Session hsession, String repositoryName) throws MailException {
    Counters counters = new Counters();

    try {
        Users user = getUser(hsession, repositoryName);

        Query query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderInbox);
        query.setInteger("user", user.getUseIdint());
        counters.setInbox(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("count-new-messages-by-folder");
        query.setString("folder", this.folderSpam);
        query.setInteger("user", user.getUseIdint());
        counters.setSpam(((Integer) query.uniqueResult()).intValue());

        query = hsession.getNamedQuery("group-count-new-messages-by-label");
        query.setInteger("user", user.getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Integer idint = (Integer) scroll.get(0);
            Integer count = (Integer) scroll.get(1);

            counters.addLabel(idint.intValue(), count.intValue());
        }

        counters.setQuota(getQuotaLayer(hsession, user));
    } catch (Exception ex) {
        return null;
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }

    return counters;
}

From source file:com.duroty.application.mail.manager.PreferencesManager.java

License:Open Source License

/**
 * DOCUMENT ME!//from w ww.  ja va  2s .c om
 *
 * @param hsession DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
public Vector getFilters(Session hsession, String repositoryName) throws MailException {
    Vector filters = new Vector();

    try {
        Query query = hsession.getNamedQuery("filters");
        query.setInteger("user", getUser(hsession, repositoryName).getUseIdint());

        ScrollableResults scroll = query.scroll();

        while (scroll.next()) {
            Filter filter = (Filter) scroll.get(0);
            FilterObj obj = new FilterObj();
            Label label = filter.getLabel();

            obj.setArchive(filter.isFilArchive());
            obj.setDoesntHaveWords(filter.getFilDoesntHaveWords());
            obj.setForward(filter.getFilForwardTo());
            obj.setFrom(filter.getFilFrom());
            obj.setHasAttachment(filter.isFilHasAttacment());
            obj.setHasWords(filter.getFilHasWords());
            obj.setIdint(filter.getFilIdint());
            obj.setImportant(filter.isFilImportant());
            obj.setLabel(new LabelObj(label.getLabIdint(), label.getLabName()));
            obj.setOperator(!filter.isFilOrOperator());
            obj.setSubject(filter.getFilSubject());
            obj.setTo(filter.getFilTo());
            obj.setTrash(filter.isFilTrash());

            filters.addElement(obj);
        }

        return filters;
    } catch (Exception e) {
        throw new MailException(e);
    } finally {
        GeneralOperations.closeHibernateSession(hsession);
    }
}