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.cgiar.dapa.ccafs.tpe.dao.impl.PhenologyGrowthDao.java

private List<Float> getOutliers(Float lowerLimit, Float upperLimit, Integer country, Integer cultivar,
        String year, Integer env) {
    StringBuffer q = new StringBuffer("select r.wrr14 from " + entityClass.getName())
            .append(" r where r.region.id =:country").append(" or r.region.parent.parent.id =:country")
            .append(" and r.cultivar.id =:cultivar").append(" and r.environment.id =:environment")
            .append(" and r.year =:year").append(" and r.wrr14 <:lower").append(" or r.wrr14 >:upper")
            .append(" order by r.wrr14 asc");

    Query query = entityManager.createQuery(q.toString());

    query.setParameter("country", country);
    query.setParameter("cultivar", cultivar);
    query.setParameter("year", year);
    query.setParameter("environment", env);
    query.setParameter("lower", lowerLimit);
    query.setParameter("upper", upperLimit);

    query.setFirstResult(0);
    query.setMaxResults(4);//from w w w.  j  a  v  a 2  s.c  om

    List<Float> results = query.getResultList();

    return results;

}

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

/**
 * {@inheritDoc}/*from w  w w  .  ja v  a  2 s  . co  m*/
 */
public List<MediaFile> searchMediaFiles(Weblog weblog, MediaFileFilter filter) throws WebloggerException {

    List<Object> params = new ArrayList<Object>();
    int size = 0;
    StringBuilder queryString = new StringBuilder();
    StringBuilder whereClause = new StringBuilder();
    StringBuilder orderBy = new StringBuilder();

    queryString.append("SELECT m FROM MediaFile m WHERE ");

    params.add(size++, weblog);
    whereClause.append("m.directory.weblog = ?").append(size);

    if (!StringUtils.isEmpty(filter.getName())) {
        String nameFilter = filter.getName();
        nameFilter = nameFilter.trim();
        if (!nameFilter.endsWith("%")) {
            nameFilter = nameFilter + "%";
        }
        params.add(size++, nameFilter);
        whereClause.append(" AND m.name like ?").append(size);
    }

    if (filter.getSize() > 0) {
        params.add(size++, filter.getSize());
        whereClause.append(" AND m.length ");
        switch (filter.getSizeFilterType()) {
        case GT:
            whereClause.append(">");
            break;
        case GTE:
            whereClause.append(">=");
            break;
        case EQ:
            whereClause.append("=");
            break;
        case LT:
            whereClause.append("<");
            break;
        case LTE:
            whereClause.append("<=");
            break;
        default:
            whereClause.append("=");
            break;
        }
        whereClause.append(" ?").append(size);
    }

    if (filter.getTags() != null && filter.getTags().size() > 1) {
        whereClause.append(" AND EXISTS (SELECT t FROM MediaFileTag t WHERE t.mediaFile = m and t.name IN (");
        for (String tag : filter.getTags()) {
            params.add(size++, tag);
            whereClause.append("?").append(size).append(",");
        }
        whereClause.deleteCharAt(whereClause.lastIndexOf(","));
        whereClause.append("))");
    } else if (filter.getTags() != null && filter.getTags().size() == 1) {
        params.add(size++, filter.getTags().get(0));
        whereClause.append(" AND EXISTS (SELECT t FROM MediaFileTag t WHERE t.mediaFile = m and t.name = ?")
                .append(size).append(")");
    }

    if (filter.getType() != null) {
        if (filter.getType() == MediaFileType.OTHERS) {
            for (MediaFileType type : MediaFileType.values()) {
                if (type != MediaFileType.OTHERS) {
                    params.add(size++, type.getContentTypePrefix() + "%");
                    whereClause.append(" AND m.contentType not like ?").append(size);
                }
            }
        } else {
            params.add(size++, filter.getType().getContentTypePrefix() + "%");
            whereClause.append(" AND m.contentType like ?").append(size);
        }
    }

    if (filter.getOrder() != null) {
        switch (filter.getOrder()) {
        case NAME:
            orderBy.append(" order by m.name");
            break;
        case DATE_UPLOADED:
            orderBy.append(" order by m.dateUploaded");
            break;
        case TYPE:
            orderBy.append(" order by m.contentType");
            break;
        default:
        }
    } else {
        orderBy.append(" order by m.name");
    }

    Query query = strategy
            .getDynamicQuery(queryString.toString() + whereClause.toString() + orderBy.toString());
    for (int i = 0; i < params.size(); i++) {
        query.setParameter(i + 1, params.get(i));
    }

    if (filter.getStartIndex() >= 0) {
        query.setFirstResult(filter.getStartIndex());
        query.setMaxResults(filter.getLength());
    }
    return query.getResultList();
}

From source file:portal.api.impl.PortalJpaController.java

@SuppressWarnings("unchecked")
public List<PortalUser> readUsers(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM PortalUser m");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);//from www .j  av a 2s  .  c  om
    return q.getResultList();
}

