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:io.isoft.blood.service.RdServiceImpl.java

@Override
public MyFilterResult findByFilter(MyFilter filter) {
    try {/*from   w  ww . j  a  v a  2 s .  c  o  m*/
        FilterBuilder filterBuilder = new FilterBuilder();

        String hql = " from BloodRdsMaster 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<BloodRdsMaster> results = query.setFirstResult(pageStart).setMaxResults(filter.pageSize)
                .getResultList();

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

From source file:es.alvsanand.webpage.db.dao.DAOHelper.java

protected Query getQuery(EntityManager entityManager, String queryName, QueryBean queryBean) {
    StringBuffer queryString = new StringBuffer(queryName);

    Query q = null;

    if (queryBean != null) {
        if (queryBean.getOrderBeans() != null) {
            for (int i = 0; i < queryBean.getOrderBeans().size(); i++) {
                OrderBean orderBean = queryBean.getOrderBeans().get(i);

                if (StringUtils.isEmpty(orderBean.getName())) {
                    continue;
                }// ww  w .  j  a v a  2  s .  c om

                if (i == 0) {
                    queryString.append(ORDERBY_SPQL_TEXT);
                }

                queryString.append(orderBean.getName());
                queryString.append((orderBean.isAscendingOrder()) ? ASC_SPQL_TEXT : DES_SPQL_TEXT);
                if (i + 1 < queryBean.getOrderBeans().size()) {
                    queryString.append(", ");
                }
            }
        }

        q = entityManager.createQuery(queryString.toString());

        if (queryBean.getLimit() > 0) {
            q.setMaxResults(queryBean.getLimit());
        }
        if (queryBean.getOffset() > 0) {
            q.setFirstResult(queryBean.getOffset());
        }
    } else {
        q = entityManager.createQuery(queryName);
    }

    return q;
}

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

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

    Query q = entityManager.createQuery("SELECT m FROM VxFOnBoardedDescriptor m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);//from www.j  ava 2s  . c o  m
    return q.getResultList();
}

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

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

    Query q = entityManager.createQuery("SELECT m FROM ExperimentOnBoardDescriptors m  ORDER BY m.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*from  w  w w . j  a  v  a2  s  . com*/
    return q.getResultList();
}

From source file:io.isoft.reg.service.StaffDictServiceImpl.java

@Override
public MyFilterResult findByFilter(MyFilter filter) {
    try {//  w  w w.  j ava2 s  .  c o  m
        FilterBuilder filterBuilder = new FilterBuilder();

        String hql = " from StaffDict 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:fi.vm.sade.organisaatio.dao.impl.OrganisaatioDAOImpl.java

public List<Organisaatio> findOrganisaatioByNimiLike(String organisaatioNimi, int firstResult, int maxResults) {
    LOG.debug("findOrganisaatioByNimiLike()");
    Query query = getEntityManager().createQuery(
            "SELECT o FROM Organisaatio o WHERE UPPER(o.nimiFi) LIKE :orgnimi OR UPPER(o.nimiSv) LIKE :orgnimi "
                    + "OR UPPER(o.nimiEn) LIKE :orgnimi");
    query.setParameter("orgnimi", "%" + organisaatioNimi.toUpperCase() + "%");
    query.setFirstResult(firstResult);
    query.setMaxResults((maxResults <= 100) ? maxResults + 1 : 101);
    return query.getResultList();
}

From source file:org.tolven.app.bean.DataExtractBean.java

/**
 * Perform the query defined by the supplied DataQueryResults object.
 * @param dq//from   w ww. j av  a2  s. c  o  m
 */
protected void getMDQueryResults(DataQueryResults dq, boolean countOnly) {
    Map<String, Object> params = new HashMap<String, Object>();
    String queryString = setupQueryString(dq, countOnly, params);
    try {
        Query query;
        try {
            query = em.createQuery(queryString);
            logger.debug(queryString);
        } catch (Exception e) {
            throw new RuntimeException("Error constructing item query " + queryString, e);
        }
        if (!countOnly) {
            query.setFirstResult(dq.getOffset());
        }
        if (!countOnly && dq.getLimit() >= 0) {
            query.setMaxResults(dq.getLimit());
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            logger.debug(entry.getKey() + "=" + entry.getValue());
            query.setParameter(entry.getKey(), entry.getValue());
        }
        List<Object> results = query.getResultList();
        dq.setCount(results.size());
        dq.setIterator(results.iterator());
    } catch (RuntimeException e) {
        throw new RuntimeException("Error executing query " + queryString, e);
    }
}

From source file:de.iai.ilcd.model.dao.DataSetDao.java

private List<T> getDatasetsFromNonRootDs(IDataStockMetaData dataStock, int startIndex, int pageSize) {
    final String dsField = this.getDataStockField();
    StringBuilder sb = new StringBuilder("SELECT b FROM DataStock a LEFT JOIN a.").append(dsField)
            .append(" b WHERE a.id=:dsId");

    EntityManager em = PersistenceUtil.getEntityManager();
    Query query = em.createQuery(sb.toString());

    query.setParameter("dsId", dataStock.getId());
    query.setFirstResult(startIndex);
    query.setMaxResults(pageSize);//ww  w .jav a  2 s  .c o  m

    return query.getResultList();
}

From source file:org.vulpe.model.dao.impl.jpa.VulpeBaseDAOJPA.java

public Paging<ENTITY> paging(final ENTITY entity, final Integer pageSize, final Integer page)
        throws VulpeApplicationException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Paging object: ".concat(entity.toString()));
    }/*from  w w  w  . j av  a  2  s .  c  om*/
    final QueryConfiguration queryConfiguration = getQueryConfiguration(entity);
    if (queryConfiguration != null && !queryConfiguration.disableFilters()) {
        enableFilters();
    }
    final Map<String, Object> params = new HashMap<String, Object>();
    final String hql = getHQL(entity, params);

    // getting total records
    StringBuilder hqlCount = new StringBuilder();
    hqlCount.append("select count(*) from ");
    hqlCount.append(entity.getClass().getSimpleName()).append(" objc where objc.id in (");
    hqlCount.append("select distinct obj.id ");
    String hqlString = hql.substring(hql.toLowerCase().indexOf("from"));
    if (hqlString.contains("order by")) {
        hqlString = hqlString.substring(0, hqlString.toLowerCase().indexOf("order by"));
    }
    hqlCount.append(hqlString);
    hqlCount.append(")");
    Query query = getEntityManager().createQuery(hqlCount.toString());
    setParams(query, params);
    final Long size = (Long) query.getSingleResult();

    final Paging paging = new Paging<ENTITY>(size.intValue(), pageSize, page);
    if (size.longValue() > 0) {
        // getting list by size of page
        query = getEntityManager().createQuery(hql);
        setParams(query, params);
        query.setFirstResult(paging.getFromIndex());
        query.setMaxResults(pageSize);
        final List<ENTITY> entities = query.getResultList();
        for (final ENTITY entity2 : entities) {
            entity2.map(entity.map());
        }
        loadRelationships(entities, params, false);
        paging.setList(entities);
    }
    disableFilters();
    return paging;
}

From source file:de.iai.ilcd.model.dao.DataSetDao.java

private List<T> getDatasetsFromRootDs(IDataStockMetaData dataStock, int startIndex, int pageSize) {
    EntityManager em = PersistenceUtil.getEntityManager();
    Query query = em.createQuery("SELECT a FROM " + this.getJpaName() + " a WHERE a.rootDataStock.id=:dsId");

    query.setParameter("dsId", dataStock.getId());
    query.setFirstResult(startIndex);
    query.setMaxResults(pageSize);/*from w  w  w.jav  a 2s  .  co m*/

    return query.getResultList();
}