Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setMaxResults.

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAUserDAO.java

@Override
public List<User> findAll(final int page, final int itemsPerPage) {
    TypedQuery<User> query = entityManager()
            .createQuery("SELECT e FROM  " + JPAUser.class.getSimpleName() + " e ORDER BY e.id", User.class);
    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));
    query.setMaxResults(itemsPerPage);

    return query.getResultList();
}

From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java

@Override
public List<Product> readProductsByName(@Nonnull String searchName, @Nonnull int limit, @Nonnull int offset) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_NAME", Product.class);
    query.setParameter("name", searchName + '%');
    query.setFirstResult(offset);/*from   www  .  j  a  va 2 s.co  m*/
    query.setMaxResults(limit);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}

From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java

public List<Product> readActiveProductsByCategoryInternal(Long categoryId, Date currentDate, int limit,
        int offset) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_ACTIVE_PRODUCTS_BY_CATEGORY", Product.class);
    query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId));
    query.setParameter("currentDate", currentDate);
    query.setFirstResult(offset);/*from  w ww  . jav  a 2s . c  o m*/
    query.setMaxResults(limit);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}

From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java

@Override
public List<Product> readProductsByCategory(Long categoryId, int limit, int offset) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_CATEGORY", Product.class);
    query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId));
    query.setFirstResult(offset);//  www .  j ava2s . c  o m
    query.setMaxResults(limit);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}

From source file:org.broadleafcommerce.core.order.dao.OrderDaoImpl.java

@Override
public List<Order> readBatchOrders(int start, int pageSize, List<OrderStatus> statuses) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
    Root<OrderImpl> order = criteria.from(OrderImpl.class);
    criteria.select(order);//from  ww  w. j av a  2s . c  o  m

    if (CollectionUtils.isNotEmpty(statuses)) {
        // We only want results that match the orders with the correct status
        ArrayList<String> statusStrings = new ArrayList<String>();
        for (OrderStatus status : statuses) {
            statusStrings.add(status.getType());
        }
        criteria.where(order.get("status").as(String.class).in(statusStrings));
    }

    TypedQuery<Order> query = em.createQuery(criteria);
    query.setFirstResult(start);
    query.setMaxResults(pageSize);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order");

    return query.getResultList();
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

/**
 * Builds the Typed Query//from  ww  w .j  ava2  s  .  co m
 * @return
 */
public TypedQuery<Connection> build() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        if (Relationship.Type.PENDING.equals(this.status)) {
            predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types));
        } else {
            predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
        }
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

public TypedQuery<Connection> buildLastConnections() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);

    Predicate predicate = null;/*w w w . j a v  a 2  s .co  m*/
    //owner
    if (this.owner != null) {
        predicate = cb.equal(connection.get(Connection_.senderId), owner.getId());
    }
    //status
    if (this.status != null) {
        predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status));
    }

    CriteriaQuery<Connection> select = criteria.select(connection).distinct(true);
    select.where(predicate);
    select.orderBy(cb.desc(connection.<Long>get(Connection_.id)));

    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }

    return typedQuery;
}

From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java

public TypedQuery<Connection> buildFilter() {
    EntityManager em = EntityManagerHolder.get();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class);
    Root<Connection> connection = criteria.from(Connection.class);
    Join<Connection, Profile> receiver = connection.join(Connection_.receiver);
    ///*from   w  w w.  j  a  v  a  2  s.  c  om*/
    CriteriaQuery<Connection> select = criteria.select(connection);
    select.where(buildPredicateFilter(cb, receiver, connection));
    select.orderBy(cb.asc(receiver.get(Profile_.fullName)));
    //
    TypedQuery<Connection> typedQuery = em.createQuery(select);
    if (this.limit > 0) {
        typedQuery.setFirstResult((int) offset);
        typedQuery.setMaxResults((int) limit);
    }
    //
    return typedQuery;
}

From source file:org.finra.herd.dao.impl.BusinessObjectDataDaoImpl.java