From source file:portal.api.impl.PortalJpaController.java

@SuppressWarnings("unchecked")
public List<InstalledVxF> readInstalledVxFs(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM InstalledVxF m");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);//from w ww.  j  av a 2 s  .  c  o m
    return q.getResultList();
}

From source file:io.isoft.system.service.SysUserServiceImpl.java

@Override
public MyFilterResult getSysUsers(MyFilter filter) {
    // TODO Auto-generated method stub
    try {//from   ww  w.j  a  va 2  s  .  com
        FilterBuilder filterBuilder = new FilterBuilder();

        String hql = " from SysUser e ";
        if (filter.arguments.size() > 0)
            hql += " WHERE " + filterBuilder.buildHql(filter, "e");

        //???count?;
        Query countQuery = entityManager.createQuery("select count(*) " + hql);
        if (filter.sorts.size() == 0)
            hql += " ORDER BY e.autoId";
        else {
            //?;
            hql += " ORDER BY ";
            int sortIndex = 0;
            for (MyFilterSort myFilterSort : filter.sorts) {
                hql += String.format("  e.%s %s %s", myFilterSort.sortColumn,
                        myFilterSort.isAscending ? "" : "DESC",
                        (++sortIndex == filter.sorts.size()) ? "" : ",");
            }

        }
        //??
        Query query = entityManager.createQuery(hql);

        //?queryquery?
        for (ObjectParameter parameter : filterBuilder.parameters) {
            if (parameter.fieldName.equals("amount")) {
                //blazeDs value?int?, ? big decimal
                parameter.value = new BigDecimal((Integer) parameter.value);
                //               parameter.value = (String)parameter.value;
            }
            if (parameter.fieldName.equals("tradePrice")) {
                parameter.value = new Double(parameter.value.toString());
            }

            query.setParameter(parameter.name, parameter.value);
            countQuery.setParameter(parameter.name, parameter.value);
        }
        Object o = countQuery.getSingleResult();
        int recordCount = Integer.parseInt(o.toString());
        int pageStart = (filter.pageIndex) * filter.pageSize;
        List<StaffDict> results = query.setFirstResult(pageStart).setMaxResults(filter.pageSize)
                .getResultList();

        return new MyFilterResult((ArrayList<StaffDict>) results, recordCount);
    } catch (RuntimeException re) {
        throw re;
    }
}

From source file:portal.api.impl.PortalJpaController.java

public List<MANOplatform> readMANOplatforms(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM MANOplatform m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*  www . ja v a2  s  .c o m*/
    return q.getResultList();
}

From source file:portal.api.impl.PortalJpaController.java

public List<MANOprovider> readMANOproviders(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM MANOprovider m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*from   w w w .  ja  v a 2s .c o  m*/
    return q.getResultList();
}

From source file:net.sf.gazpachoquest.qbe.NamedQueryUtil.java

public <T> List<T> findByNamedQuery(final SearchParameters sp) {
    if (sp == null || !sp.hasNamedQuery()) {
        throw new IllegalArgumentException("searchParameters must be non null and must have a namedQuery");
    }//w w w. j  ava 2  s . co  m

    Query query = entityManager.createNamedQuery(sp.getNamedQuery());
    String queryString = getQueryString(query);

    // append order by if needed
    if (queryString != null && sp.hasOrders()) {
        // create the sql restriction clausis
        StringBuilder orderClausis = new StringBuilder("order by ");
        boolean first = true;
        for (OrderBy orderBy : sp.getOrders()) {
            if (!first) {
                orderClausis.append(", ");
            }
            orderClausis.append(orderBy.getColumn());
            orderClausis.append(orderBy.isOrderDesc() ? " desc" : " asc");
            first = false;
        }

        if (logger.isDebugEnabled()) {
            logger.debug("appending: [" + orderClausis.toString() + "] to " + queryString);
        }

        query = recreateQuery(query, queryString + " " + orderClausis.toString());
    }

    // pagination
    if (sp.getFirstResult() >= 0) {
        query.setFirstResult(sp.getFirstResult());
    }
    if (sp.getMaxResults() > 0) {
        query.setMaxResults(sp.getMaxResults());
    }

    // named parameters
    setQueryParameters(query, sp);

    // execute
    @SuppressWarnings("unchecked")
    List<T> result = query.getResultList();

    if (result != null && logger.isDebugEnabled()) {
        logger.debug(sp.getNamedQuery() + " returned a List of size: " + result.size());
    }

    return result;
}

From source file:portal.api.impl.PortalJpaController.java

public List<Infrastructure> readInfrastructures(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM Infrastructure m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*w  ww  .  j  av a 2s  . c  om*/
    return q.getResultList();
}

From source file:portal.api.impl.PortalJpaController.java

public List<PortalProperty> readProperties(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM PortalProperty m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*from   w  w  w . ja va2s .co  m*/
    return q.getResultList();

}