Example usage for javax.persistence Query setMaxResults

List of usage examples for javax.persistence Query setMaxResults

Introduction

In this page you can find the example usage for javax.persistence Query setMaxResults.

Prototype

Query setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}/*from   w  w  w  . j  a v a  2s . c o m*/
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findLastEntry(User user) throws PersistenceException {
    String queryString = "FROM UserHistory WHERE user=:user and logicalDelete=false ORDER BY dateAndTime DESC";

    Query query = getEntityManager().createQuery(queryString);

    query.setParameter("user", user);
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() == 1) {
        return result.get(0);
    }

    return null;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}/* w w w .j a  v  a2 s.c o m*/
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findLastEntryBase(User user) throws PersistenceException {
    String queryString = "FROM UserHistory WHERE user=:user and logicalDelete=false  AND (category LIKE 'DOCUMENT' OR category LIKE 'VOLUME' OR category LIKE 'PEOPLE' OR category LIKE 'PLACE') ORDER BY dateAndTime DESC";

    Query query = getEntityManager().createQuery(queryString);

    query.setParameter("user", user);
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() == 1) {
        return result.get(0);
    }

    return null;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}//from   w  w  w.  j av a  2  s. co  m
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findNextCategoryHistoryCursor(User user, Category category, Integer idUserHistory)
        throws PersistenceException {
    // -- Next
    // SELECT * FROM tblUserHistory WHERE user.account='lpasquinelli' AND idUserHistory > 15 and entryId is not null ORDER BY idUserHistory ASC LIMIT 1
    StringBuilder stringBuilder = new StringBuilder("FROM UserHistory WHERE user.account='");
    stringBuilder.append(user.getAccount());
    stringBuilder.append('\'');
    stringBuilder.append(" AND idUserHistory > ");
    stringBuilder.append(idUserHistory);

    switch (category) {
    case DOCUMENT:
        stringBuilder.append(" AND document is not null ");
        break;
    case PEOPLE:
        stringBuilder.append(" AND person is not null ");
        break;
    case PLACE:
        stringBuilder.append(" AND place is not null ");
        break;
    case VOLUME:
        stringBuilder.append(" AND volume is not null ");
        break;
    case FORUM:
        stringBuilder.append(" AND forum is not null ");
        break;
    case FORUM_POST:
        stringBuilder.append(" AND forumPost is not null ");
        break;
    default:
        break;
    }

    stringBuilder.append(" ORDER BY idUserHistory ASC LIMIT 1");

    Query query = getEntityManager().createQuery(stringBuilder.toString());
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() > 0) {
        return result.get(0);
    }

    return null;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}//from  ww w.j  a v a 2s .c  o m
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findPreviousCategoryHistoryCursor(User user, Category category, Integer idUserHistory)
        throws PersistenceException {
    // -- Previous
    // SELECT * FROM tblUserHistory WHERE user.account='lpasquinelli' AND idUserHistory < 15 and entryId is not null ORDER BY idUserHistory DESC LIMIT 1
    StringBuilder stringBuilder = new StringBuilder("FROM UserHistory WHERE user.account='");
    stringBuilder.append(user.getAccount());
    stringBuilder.append("' AND idUserHistory < ");
    stringBuilder.append(idUserHistory);

    switch (category) {
    case DOCUMENT:
        stringBuilder.append(" AND document is not null ");
        break;
    case PEOPLE:
        stringBuilder.append(" AND person is not null ");
        break;
    case PLACE:
        stringBuilder.append(" AND place is not null ");
        break;
    case VOLUME:
        stringBuilder.append(" AND volume is not null ");
        break;
    case FORUM:
        stringBuilder.append(" AND forum is not null ");
        break;
    case FORUM_POST:
        stringBuilder.append(" AND forumPost is not null ");
        break;

    default:
        break;
    }

    stringBuilder.append(" ORDER BY idUserHistory DESC LIMIT 1");

    Query query = getEntityManager().createQuery(stringBuilder.toString());
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() > 0) {
        return result.get(0);
    }

    return null;
}

From source file:org.medici.bia.dao.image.ImageDAOJpaImpl.java

/**
 * {@inheritDoc}/*  w w w  . j  a  va  2s.c  om*/
 */
