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.gettec.fsnip.fsn.dao.product.impl.ProductDAOImpl.java

/**
 * ???// w w  w  .  j  ava 2 s. c  om
 * @param page 
 * @param size 
 * @param organization 
 * @return
 * @throws DaoException 
 */
@SuppressWarnings("unchecked")
@Override
public List<Product> getAllLocalProduct(int page, int size, Long organization) throws DaoException {
    try {
        String sql = "SELECT DISTINCT p.* FROM product p " + "Inner JOIN business_unit b ON p.producer_id=b.id "
                + "LEFT JOIN t_meta_initialize_product t ON p.id=t.product_id AND t.organization=?1 "
                + "WHERE b.organization=?2 AND t.first_storage_id is NULL ";

        Query query = entityManager.createNativeQuery(sql.toString(), Product.class);
        query.setParameter(1, organization);
        query.setParameter(2, organization);
        if (page > 0) {
            query.setFirstResult((page - 1) * size);
            query.setMaxResults(size);
        }
        List<Product> result = query.getResultList();
        return result;
    } catch (Exception e) {
        throw new DaoException(
                "ProductDAOImpl.getAllLocalProduct() ???,?",
                e);
    }
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * //from  w  ww. j a va  2 s  .  c o  m
 * Metodo che restituisce tutta la collezione di oggetti specificati come
 * parametro, tramite l'elemento di inizio e il numero di elementi
 * desiderati (mettendo a 0 questo parametro li restituisce tutti)
 * 
 * @param classObj
 * @param query
 * @param map
 * @param firstItem
 * @param batchSize
 * @return
 * @throws java.lang.Exception
 */
public <T extends EntityBaseStandard> List<T> findAll(Class<T> classObj, int firstItem, int batchSize)
        throws Exception {
    List<T> returnValue = new ArrayList<T>(); // Non piu' null cos posso
    // semplificare il codice
    // del chiamante

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzia l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        /* Crea la query */
        q = em.createQuery("from " + classObj.getName());

        /*
         * Se il numero di elementi  diverso da 0 specifico quanti e da
         * dove cominciare
         */
        if (batchSize > 0) {
            q.setFirstResult(firstItem);
            q.setMaxResults(batchSize);
        }

        /* Calcolo la collezione di elementi desiderati */
        returnValue = q.getResultList();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ????????//from w  ww.jav  a2s .c  om
 * @param paramVO
 * @return
 * @throws DaoException
  */
@Override
public List<ProcurementInfo> getRawMaterialInfoList(TzQueryRequestParamVO paramVO, int type)
        throws DaoException {
    try {
        List<ProcurementInfo> list = new ArrayList<ProcurementInfo>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT p.* FROM procurement_info p ");
        sql.append("LEFT JOIN business_unit b ON b.organization=p.organization_id ");
        sql.append("WHERE b.`name`=?1 AND p.type=?2  ");
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            sql.append(" AND  p.name LIKE ?3  ");
        }
        sql.append(" ORDER BY p.procurement_date desc ");
        Query query = entityManager.createNativeQuery(sql.toString(), ProcurementInfo.class);
        query.setParameter(1, paramVO.getBusName());
        query.setParameter(2, type);
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            query.setParameter(3, "%" + paramVO.getProName() + "%");
        }
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        list = query.getResultList();
        return list;
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getRawMaterialInfoList()?????????", e);
    }
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ?id??????// w  ww  .  j  a v a  2 s.  c  o m
 * @param paramVO
 * @return
 * @throws DaoException
 */
@Override
public List<ProcurementDispose> getProcurementDisposeList(TzQueryRequestParamVO paramVO, int type)
        throws DaoException {
    try {
        List<ProcurementDispose> list = new ArrayList<ProcurementDispose>();
        StringBuffer sql = new StringBuffer();
        sql.append(
                "SELECT p.* FROM procurement_dispose p LEFT JOIN business_unit b ON b.organization=p.organization_id  WHERE b.name=?1 AND p.type=?2 ");
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            sql.append(" AND  p.procurement_name LIKE ?3  ");
        }
        sql.append(" ORDER BY dispose_date desc ");
        Query query = entityManager.createNativeQuery(sql.toString(), ProcurementDispose.class);
        query.setParameter(1, paramVO.getBusName());
        query.setParameter(2, type);
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            query.setParameter(3, "%" + paramVO.getProName() + "%");
        }
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        list = query.getResultList();
        return list;
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getProcurementDisposeList()?id???????",
                e);
    }
}

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

/**
 * @inheritDoc/*  w ww .j a va2s .c  om*/
 */
public List getHotWeblogs(int sinceDays, int offset, int length) throws WebloggerException {

    // figure out start date
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.DATE, -1 * sinceDays);
    Date startDate = cal.getTime();

    Query query = strategy.getNamedQuery(
            "WeblogHitCount.getByWeblogEnabledTrueAndActiveTrue&DailyHitsGreaterThenZero&WeblogLastModifiedGreaterOrderByDailyHitsDesc");
    query.setParameter(1, startDate);

    // Was commented out due to https://glassfish.dev.java.net/issues/show_bug.cgi?id=2084
    // TODO: determine if this is still an issue. Is it a problem with other JPA implementations?
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}

