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.apache.roller.weblogger.business.jpa.JPAUserManagerImpl.java

public List getUsersStartingWith(String startsWith, Boolean enabled, int offset, int length)
        throws WebloggerException {
    Query query = null;

    if (enabled != null) {
        if (startsWith != null) {
            query = strategy.getNamedQuery("User.getByEnabled&UserNameOrEmailAddressStartsWith");
            query.setParameter(1, enabled);
            query.setParameter(2, startsWith + '%');
            query.setParameter(3, startsWith + '%');
        } else {/* w w  w.j  a v a2  s  . c  om*/
            query = strategy.getNamedQuery("User.getByEnabled");
            query.setParameter(1, enabled);
        }
    } else {
        if (startsWith != null) {
            query = strategy.getNamedQuery("User.getByUserNameOrEmailAddressStartsWith");
            query.setParameter(1, startsWith + '%');
        } else {
            query = strategy.getNamedQuery("User.getAll");
        }
    }
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}

From source file:info.dolezel.jarss.rest.v1.FeedsService.java

@GET
@Produces(MediaType.APPLICATION_JSON)/*from   w ww .ja  va2s .c o m*/
@Path("{id}/headlines")
public Response getArticleHeadlines(@Context SecurityContext context, @PathParam("id") int feedId,
        @QueryParam("skip") int skip, @QueryParam("limit") int limit) {
    EntityManager em;
    List<Object[]> articles;
    Query query;
    ArticleHeadlineData[] result;
    Feed feed;
    User user;

    user = (User) context.getUserPrincipal();
    em = HibernateUtil.getEntityManager();

    try {

        feed = em.find(Feed.class, feedId);
        if (feed == null) {
            return Response.status(Response.Status.NOT_FOUND)
                    .entity(new ErrorDescription("Feed does not exist")).build();
        }
        if (!feed.getUser().equals(user)) {
            return Response.status(Response.Status.FORBIDDEN)
                    .entity(new ErrorDescription("Feed not owned by user")).build();
        }

        query = em.createQuery(
                "SELECT fid, fi from FeedItemData fid LEFT OUTER JOIN fid.feedItems AS fi where fid.feedData = :fd and (fi is null or fi.feed = :feed) order by fid.date desc",
                Object[].class).setParameter("fd", feed.getData()).setParameter("feed", feed)
                .setFirstResult(skip);

        if (limit > 0)
            query.setMaxResults(limit);

        articles = query.getResultList();

        result = new ArticleHeadlineData[articles.size()];
        for (int i = 0; i < articles.size(); i++) {
            FeedItemData article = (FeedItemData) articles.get(i)[0];
            FeedItem feedItem = (FeedItem) articles.get(i)[1];

            ArticleHeadlineData data = new ArticleHeadlineData();
            String text;

            data.setPublished(article.getDate().getTime());
            data.setTitle(article.getTitle());
            data.setId(article.getId());

            text = StringUtils.html2text(article.getText());

            if (text.length() > 130)
                text = text.substring(0, 130);

            data.setExcerpt(text);
            data.setLink(article.getLink());

            if (feedItem != null) {
                data.setRead(feedItem.isRead());
                data.setStarred(feedItem.isStarred());
            }

            result[i] = data;
        }

        return Response.ok(result).build();
    } finally {
        em.close();
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}//from  w ww  .  j a v  a  2  s  .  co  m
 */
@Override
public Page searchDocumentsCreatedBefore(Date date, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    String jpql = "FROM Document WHERE dateCreated <= :dateTime";
    Query query = null;

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + jpql;
        query = getEntityManager().createQuery(countQuery);
        query.setParameter("dateTime", date);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    query = getEntityManager().createQuery(jpql + getOrderByQuery(paginationFilter.getSortingCriterias()));
    query.setParameter("dateTime", date);

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

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

/**
 * {@inheritDoc}//from w w  w  . j av  a2 s  .c  o m
 */
@Override
public Page searchMYSQL(org.medici.bia.common.search.Search searchContainer,
        PaginationFilter paginationFilter) {
    // We prepare object of return method.
    Page page = new Page(paginationFilter);

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

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

    String objectsQuery = searchContainer.toJPAQuery();

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

    String jpql = objectsQuery + getOrderByQuery(paginationFilter.getSortingCriterias());
    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.egov.ptis.actions.transfer.PropertyTransferAction.java

public boolean isLatestPropertyMutationClosed() {
    boolean closed = true;
    final javax.persistence.Query qry = entityManager.createNamedQuery("UNDER_WF_MUTATION_BY_UPICNO");
    qry.setParameter("upicNo", basicproperty.getUpicNo());
    qry.setMaxResults(1);
    if (!qry.getResultList().isEmpty())
        closed = false;/*from ww w . jav  a 2 s. c om*/
    return closed;
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ???????//from  w  ww.j a  va2  s  .  c om
 * @param condition 
 * @param page
 * @param pageSize
 * @return List<String>
 * @throws DaoException
 * @author LongXianZhen
 */
@SuppressWarnings("unchecked")
@Override
public List<String> getBarcodeListByCondition(String condition, int page, int pageSize) throws DaoException {
    try {
        List<String> barcodes = null;
        if (StringUtils.isNotBlank(condition)) {
            String sql = "select barcode from member where barcode like :barcode ";
            Query query = entityManager.createNativeQuery(sql).setParameter("barcode", "%" + condition + "%");
            if (page > 0) {
                query.setFirstResult((page - 1) * pageSize);
                query.setMaxResults(pageSize);
            }
            barcodes = query.getResultList();
        }
        return barcodes;
    } catch (Exception e) {
        throw new DaoException(
                "MemberDAOImpl.getBarcodeListByCondition()???????, ?",
                e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ??/*from   w w  w. j  a va2 s  .  com*/
 * @param page 
 * @param size 
 * @param organization 
 * @return
 * @throws DaoException 
 */
@SuppressWarnings("unchecked")
@Override
public List<Member> getAllNotLocalMember(int page, int size, Long organization) throws DaoException {
    try {
        String sql = "SELECT  p.* FROM member p " + "RIGHT JOIN t_meta_initialize_member t ON p.id=t.member_id "
                + "WHERE t.`local`=0 AND t.first_storage_id IS NULL AND t.organization=?1";
        Query query = entityManager.createNativeQuery(sql.toString(), Member.class);
        query.setParameter(1, organization);
        if (page > 0) {
            query.setFirstResult((page - 1) * size);
            query.setMaxResults(size);
        }
        List<Member> result = query.getResultList();
        return result;
    } catch (Exception e) {
        throw new DaoException(
                "MemberDAOImpl.getAllNotLocalMember() ???",
                e);
    }
}

From source file:com.gettec.fsnip.fsn.dao.member.impl.MemberDAOImpl.java

/**
 * ??//from   w  w w. ja v a 2s.  c o m
 * @author tangxin 2015-05-05
 */
@SuppressWarnings("unchecked")
@Override
public List<DetailAlbumVO> getMemberAlbums(Long organization, int page, int pageSize, String cut)
        throws DaoException {
    try {
        String sql = "SELECT pd.id,pd.`name`,tm0.URL,pd.format,pd.des,tm1.censcon1,tm2.censcon2,tm3.censcon3,pd.riskIndex FROM member pd "
                + "LEFT JOIN (SELECT tt.id,tt.URL,tt.UPLOAD_DATE FROM (SELECT pr.id,res.URL,res.UPLOAD_DATE FROM member pr "
                + "LEFT JOIN t_test_member_to_resource t2p ON pr.id = t2p.PRODUCT_ID "
                + "LEFT JOIN t_test_resource res ON t2p.RESOURCE_ID = res.RESOURCE_ID "
                + "WHERE pr.organization = :organization ORDER BY res.UPLOAD_DATE DESC) tt GROUP BY tt.id ORDER BY tt.id DESC) tm0 ON pd.id = tm0.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon1' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '??' GROUP BY pro.id) tm1 ON pd.id = tm1.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon2' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '' GROUP BY pro.id) tm2 ON pd.id = tm2.id "
                +

                "LEFT JOIN (SELECT pro.id,count(tr.id) 'censcon3' FROM member pro "
                + "LEFT JOIN member_instance pi ON pro.id = pi.member_id "
                + "LEFT JOIN test_result tr ON pi.id = tr.sample_id "
                + "where pro.organization = :organization AND tr.publish_flag = 1 and tr.test_type = '?' GROUP BY pro.id) tm3 ON pd.id = tm3.id "
                +

                " where pd.organization = :organization ORDER BY pd.id DESC";
        Query query = entityManager.createNativeQuery(sql);
        if (page > 0 && pageSize > 0) {
            page = (page - 1) * pageSize;
            query.setFirstResult(page);
            query.setMaxResults(pageSize);
        }
        query.setParameter("organization", organization);
        List<Object[]> listMember = query.getResultList();
        //         return createDetailAlbumVO(listMember,cut);
        return null;
    } catch (Exception e) {
        throw new DaoException(e.getMessage(), e);
    }
}

From source file:org.opencms.db.jpa.CmsHistoryDriver.java

/**
 * @see org.opencms.db.I_CmsHistoryDriver#readProjects(org.opencms.db.CmsDbContext)
 *///  ww w .  java 2s .c o m
public List<CmsHistoryProject> readProjects(CmsDbContext dbc) throws CmsDataAccessException {

    List<CmsHistoryProject> projects = new ArrayList<CmsHistoryProject>();

    try {
        Query q = m_sqlManager.createQuery(dbc, C_PROJECTS_HISTORY_READ_ALL);
        q.setMaxResults(300);
        @SuppressWarnings("unchecked")
        List<CmsDAOHistoryProjects> res = q.getResultList();

        for (CmsDAOHistoryProjects hp : res) {
            List<String> resources = readProjectResources(dbc, hp.getPublishTag());
            projects.add(internalCreateProject(hp, resources));
        }
    } catch (PersistenceException e) {
        throw new CmsDataAccessException(Messages.get().container(Messages.ERR_JPA_PERSITENCE, e), e);
    }

    return (projects);
}

From source file:org.apache.roller.weblogger.business.jpa.JPAUserManagerImpl.java

/**
 * Get users of a website/*from w ww . java  2s  .  c o m*/
 */
public List getUsers(Weblog website, Boolean enabled, int offset, int length) throws WebloggerException {
    Query query = null;
    List results = null;
    boolean setRange = offset != 0 || length != -1;

    if (length == -1) {
        length = Integer.MAX_VALUE - offset;
    }

    if (enabled != null) {
        if (website != null) {
            query = strategy.getNamedQuery("User.getByEnabled&Permissions.website");
            query.setParameter(1, enabled);
            query.setParameter(2, website);
        } else {
            query = strategy.getNamedQuery("User.getByEnabled");
            query.setParameter(1, enabled);
        }
    } else {
        if (website != null) {
            query = strategy.getNamedQuery("User.getByPermissions.website");
            query.setParameter(1, website);
        } else {
            query = strategy.getNamedQuery("User.getAll");
        }
    }
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}