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:com.bizintelapps.bugtracker.dao.impl.GenericDaoImpl.java

private List<T> executeNamedQueryReturnList(final String namedQuery, final PagingParams pagingParams,
        final Object... params) throws DataAccessException {
    Query query = entityManager.createNamedQuery(namedQuery);
    int i = 1;//from w w  w . j a va2  s. c  om
    for (Object param : params) {
        query.setParameter(i++, param);
    }
    if (pagingParams != null) {
        query.setFirstResult((int) pagingParams.getStart());
        query.setMaxResults(pagingParams.getMaxLimit());
    }
    try {
        List result = query.getResultList();
        return result;
    } catch (RuntimeException re) {
        log.debug(re);
        return null;
    }
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *//*www.  j  a  va 2 s . c  o m*/
@SuppressWarnings(value = "unchecked")
public SocialSiteActivity getLatestStatus(Profile profile) throws SocialSiteException {
    if (profile == null) {
        throw new SocialSiteException("profile is null");
    }
    Query query = strategy.getNamedQuery("Activity.getStatusByProfile");
    query.setParameter(1, profile);
    query.setParameter(2, SocialSiteActivity.STATUS);
    query.setFirstResult(0);
    query.setMaxResults(1);
    List<SocialSiteActivity> ac = (List<SocialSiteActivity>) query.getResultList();
    if ((ac != null) && (ac.size() > 0)) {
        return ac.get(0);
    } else {
        return null;
    }
}

From source file:eu.planets_project.tb.impl.persistency.ExperimentPersistencyImpl.java

@SuppressWarnings("unchecked")
public List<Experiment> getPagedExperiments(int firstRow, int numberOfRows, String sortField,
        boolean descending) {
    log.info("Searching for experiments that match: " + firstRow + "," + numberOfRows + " : " + sortField
            + " : " + descending);
    // Execute the query:
    Query query = manager.createQuery("FROM ExperimentImpl AS e " + createOrderBy(sortField, descending));
    query.setFirstResult(firstRow);
    query.setMaxResults(numberOfRows);/*from ww w.ja va 2s .  co  m*/
    List<Experiment> resultList = query.getResultList();
    log.info("Search complete.");
    return resultList;

    /*
    String propertyName;
    if( "experimenter".equals(sortField) ) {
    //            this.findExperiment(1).getExperimentSetup().getBasicProperties().getExperimenter();
    propertyName = "expSetup.basicProperties.sExpName";
    } else {
    //          this.findExperiment(1).getExperimentSetup().getBasicProperties().getExperimentName();
    propertyName = "expSetup.basicProperties.sExpName";
    }
    Order order;
    if( descending ) {
    //    query.setParameter("deasc", "descending");
    order = Order.asc(propertyName);
    } else {
    //    query.setParameter("deasc", "ascending");
    order = Order.desc(propertyName);
    }
            
    Session hibernateSession = null;
    if (manager.getDelegate() instanceof org.hibernate.ejb.HibernateEntityManager) {
    hibernateSession = ((org.hibernate.ejb.HibernateEntityManager) manager
            .getDelegate()).getSession();
    } else {
    hibernateSession = (org.hibernate.Session) manager.getDelegate();
    }
            
    return hibernateSession.createCriteria(ExperimentImpl.class)
    .addOrder( order )
    .setFirstResult(firstRow)
    .setMaxResults(numberOfRows)
    .list();
    */
}

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

/**
 * {@inheritDoc}//from  w w w .  j  a  v  a  2s.c  o m
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<PermissionGrant> getPermissionGrants(String gadgetDomain, int offset, int length)
        throws SocialSiteException {
    log.debug("gadgetDomain=" + gadgetDomain);
    Query query = strategy.getNamedQuery("PermissionGrant.getByGadgetDomain");
    query.setParameter(1, gadgetDomain);
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<PermissionGrant>) query.getResultList();
}

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

/**
 * {@inheritDoc}/*from  ww  w  .j av  a2s.c om*/
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<PermissionGrant> getPermissionGrants(Profile profile, int offset, int length)
        throws SocialSiteException {
    log.debug("profile=" + profile);
    Query query = strategy.getNamedQuery("PermissionGrant.getByProfileId");
    query.setParameter(1, profile.getId());
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<PermissionGrant>) query.getResultList();
}