From source file:org.medici.bia.dao.image.ImageDAOJpaImpl.java

/**
 * {@inheritDoc}/*from w  w  w . j  a  va  2 s.  c o  m*/
 */
@Override
public Page searchImagesCreatedBefore(Date timeStamp, List<ImageType> types, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    String jpql = "FROM Image WHERE dateCreated <= :timeStamp";
    if (types != null && types.size() > 0) {
        jpql += " AND imageType IN (:types)";
    }

    Query query = null;

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + jpql;
        query = getEntityManager().createQuery(countQuery);
        query.setParameter("timeStamp", timeStamp);
        if (types != null && types.size() > 0) {
            query.setParameter("types", types);
        }
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    query = getEntityManager().createQuery(jpql + getOrderByQuery(paginationFilter.getSortingCriterias()));
    query.setParameter("timeStamp", timeStamp);
    if (types != null && types.size() > 0) {
        query.setParameter("types", types);
    }

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

    page.setList(query.getResultList());

    return page;
}

From source file:org.j2free.jpa.Controller.java

/**
 * /* ww w. j a v a  2  s. com*/
 * @param <T>
 * @param query
 * @param parameters
 * @return
 */
public <T> T query(Query query, KeyValuePair<String, ? extends Object>... parameters) {
    if (parameters != null) {
        for (KeyValuePair<String, ? extends Object> parameter : parameters) {
            query.setParameter(parameter.key, parameter.value);
        }
    }
    try {
        return (T) query.setMaxResults(1).getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:org.mingle.pear.persistence.dao.impl.GenericDaoImpl.java

/**
 * ??/*from ww w. java2s .  c  o m*/
 *
 * @param qt
 * @param clazz
 * @return
 */
protected <R> Query createQuery(QueryTemplate qt, Class<R> clazz) {
    Query query = null;
    if (qt instanceof JqlQueryTemplate) {
        if (clazz == null) {
            query = entityManager().createQuery(qt.getQuery());
        } else {
            query = entityManager().createQuery(qt.getQuery(), clazz);
        }
    }
    if (qt instanceof NamedQueryTemplate) {
        if (clazz == null) {
            query = entityManager().createNamedQuery(qt.getQuery());
        } else {
            query = entityManager().createNamedQuery(qt.getQuery(), clazz);
        }
    }
    if (qt instanceof SqlQueryTemplate) {
        query = entityManager().createNativeQuery(qt.getQuery());
    }
    if (qt instanceof SqlResultSetMappingQueryTemplate) {
        SqlResultSetMappingQueryTemplate sqt = (SqlResultSetMappingQueryTemplate) qt;
        query = entityManager().createNativeQuery(qt.getQuery(), sqt.getSqlResultMapping());
    }
    // ?
    if (qt.getParameters() != null) {
        for (Entry<String, ?> paramEntry : qt.getParameters().entrySet()) {
            query.setParameter(paramEntry.getKey(), paramEntry.getValue());
        }
    }
    // 
    if (qt.isCachable()) {
        query.setHint("org.hibernate.cacheable", true);
        if (StringUtils.isNotBlank(qt.getCacheRegion())) {
            query.setHint("org.hibernate.cacheRegion", qt.getCacheRegion());
        } else {
            query.setHint("org.hibernate.cacheRegion", clazz.getName());
        }
    }
    // 
    query.setFirstResult(qt.getFirstResult());
    query.setMaxResults(qt.getMaxResults());
    return query;
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ??????//  w ww . j a  va 2 s.  com
 * @param paramVO
 * @return
 * @throws DaoException
 */
@Override
public List<Member> getMemberInfoList(TzQueryRequestParamVO paramVO) throws DaoException {
    try {
        List<Member> list = new ArrayList<Member>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.* FROM member m LEFT JOIN business_unit b ON m.orgId=b.id ");
        sql.append("WHERE b.`name`=?1  ");
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            sql.append(" AND ( m.name LIKE ?2 OR m.position LIKE ?3 OR m.identificationNo LIKE ?4 )");
        }
        sql.append(" ORDER BY m.id desc  ");
        Query query = entityManager.createNativeQuery(sql.toString(), Member.class);
        query.setParameter(1, paramVO.getBusName());
        if (StringUtils.isNotBlank(paramVO.getProName())) {
            query.setParameter(2, "%" + paramVO.getProName() + "%");
            query.setParameter(3, "%" + paramVO.getProName() + "%");
            query.setParameter(4, "%" + paramVO.getProName() + "%");
        }
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        list = query.getResultList();
        return list;
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getMemberInfoList()???????", e);
    }
}

From source file:org.medici.bia.dao.userhistory.UserHistoryDAOJpaImpl.java

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

    if (paginationFilter.getTotal() == null) {
        String queryString = "SELECT count(user) FROM UserHistory WHERE user=:user and logicalDelete=false ";

        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("user", user);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    String objectsQuery = "FROM UserHistory WHERE user=:user and logicalDelete=false ";

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    String jpql = objectsQuery + getOrderByQuery(paginationFilter.getSortingCriterias());
    logger.debug("JPQL Query : " + jpql);

    Query query = getEntityManager().createQuery(jpql);
    query.setParameter("user", user);
    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());
    page.setList(query.getResultList());

    return page;
}