Example usage for javax.persistence TypedQuery setHint

List of usage examples for javax.persistence TypedQuery setHint

Introduction

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

Prototype

TypedQuery<X> setHint(String hintName, Object value);

Source Link

Document

Set a query property or hint.

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);//w w w  .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

@Override
public List<Product> readProductsByCategory(Long categoryId) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_CATEGORY", Product.class);
    query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId));
    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);/*from ww  w . j a  v  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<ProductBundle> readAutomaticProductBundles() {
    Date currentDate = DateUtil.getCurrentDateAfterFactoringInDateResolution(cachedDate, currentDateResolution);
    TypedQuery<ProductBundle> query = em.createNamedQuery("BC_READ_AUTOMATIC_PRODUCT_BUNDLES",
            ProductBundle.class);
    query.setParameter("currentDate", currentDate);
    query.setParameter("autoBundle", Boolean.TRUE);
    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.catalog.dao.ProductDaoImpl.java

protected List<Product> readAllActiveProductsInternal(Date currentDate) {
    CriteriaQuery<Product> criteria = getCriteriaForActiveProducts(currentDate);
    TypedQuery<Product> query = em.createQuery(criteria);
    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 Long readCountAllActiveProductsInternal(Date currentDate) {
    // Set up the criteria query that specifies we want to return a Long
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);

    // The root of our search is Product
    Root<ProductImpl> product = criteria.from(ProductImpl.class);

    // We need to filter on active date on the sku
    Join<Product, Sku> sku = product.join("defaultSku");

    // We want the count of products
    criteria.select(builder.count(product));

    // Ensure the product is currently active
    List<Predicate> restrictions = new ArrayList<Predicate>();
    attachActiveRestriction(currentDate, product, sku, restrictions);

    // Add the restrictions to the criteria query
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

    TypedQuery<Long> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getSingleResult();
}

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

@Override
public List<Order> readOrdersByIds(List<Long> orderIds) {
    if (orderIds == null || orderIds.size() == 0) {
        return null;
    }// w  w  w.ja v a 2  s .  c  o  m
    if (orderIds.size() > 100) {
        LOG.warn("Not recommended to use the readOrdersByIds method for long lists of orderIds, since "
                + "Hibernate is required to transform the distinct results. The list of requested"
                + "order ids was (" + orderIds.size() + ") in length.");
    }
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Order> criteria = builder.createQuery(Order.class);
    Root<OrderImpl> order = criteria.from(OrderImpl.class);
    criteria.select(order);

    // We only want results that match the order IDs
    criteria.where(order.get("id").as(Long.class).in(orderIds));

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

    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);/*  www.j a  va2  s .  co  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.broadleafcommerce.openadmin.server.security.dao.AdminPermissionDaoImpl.java

@Override
public AdminPermission readAdminPermissionByNameAndType(String name, String type) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<AdminPermission> criteria = builder.createQuery(AdminPermission.class);
    Root<AdminPermissionImpl> adminPerm = criteria.from(AdminPermissionImpl.class);
    criteria.select(adminPerm);//from   w  w  w. ja  v  a2s  . c  om

    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(adminPerm.get("name"), name));
    restrictions.add(builder.equal(adminPerm.get("type"), type));

    // Execute the query with the restrictions
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<AdminPermission> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<AdminPermission> results = query.getResultList();
    if (results == null || results.size() == 0) {
        return null;
    } else {
        return results.get(0);
    }
}