Example usage for javax.persistence TypedQuery setFirstResult

List of usage examples for javax.persistence TypedQuery setFirstResult

Introduction

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

Prototype

TypedQuery<X> setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

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);
    query.setMaxResults(limit);/* w  ww  . j av  a2s  .co m*/
    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);
    query.setMaxResults(limit);/*  www  .j  ava  2  s. c  o  m*/
    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

protected List<Product> readAllActiveProductsInternal(int page, int pageSize, Date currentDate) {
    CriteriaQuery<Product> criteria = getCriteriaForActiveProducts(currentDate);
    int firstResult = page * pageSize;
    TypedQuery<Product> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.setFirstResult(firstResult).setMaxResults(pageSize).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 w  w  w.  ja  va 2  s. 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.dragoneronca.nlp.wol.domain.WolEntityIterator.java

@SuppressWarnings("unchecked")
private boolean retrieveResults() {
    if (ended) {//w w w. ja v a 2  s  .  com
        return false;
    } else if (!results.isEmpty()) {
        return true;
    }

    WolDomainContext domainContext = WolDomainContext.getInstance();
    EntityManager entityManager = domainContext.getEntityManager();
    if (clear) {
        entityManager.clear();
    }
    TypedQuery<T> q = entityManager.createNamedQuery(name, tClass);

    if (parameters != null) {
        int i = 1;
        for (Object parameter : parameters) {
            q.setParameter(i++, parameter);
        }
    }
    q.setFirstResult(offset).setMaxResults(max);

    long startQueryTime = System.currentTimeMillis();
    if (logEnabled) {
        LOG.info("Starting query: " + name + "\t\tType: " + tClass.getSimpleName());
    }

    Runtime.getRuntime().gc();
    List<T> resultList = q.getResultList();

    if (!resultList.isEmpty()) {
        results.addAll(resultList);
        offset += results.size();

        long endQueryTime = System.currentTimeMillis();
        if (logEnabled) {
            LOG.info("Retrieved " + results.size() + " results in " + ((endQueryTime - startQueryTime) / 1000)
                    + " sec.");
        }

        return true;
    } else {
        ended = true;
        return false;
    }
}

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

/**
 * Builds the Typed Query// w w  w .j  a v a  2s .com
 * @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;//from  w  w w  .  j  av  a2 s. c o  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   ww  w  .  java  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.BusinessObjectDefinitionDaoImpl.java

@Override
public List<BusinessObjectDefinitionEntity> getAllBusinessObjectDefinitions(Integer startPosition,
        Integer maxResult) {/*from  w  ww.  j  ava2 s .c  o  m*/
    // Create the criteria builder and a tuple style criteria query.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BusinessObjectDefinitionEntity> criteria = builder
            .createQuery(BusinessObjectDefinitionEntity.class);

    // The criteria root is the business object definition.
    Root<BusinessObjectDefinitionEntity> businessObjectDefinitionEntityRoot = criteria
            .from(BusinessObjectDefinitionEntity.class);

    // Join to the other tables we can filter on.
    Join<BusinessObjectDefinitionEntity, NamespaceEntity> namespaceEntity = businessObjectDefinitionEntityRoot
            .join(BusinessObjectDefinitionEntity_.namespace);

    // Get the columns.
    Path<String> namespaceCodeColumn = namespaceEntity.get(NamespaceEntity_.code);
    Path<String> businessObjectDefinitionNameColumn = businessObjectDefinitionEntityRoot
            .get(BusinessObjectDefinitionEntity_.name);

    // Add all clauses to the query.
    criteria.select(businessObjectDefinitionEntityRoot).orderBy(builder.asc(businessObjectDefinitionNameColumn),
            builder.asc(namespaceCodeColumn));

    // Get an instance of the query ready for execution.
    TypedQuery<BusinessObjectDefinitionEntity> query = entityManager.createQuery(criteria);

    // If start position is specified, set it for the query.
    if (startPosition != null) {
        query.setFirstResult(startPosition.intValue());
    }

    // If start position is specified, set it for the query.
    if (maxResult != null) {
        query.setMaxResults(maxResult.intValue());
    }

    // Execute the query and return the results.
    return query.getResultList();
}

From source file:org.keycloak.models.jpa.JpaRealmProvider.java

@Override
public List<GroupModel> searchForGroupByName(RealmModel realm, String search, Integer first, Integer max) {
    TypedQuery<String> query = em.createNamedQuery("getGroupIdsByNameContaining", String.class)
            .setParameter("realm", realm.getId()).setParameter("search", search);
    if (Objects.nonNull(first) && Objects.nonNull(max)) {
        query = query.setFirstResult(first).setMaxResults(max);
    }/*from  w ww.j a  v a 2s .  co  m*/
    List<String> groups = query.getResultList();
    if (Objects.isNull(groups))
        return Collections.EMPTY_LIST;
    List<GroupModel> list = new ArrayList<>();
    for (String id : groups) {
        GroupModel groupById = session.realms().getGroupById(id, realm);
        while (Objects.nonNull(groupById.getParentId())) {
            groupById = session.realms().getGroupById(groupById.getParentId(), realm);
        }
        if (!list.contains(groupById)) {
            list.add(groupById);
        }
    }
    list.sort(Comparator.comparing(GroupModel::getName));

    return Collections.unmodifiableList(list);
}