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:com.eu.evaluation.server.dao.DefaultDAO.java

/**
 * ??//www. ja va 2s.  c om
 * @param jpql
 * @param params
 * @return 
 */
public <T> T findOnylFirst(String jpql, MapSqlParameterSource params) {
    Query query = this.createQuery(jpql, params);
    query.setFirstResult(0);
    query.setMaxResults(1);
    List<T> result = query.getResultList();
    if (result.isEmpty()) {
        return null;
    } else {
        return result.get(0);
    }
}

From source file:com.fantasy.stataggregator.entities.dao.AbstractRepository.java

public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));/*from  www. j ava  2  s.c om*/
    javax.persistence.Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

From source file:com.luna.common.repository.UserRepository2Impl.java

public BaseInfo findBaseInfoByUserId(Long userId) {
    String ql = "select bi from BaseInfo bi where bi.user.id=?1";
    Query query = entityManager.createQuery(ql);
    query.setParameter(1, userId);// w ww .j  ava2s .c o m
    query.setMaxResults(1);
    List<BaseInfo> baseInfoList = query.getResultList();
    if (baseInfoList.size() > 0) {
        return baseInfoList.get(0);
    }
    return null;
}

From source file:modelo.dao.GestionHistoricoImpl.java

@Override
public Historico obtenerUltimoHistorico(String referencia, int usuario) {
    Query q = em.createNamedQuery("Historico.findLastDateByRef");
    q.setParameter(1, referencia);/*from   ww  w . j  ava2s  .  c o m*/
    q.setParameter(2, usuario);
    q.setMaxResults(1);
    try {
        return (Historico) q.getSingleResult();
    } catch (Exception e) {
        return new Historico(null);
    }
}

From source file:com.wpl.db.query.jpa.Limit.java

public void setParameter(final IQuery query) {

    final Query q = (Query) query.getQuery();

    if (mFirst != null) {
        q.setFirstResult(mFirst);//w w w . j a  v a2s  . co  m
    }

    if (mMax != null) {
        q.setMaxResults(mMax);
    }
}

From source file:com.june.app.site.repository.jpa.VideoRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override//from  w  ww.j  ava2s. c  om
public Collection<Video> videoListWithPaging(Video vo) {

    int pageSize = vo.getPageSize();
    int pageNumber = (int) vo.getPageIndex();

    String queryString = "SELECT video FROM Video video WHERE video.useYn ='Y' ORDER BY video.regiDate desc";
    Query query = this.em.createQuery(queryString);
    query.setFirstResult((pageNumber - 1) * pageSize);
    query.setMaxResults(pageSize);

    return query.getResultList();
}

From source file:dao.ExperienceDAOImpl.java

@Override
public List<ExperienceEntity> findExperiencesForProfil(Long profileId, int limit) {
    Query q;
    q = this.em.createQuery("SELECT e FROM ExperienceEntity e WHERE e.profile.id = ?");
    q.setParameter(1, profileId);/*w  w  w .  j  a v  a2  s  . co  m*/
    q.setMaxResults(limit);
    return q.getResultList();
}

From source file:org.eurekastreams.server.persistence.mappers.stream.GetFlaggedActivities.java

/**
 * Get all flagged activities./*  w w  w . ja  v  a 2  s .  co m*/
 * 
 * @param inRequest
 *            the request containing the start and end index
 * @return a paged set of ActivityDTOs
 */
@Override
@SuppressWarnings("unchecked")
public PagedSet<ActivityDTO> execute(final GetFlaggedActivitiesRequest inRequest) {
    if (inRequest.getRequestingUserAccountId() == null) {
        log.error("Missing requesting user account.");
        throw new RuntimeException("Missing requesting user account.");
    }

    // get the total number of flagged activities
    Long flaggedActivityCount = (Long) buildQuery("count(*)").getSingleResult();

    List<ActivityDTO> activities;
    if (flaggedActivityCount > 0) {
        log.info("Found " + flaggedActivityCount
                + " flagged activity ids - passing them to the bulk activity mapper for ActivityDTOs.");

        Query q = buildQuery("id");
        q.setFirstResult(inRequest.getStartIndex());
        q.setMaxResults(inRequest.getEndIndex() - inRequest.getStartIndex() + 1);
        List<Long> activityIds = q.getResultList();

        activities = activitiesMapper.execute(activityIds);
    } else {
        log.info("Found no flagged activity ids.");
        activities = new ArrayList<ActivityDTO>();
    }
    return new PagedSet<ActivityDTO>(inRequest.getStartIndex(), inRequest.getEndIndex(),
            flaggedActivityCount.intValue(), activities);
}

From source file:com.grummages.app.rest.entity.service.AddonRESTFacade.java

private List<Addon> find(boolean all, int maxResults, int firstResult) {
    try {/*ww w.  j ava2  s.  co m*/
        Query query = entityManager.createQuery("SELECT object(o) FROM Addon AS o");
        if (!all) {
            query.setMaxResults(maxResults);
            query.setFirstResult(firstResult);
        }
        return query.getResultList();
    } finally {
        entityManager.close();
    }
}

From source file:com.grummages.app.rest.entity.service.UsersRESTFacade.java

private List<Users> find(boolean all, int maxResults, int firstResult) {
    try {//from  ww w .  j a va 2s.com
        Query query = entityManager.createQuery("SELECT object(o) FROM Users AS o");
        if (!all) {
            query.setMaxResults(maxResults);
            query.setFirstResult(firstResult);
        }
        return query.getResultList();
    } finally {
        entityManager.close();
    }
}