From source file:org.springframework.integration.jpa.core.DefaultJpaOperations.java

@Override
public List<?> getResultListForClass(Class<?> entityClass, int firstResult, int maxNumberOfResults) {

    final String entityName = JpaUtils.getEntityName(entityManager, entityClass);
    final Query query = entityManager.createQuery("select x from " + entityName + " x", entityClass);
    if (firstResult > 0) {
        query.setFirstResult(firstResult);
    }/*w ww  .j a v  a 2 s .co  m*/
    if (maxNumberOfResults > 0) {
        query.setMaxResults(maxNumberOfResults);
    }

    return query.getResultList();

}

From source file:cz.muni.fi.mir.db.dao.impl.CanonicOutputDAOImpl.java

@Override
public SearchResponse<CanonicOutput> getCanonicOutputByAppRun(ApplicationRun applicationRun,
        Pagination pagination) {//from w  w w .  j  av a 2  s  . com
    SearchResponse<CanonicOutput> sr = new SearchResponse<>();
    if (pagination == null) {
        List<CanonicOutput> resultList = entityManager
                .createQuery("SELECT co FROM canonicOutput co WHERE co.applicationRun = :appRun",
                        CanonicOutput.class)
                .setParameter("appRun", applicationRun).getResultList();
        sr.setTotalResultSize(resultList.size());
        sr.setResults(resultList);

        return sr;
    } else {
        try {
            Query query = entityManager
                    .createQuery("SELECT co FROM canonicOutput co WHERE co.applicationRun = :appRun",
                            CanonicOutput.class)
                    .setParameter("appRun", applicationRun);
            sr.setTotalResultSize(query.getResultList().size());
            sr.setResults(query.setFirstResult(pagination.getPageSize() * (pagination.getPageNumber() - 1))
                    .setMaxResults(pagination.getPageSize()).getResultList());
        } catch (NoResultException nre) {
            logger.debug(nre);
        }

        return sr;
    }
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * find entity in the range/*w ww  . ja  va2  s. c  o m*/
 *
 * @param range
 * @return
 */
public List<T> findRange(int[] range) {
    EntityManager em = getEntityManager();
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java

/**
 * /*from  w  w  w .jav  a  2 s .  c  o m*/
 * @param firstResult
 * @param listMax
 * @param transactionType
 * @return
 */
public List getTransactions(int firstResult, int listMax, int type) throws Exception {
    Object identity = null;
    try {
        identity = MyEMFDatabase.createEntityManager(MyEMFDatabase.MAIN_EM);
        log.debug("Getting entity manager ......");
        EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);

        List resultList = null;
        StringBuffer q = new StringBuffer();
        q.append("FROM Transaction AS t ");
        q.append(" WHERE t.account.id = :accid");
        q.append(" AND t.type = :type");
        q.append(" ORDER by t.transactionDate DESC");
        Query query = em.createQuery(q.toString());
        query.setParameter("accid", organizationId);
        query.setParameter("type", type);
        query.setFirstResult(firstResult);
        query.setMaxResults(listMax);
        resultList = query.getResultList();
        return resultList;
    } finally {
        log.debug("Closing entity manager .....");
        MyEMFDatabase.closeEntityManager(identity, MyEMFDatabase.MAIN_EM);
    }

}

From source file:org.nuxeo.ecm.platform.audit.service.LogEntryProvider.java

public List<?> nativeQuery(String queryString, int pageNb, int pageSize) {
    Query query = em.createQuery(queryString);
    if (pageNb > 1) {
        query.setFirstResult((pageNb - 1) * pageSize);
    }/*w  ww  .  j a v  a 2  s  .  c o  m*/
    query.setMaxResults(pageSize);
    return doPublishIfEntries(query.getResultList());
}