Example usage for javax.persistence Query setFirstResult

List of usage examples for javax.persistence Query setFirstResult

Introduction

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

Prototype

Query setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

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());//www  .java 2s.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:com.seer.datacruncher.jpa.dao.ChecksTypeDao.java

public ReadList read(int start, int limit) {
    ReadList readList = new ReadList();
    try {/*from w  ww . j a v  a  2 s.c  o  m*/
        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:corner.orm.gae.impl.PaginatedJpaEntityServiceTest.java

@Test
public void test_paginate_with_order() {
    EntityManager entityManager = newMock(EntityManager.class);
    Query query = newMock(Query.class);
    Query query2 = newMock(Query.class);
    expect(entityManager.createQuery(/*www.java 2  s. c o m*/
            "select root.id from corner.orm.gae.impl.TestAEntity as root  where name=:1 order by id desc"))
                    .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" }, "id desc",
            options);
    assertFalse(((Iterator) pl.collectionObject()).hasNext());
    PaginationOptions optionsR = pl.options();
    assertEquals(1234, optionsR.getTotalRecord());
    verify();
}

From source file:org.opentides.dao.impl.BaseEntityDaoJpaImpl.java

/**
 * {@inheritDoc}//  w  ww  . j ava  2 s. c om
 */
@Override
@SuppressWarnings("unchecked")
public final List<T> findAll(int start, int total) {
    Query query = getEntityManager().createQuery("select obj from " + getEntityBeanType().getName() + " obj ");
    if (start > -1)
        query.setFirstResult(start);
    if (total > -1)
        query.setMaxResults(total);
    return query.getResultList();
}

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

public ReadList readRegExps(int start, int limit) {
    ReadList readList = new ReadList();
    try {/*w  w  w  .j  ava 2  s .  co 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:org.apache.syncope.core.persistence.jpa.dao.JPATaskDAO.java

@Override
@SuppressWarnings("unchecked")
public <T extends Task> List<T> findAll(final TaskType type, final ExternalResource resource,
        final Notification notification, final AnyTypeKind anyTypeKind, final String entityKey, final int page,
        final int itemsPerPage, final List<OrderByClause> orderByClauses) {

    StringBuilder queryString = buildFindAllQuery(type, resource, notification, anyTypeKind, entityKey)
            .append(toOrderByStatement(getEntityReference(type), orderByClauses));

    Query query = entityManager().createQuery(queryString.toString());
    if (resource != null) {
        query.setParameter("resource", resource);
    }//from  w  ww.  j  ava 2s  .c o m
    if (notification != null) {
        query.setParameter("notification", notification);
    }
    if (anyTypeKind != null && entityKey != null) {
        query.setParameter("anyTypeKind", anyTypeKind);
        query.setParameter("entityKey", entityKey);
    }

    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));

    if (itemsPerPage > 0) {
        query.setMaxResults(itemsPerPage);
    }

    return query.getResultList();
}

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

public ReadList readComplete(int start, int limit) {
    ReadList readList = new ReadList();
    try {//from  w w w . j a v a2 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;
}

From source file:br.com.surittec.surijpa.criteria.JPQL.java

private Query getQuery() {
    Query query = entityManager.createQuery(this.toString());

    for (String paramName : params.keySet()) {
        query.setParameter(paramName, params.get(paramName));
    }//www .  j a v  a  2 s .c  o m

    if (firstResult != null)
        query.setFirstResult(firstResult);
    if (maxResults != null)
        query.setMaxResults(maxResults);

    return query;
}

From source file:com.sun.socialsite.userapi.UserManagerImpl.java

public List<User> getUsersByLetter(char letter, int offset, int length) throws UserManagementException {
    List<User> users = new ArrayList<User>();
    Query query = getNamedQuery("User.findByUserIdPatternOrderByUserId");
    if (offset != 0) {
        query.setFirstResult(offset);
    }/*w ww.j  a va 2  s .c o  m*/
    if (length != -1) {
        query.setMaxResults(length);
    }
    query.setParameter("pattern", letter + "%");
    List results = query.getResultList();
    if (results != null) {
        for (Object obj : results) {
            users.add((User) obj);
        }
    }
    return users;
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from  ww  w .java 2s .c o m
@SuppressWarnings(value = "unchecked")
public List<GroupRequest> getMembershipRequestsByGroup(Group group, int offset, int length)
        throws SocialSiteException {
    if (group == null) {
        throw new SocialSiteException("Group is null");
    }
    Query query = strategy.getNamedQuery("GroupRequest.getByGroup");
    query.setParameter(1, group);
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<GroupRequest>) query.getResultList();
}