@Override
public List<BusinessObjectDataKey> getBusinessObjectDataByBusinessObjectDefinition(
        BusinessObjectDefinitionEntity businessObjectDefinitionEntity, Integer maxResults) {
    // Get ids for all business object formats registered with the specified business object definition.
    List<Integer> businessObjectFormatIds = businessObjectFormatDao
            .getBusinessObjectFormatIdsByBusinessObjectDefinition(businessObjectDefinitionEntity);

    // Return no results if the business object definition has no business object formats registered with it.
    if (CollectionUtils.isEmpty(businessObjectFormatIds)) {
        return new ArrayList<>();
    }// w w  w .j ava  2s  . c  om

    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class);

    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate predicate = getPredicateForInClause(builder,
            businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.businessObjectFormatId),
            businessObjectFormatIds);

    // Build an order by clause.
    List<Order> orderBy = new ArrayList<>();
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) {
        orderBy.add(builder.desc(businessObjectDataEntityRoot.get(businessObjectDataPartition)));
    }
    orderBy.add(builder.desc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.version)));

    // Add the clauses for the query.
    criteria.select(businessObjectDataEntityRoot).where(predicate).orderBy(orderBy);

    // Create a query.
    TypedQuery<BusinessObjectDataEntity> query = entityManager.createQuery(criteria);

    // If specified, set the maximum number of results for query to return.
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }

    // Run the query to get a list of entities back.
    List<BusinessObjectDataEntity> businessObjectDataEntities = query.getResultList();

    // Populate the "keys" objects from the returned entities.
    List<BusinessObjectDataKey> businessObjectDataKeys = new ArrayList<>();
    for (BusinessObjectDataEntity businessObjectDataEntity : businessObjectDataEntities) {
        BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey();
        businessObjectDataKeys.add(businessObjectDataKey);
        businessObjectDataKey.setNamespace(businessObjectDataEntity.getBusinessObjectFormat()
                .getBusinessObjectDefinition().getNamespace().getCode());
        businessObjectDataKey.setBusinessObjectDefinitionName(
                businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectDefinition().getName());
        businessObjectDataKey
                .setBusinessObjectFormatUsage(businessObjectDataEntity.getBusinessObjectFormat().getUsage());
        businessObjectDataKey.setBusinessObjectFormatFileType(
                businessObjectDataEntity.getBusinessObjectFormat().getFileType().getCode());
        businessObjectDataKey.setBusinessObjectFormatVersion(
                businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectFormatVersion());
        businessObjectDataKey.setPartitionValue(businessObjectDataEntity.getPartitionValue());
        businessObjectDataKey.setSubPartitionValues(getSubPartitionValues(businessObjectDataEntity));
        businessObjectDataKey.setBusinessObjectDataVersion(businessObjectDataEntity.getVersion());
    }

    return businessObjectDataKeys;
}

From source file:org.finra.herd.dao.impl.BusinessObjectDataDaoImpl.java

@Override
public List<BusinessObjectDataKey> getBusinessObjectDataByBusinessObjectFormat(
        BusinessObjectFormatEntity businessObjectFormatEntity, Integer maxResults) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDataEntity> criteria = builder.createQuery(BusinessObjectDataEntity.class);

    // The criteria root is the business object data.
    Root<BusinessObjectDataEntity> businessObjectDataEntityRoot = criteria.from(BusinessObjectDataEntity.class);

    // Create the standard restrictions (i.e. the standard where clauses).
    Predicate predicate = builder.equal(
            businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.businessObjectFormat),
            businessObjectFormatEntity);

    // Build the order by clause. The sort order is consistent with the search business object data implementation.
    List<Order> orderBy = new ArrayList<>();
    for (SingularAttribute<BusinessObjectDataEntity, String> businessObjectDataPartition : BUSINESS_OBJECT_DATA_PARTITIONS) {
        orderBy.add(builder.desc(businessObjectDataEntityRoot.get(businessObjectDataPartition)));
    }/* w ww . ja  va  2  s .c o m*/
    orderBy.add(builder.desc(businessObjectDataEntityRoot.get(BusinessObjectDataEntity_.version)));

    // Add the clauses for the query.
    criteria.select(businessObjectDataEntityRoot).where(predicate).orderBy(orderBy);

    // Create a query.
    TypedQuery<BusinessObjectDataEntity> query = entityManager.createQuery(criteria);

    // If specified, set the maximum number of results for query to return.
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }

    // Run the query to get a list of entities back.
    List<BusinessObjectDataEntity> businessObjectDataEntities = query.getResultList();

    // Populate the "keys" objects from the returned entities.
    List<BusinessObjectDataKey> businessObjectDataKeys = new ArrayList<>();
    for (BusinessObjectDataEntity businessObjectDataEntity : businessObjectDataEntities) {
        BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey();
        businessObjectDataKeys.add(businessObjectDataKey);
        businessObjectDataKey.setNamespace(businessObjectDataEntity.getBusinessObjectFormat()
                .getBusinessObjectDefinition().getNamespace().getCode());
        businessObjectDataKey.setBusinessObjectDefinitionName(
                businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectDefinition().getName());
        businessObjectDataKey
                .setBusinessObjectFormatUsage(businessObjectDataEntity.getBusinessObjectFormat().getUsage());
        businessObjectDataKey.setBusinessObjectFormatFileType(
                businessObjectDataEntity.getBusinessObjectFormat().getFileType().getCode());
        businessObjectDataKey.setBusinessObjectFormatVersion(
                businessObjectDataEntity.getBusinessObjectFormat().getBusinessObjectFormatVersion());
        businessObjectDataKey.setPartitionValue(businessObjectDataEntity.getPartitionValue());
        businessObjectDataKey.setSubPartitionValues(getSubPartitionValues(businessObjectDataEntity));
        businessObjectDataKey.setBusinessObjectDataVersion(businessObjectDataEntity.getVersion());
    }

    return businessObjectDataKeys;
}