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:de.iai.ilcd.model.dao.ProcessDao.java

/**
 * Get the list of processes which have the provided direction and flow as in- or output exchange flow
 * //  w  w  w.  j  ava2 s.  co m
 * @param flowUuid
 *            uuid of flow
 * @param direction
 *            direction of flow
 * @param firstResult
 *            start index
 * @param maxResults
 *            maximum result items
 * @return list of processes which have the provided direction and flow as in- or output exchange flow
 */
@SuppressWarnings("unchecked")
public List<Process> getProcessesForExchangeFlow(String flowUuid, ExchangeDirection direction, int firstResult,
        int maxResults) {
    Query q = this.getProcessesForExchangeFlowQuery(flowUuid, direction, false);
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserAction addUserFootprint(UserAction a) throws UserTrackingException {
    a.setType("FOOTPRINT");
    EntityManager em = null;/* ww  w.j  av a  2  s  .c  o  m*/
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        Query q = em.createNamedQuery("findLastUserFootprintOfSession");
        q.setMaxResults(1);
        q.setParameter("sessionId", a.getSessionId());
        Collection<UserAction> userActions = q.getResultList();

        if (userActions.size() >= 1) {
            UserAction last = userActions.iterator().next();
            if (last.getMediapackageId().equals(a.getMediapackageId()) && last.getType().equals(a.getType())
                    && last.getOutpoint() == a.getInpoint()) {
                last.setOutpoint(a.getOutpoint());
                a = last;
                a.setId(last.getId());
            } else {
                em.persist(a);
            }
        } else {
            em.persist(a);
        }
        tx.commit();
        return a;
    } catch (Exception e) {
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        throw new UserTrackingException(e);
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserActionList getUserActions(int offset, int limit) {
    UserActionList result = new UserActionListImpl();

    result.setTotal(getTotal());/* w  w  w  .  j ava  2 s. c o  m*/
    result.setOffset(offset);
    result.setLimit(limit);

    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findUserActions");
        q.setFirstResult(offset);
        q.setMaxResults(limit);
        Collection<UserAction> userActions = q.getResultList();

        for (UserAction a : userActions) {
            result.add(a);
        }

        return result;
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:corner.orm.gae.impl.PaginatedJpaEntityServiceTest.java

@Test
public void test_paginate() {
    EntityManager entityManager = newMock(EntityManager.class);
    Query query = newMock(Query.class);
    Query query2 = newMock(Query.class);
    expect(entityManager//from  w ww . j a v a 2  s  .c o  m
            .createQuery("select root.id from corner.orm.gae.impl.TestAEntity as root  where name=:1"))
                    .andReturn(query);
    expect(entityManager
            .createQuery("select count(root)  from corner.orm.gae.impl.TestAEntity as root  where name=:1"))
                    .andReturn(query2);
    expect(query.setParameter("1", "acai")).andReturn(query);
    expect(query2.setParameter("1", "acai")).andReturn(query2);
    expect(query.setFirstResult(0)).andReturn(query);
    expect(query.setMaxResults(10)).andReturn(query);
    List listValue = Collections.EMPTY_LIST;
    expect(query.getResultList()).andReturn(listValue);
    expect(query2.getSingleResult()).andReturn(new Integer(1234));

    JpaTemplate jpaTemplate = GaeModule.buildJpaTemplate(entityManager);
    replay();
    PaginatedJapEntityService pjes = new PaginatedJapEntityService(jpaTemplate, typeCoercer);
    PaginationOptions options = new PaginationOptions();
    PaginationList pl = pjes.paginate(TestAEntity.class, new String[] { "name=:1", "acai" }, null, options);
    assertFalse(((Iterator) pl.collectionObject()).hasNext());
    PaginationOptions optionsR = pl.options();
    assertEquals(1234, optionsR.getTotalRecord());
    verify();
}

From source file:org.nuxeo.ecm.activity.ActivityStreamServiceImpl.java

@SuppressWarnings("unchecked")
protected ActivitiesList queryAll(EntityManager em, long offset, long limit) {
    Query query = em.createQuery("select activity from Activity activity order by activity.id asc");
    if (limit > 0) {
        query.setMaxResults((int) limit);
    }/*from  ww w  .j  a  v  a  2 s  .co m*/
    if (offset > 0) {
        query.setFirstResult((int) offset);
    }
    return new ActivitiesListImpl(query.getResultList());
}

From source file:com.sun.socialsite.business.impl.JPAGroupManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///  www .  j  av  a2 s.co m
@SuppressWarnings(value = "unchecked")
public List<Group> getGroups(int offset, int length) throws SocialSiteException {
    Query query = strategy.getNamedQuery("Group.getAll");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<Group>) query.getResultList();
}

From source file:com.seer.datacruncher.jpa.dao.ChecksTypeDao.java

public ReadList read(int start, int limit) {
    ReadList readList = new ReadList();
    try {//from w w  w  . ja va2 s .c om
        em.clear();
        @SuppressWarnings("unchecked")
        List<Long> count = em.createNamedQuery("ChecksTypeEntity.count").getResultList();
        Query query = em.createNamedQuery("ChecksTypeEntity.findAll");
        if (start >= 0 && limit >= 0) {
            query.setFirstResult(start);
            query.setMaxResults(limit);
        }
        readList.setTotal(count.get(0));
        readList.setResults(query.getResultList());
        em.clear();
        @SuppressWarnings("unchecked")
        List<ChecksTypeEntity> listResults = (List<ChecksTypeEntity>) readList.getResults();
        if (listResults != null && listResults.size() > 0) {
            for (ChecksTypeEntity entity : listResults) {
                if (entity.getExtraCheckType().equalsIgnoreCase("Regular Expression")) {
                    entity.setRegExp(true);
                } else {
                    entity.setRegExp(false);
                }
                if (entity.getNlsId() != null) {
                    entity.setNlsId(I18n.getMessage(entity.getNlsId()));
                } else {
                    entity.setNlsId(entity.getName());
                }
            }
        }
    } catch (Exception exception) {
        log.error("ChecksTypeDao - read : " + exception);
        readList.setSuccess(false);
        readList.setMessage(I18n.getMessage("error.error") + "  : ChecksTypeDao - read");
        return readList;
    }
    readList.setSuccess(true);
    readList.setMessage(I18n.getMessage("success.listRecord"));
    return readList;
}

From source file:com.sun.socialsite.business.impl.JPAGroupManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *//*from   ww  w . jav  a 2  s .  co m*/
@SuppressWarnings(value = "unchecked")
public List<Group> getOldestGroups(int offset, int length) throws SocialSiteException {
    Query query = strategy.getNamedQuery("Group.getOldest");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<Group>) query.getResultList();
}

From source file:com.seer.datacruncher.jpa.dao.ChecksTypeDao.java

public ReadList readRegExps(int start, int limit) {
    ReadList readList = new ReadList();
    try {//from w ww.j a va 2s .  c  o  m
        em.clear();
        @SuppressWarnings("unchecked")
        List<Long> count = em.createNamedQuery("ChecksTypeEntity.countRegExps").getResultList();
        Query query = em.createNamedQuery("ChecksTypeEntity.findAllRegExps");
        if (start >= 0 && limit >= 0) {
            query.setFirstResult(start);
            query.setMaxResults(limit);
        }
        readList.setTotal(count.get(0));
        readList.setResults(query.getResultList());
        em.clear();
        @SuppressWarnings("unchecked")
        List<ChecksTypeEntity> listResults = (List<ChecksTypeEntity>) readList.getResults();
        if (listResults != null && listResults.size() > 0) {
            for (ChecksTypeEntity entity : listResults) {
                if (entity.getExtraCheckType().equalsIgnoreCase("Regular Expression")) {
                    entity.setRegExp(true);
                } else {
                    entity.setRegExp(false);
                }
                if (entity.getNlsId() != null) {
                    entity.setNlsId(I18n.getMessage(entity.getNlsId()));
                } else {
                    entity.setNlsId(entity.getName());
                }
            }
        }
    } catch (Exception exception) {
        log.error("ChecksTypeDao - read : " + exception);
        readList.setSuccess(false);
        readList.setMessage(I18n.getMessage("error.error") + "  : ChecksTypeDao - read");
        return readList;
    }
    readList.setSuccess(true);
    readList.setMessage(I18n.getMessage("success.listRecord"));
    return readList;
}

From source file:com.seer.datacruncher.jpa.dao.ChecksTypeDao.java

public ReadList readComplete(int start, int limit) {
    ReadList readList = new ReadList();
    try {// w ww .j  a v  a  2 s .  com
        em.clear();
        Query count = em.createNativeQuery(
                "SELECT((SELECT COUNT(*) FROM jv_checks_types)+(SELECT COUNT(*) FROM jv_macros))"); //TODO use the entity "ChecksTypeEntity"
        Query query = em.createNamedQuery("ChecksTypeEntity.findAllComplete");
        if (start >= 0 && limit >= 0) {
            query.setFirstResult(start);
            query.setMaxResults(limit);
        }
        readList.setTotal(((BigInteger) count.getResultList().get(0)).longValue());
        readList.setResults(query.getResultList());

        em.clear();
        @SuppressWarnings("unchecked")
        List<ChecksTypeEntity> listResults = (List<ChecksTypeEntity>) readList.getResults();
        if (listResults != null && listResults.size() > 0) {
            for (ChecksTypeEntity entity : listResults) {
                if (entity.getExtraCheckType() != null
                        && entity.getExtraCheckType().equalsIgnoreCase("Regular Expression")) {
                    entity.setRegExp(true);
                } else {
                    entity.setRegExp(false);
                }
                if (entity.getNlsId() != null) {
                    entity.setNlsId(I18n.getMessage(entity.getNlsId()));
                } else {
                    entity.setNlsId(entity.getName());
                }
            }
        }
    } catch (Exception exception) {
        log.error("ChecksTypeDao - read : " + exception);
        readList.setSuccess(false);
        readList.setMessage(I18n.getMessage("error.error") + "  : ChecksTypeDao - read");
        return readList;
    }
    readList.setSuccess(true);
    readList.setMessage(I18n.getMessage("success.listRecord"));
    return readList;
}