@Override
public Image findVolumeImage(Integer volNum, String volLetExt, Integer imageOrder) throws PersistenceException {
    StringBuilder stringBuilder = new StringBuilder(" FROM Image WHERE volNum=:volNum and volLetExt ");
    if (!StringUtils.isEmpty(volLetExt))
        stringBuilder.append(" LIKE :volLetExt");
    else
        stringBuilder.append(" is null");
    stringBuilder.append(" and imageOrder=:imageOrder");

    Query query = getEntityManager().createQuery(stringBuilder.toString());
    query.setParameter("volNum", volNum);
    if (!StringUtils.isEmpty(volLetExt)) {
        query.setParameter("volLetExt", volLetExt);
    }

    query.setParameter("imageOrder", imageOrder);
    query.setFirstResult(0);
    query.setMaxResults(1);
    return (Image) query.getSingleResult();
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * Funziona SOLO CON IMPLEMENTAZIONE HIBERNATE
 * /*  w  w  w  . j a  va  2  s.c  om*/
 * @param aList
 * @param aFilter
 * @return
 * @throws Exception
 */
public <T extends EntityBaseStandard> ArrayList<T> getFilteredList(Class<T> classObj, String aFilter)
        throws Exception {
    ArrayList<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Session session = null;
    Criteria cri = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        session = (Session) em.getDelegate();

        org.hibernate.Query q = session.createQuery("from " + classObj.getName());
        q.setFirstResult(0);
        q.setMaxResults(100);
        List<T> resAll = q.list();

        org.hibernate.Query filterQuery = session.createFilter(resAll, aFilter);

        /* Effettuo la query */
        returnValue = (ArrayList) filterQuery.list();
    } catch (Exception e) {
        throw e;
    } finally {
        session = null;
        cri = null;
        em = null;
    }
    return returnValue;
}

From source file:org.tolven.analysis.bean.SnapshotBean.java

private TolvenMessage getNextScheduledTolvenMessage(String cohortType, String snapshotType, Date now,
        Account account) {//w ww. j a v  a  2 s .c o  m
    Query query = em.createQuery("SELECT tm FROM TolvenMessage tm WHERE " + "tm.accountId = :accountId AND "
            + "tm.xmlNS = :xmlNS AND " + "tm.scheduleDate is NOT NULL AND " + "tm.queueOnDate is NOT NULL AND "
            + "tm.queueOnDate > :now AND " + "tm.queueDate is NULL AND " + "tm.processDate is NULL AND "
            + "(tm.deleted IS NULL OR tm.deleted = false) AND "
            + "tm in (SELECT tmp.tolvenMessage from TolvenMessageProperty tmp where tmp.propertyName = 'cohortType' and tmp.string = :cohortType) AND "
            + "tm in (SELECT tmp.tolvenMessage from TolvenMessageProperty tmp where tmp.propertyName = 'snapshotType' and tmp.string = :snapshotType) "
            + "order by tm.queueOnDate");
    query.setParameter("now", now);
    query.setParameter("accountId", account.getId());
    query.setParameter("xmlNS", "org.tolven.analysis");
    query.setParameter("cohortType", cohortType);
    query.setParameter("snapshotType", snapshotType);
    query.setMaxResults(1);
    List<TolvenMessage> tms = query.getResultList();
    if (tms.isEmpty()) {
        return null;
    } else {
        return tms.get(0);
    }
}

From source file:org.medici.bia.dao.user.UserDAOJpaImpl.java

/**
 * {@inheritDoc}/* w ww .  j  av  a2s .  c  o  m*/
 */
@Override
public Page searchWhoIsOnline(PaginationFilter paginationFilter) {
    // We prepare object of return method.
    Page page = new Page(paginationFilter);

    // Number of minutes to check last update for people online
    //DateTime dateTime = (new DateTime(System.currentTimeMillis())).minusMinutes(5);
    DateTime dateTime = (new DateTime(System.currentTimeMillis())).minusMinutes(30);

    String baseQuery = " FROM User user, AccessLog accessLog WHERE user.account = accessLog.account and (accessLog.dateAndTime > '"
            + DateUtils.getMYSQLDateTime(dateTime) + "')";

    Query query = null;
    // We set size of result.
    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(DISTINCT user) " + baseQuery;

        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    // We manage sorting (this manages sorting on multiple fields)
    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    String jpql = "SELECT DISTINCT(user) " + baseQuery
            + getOrderByQuery(paginationFilter.getSortingCriterias(), "user");
    logger.info("JPQL Query : " + jpql);
    query = getEntityManager().createQuery(jpql);
    // We set pagination  
    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    // We set search result on return method
    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}/*from  w w w. jav  a2 s  .c om*/
 */
@SuppressWarnings("unchecked")
@Override
public List<UserHistory> findHistory(User user, Category category, Integer resultSize)
        throws PersistenceException {
    String queryString = "FROM UserHistory WHERE user=:user and category=:category and logicalDelete=false ORDER BY dateAndTime DESC";

    Query query = getEntityManager().createQuery(queryString);

    query.setParameter("user", user);
    query.setParameter("category", category);
    query.setMaxResults(resultSize);

    return query.getResultList();
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

/**
 * {@inheritDoc}//from  w w w  .  j av  a 2  s .co m
 */
@SuppressWarnings("unchecked")
@Override
public UserHistory findLastEntry(User user, Category category) {
    String queryString = "FROM UserHistory WHERE user=:user and category=:category and logicalDelete=false ORDER BY dateAndTime DESC";

    Query query = getEntityManager().createQuery(queryString);

    query.setParameter("user", user);
    query.setParameter("category", category);
    query.setMaxResults(1);

    List<UserHistory> result = query.getResultList();

    if (result.size() == 1) {
        return result.get(0);
    }

    return null;
}