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.common.config.dao.SystemPropertiesDaoImpl.java

@Override
public SystemProperty readSystemPropertyByName(final String name) {
    return getCachedObject(SystemProperty.class, "blSystemPropertyNullCheckCache",
            "SYSTEM_PROPERTY_MISSING_CACHE_HIT_RATE", new PersistentRetrieval<SystemProperty>() {
                @Override//ww  w.  j  a v  a2  s . c o m
                public SystemProperty retrievePersistentObject() {
                    TypedQuery<SystemProperty> query = em.createNamedQuery("BC_READ_SYSTEM_PROPERTIES_BY_NAME",
                            SystemProperty.class);
                    query.setParameter("propertyName", name);
                    query.setHint(QueryHints.HINT_CACHEABLE, true);
                    List<SystemProperty> props = query.getResultList();
                    if (props != null && !props.isEmpty()) {
                        return props.get(0);
                    }
                    return null;
                }
            }, name, getSite());
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public List<Translation> readTranslations(TranslatedEntity entity, String entityId, String fieldName) {
    entityId = getUpdatedEntityId(entity, entityId);

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);

    criteria.select(translation);//from w ww. j a v  a 2 s  .com
    criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()),
            builder.equal(translation.get("entityId"), entityId),
            builder.equal(translation.get("fieldName"), fieldName));

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

    return query.getResultList();
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public Translation readTranslation(TranslatedEntity entity, String entityId, String fieldName,
        String localeCode) {// ww w.  ja v  a  2 s  . c o  m
    entityId = getUpdatedEntityId(entity, entityId);

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> translation = criteria.from(TranslationImpl.class);

    criteria.select(translation);
    criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()),
            builder.equal(translation.get("entityId"), entityId),
            builder.equal(translation.get("fieldName"), fieldName),
            builder.equal(translation.get("localeCode"), localeCode));
    TypedQuery<Translation> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<Translation> translations = query.getResultList();
    if (translations.size() > 1) {
        throw new IllegalStateException("Found multiple translations for: " + entity.getFriendlyType() + "|"
                + entityId + "|" + fieldName + "|" + localeCode);
    }
    if (!translations.isEmpty()) {
        return translations.get(0);
    }
    return null;
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public Long countTranslationEntries(TranslatedEntity entityType, ResultType stage) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = builder.createQuery(Long.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(builder.count(root));
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    try {/* ww w .ja  v  a  2s .  co m*/
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root,
                    restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

        TypedQuery<Long> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getSingleResult();
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public List<Translation> readAllTranslationEntries(TranslatedEntity entityType, ResultType stage) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(root);/*from w ww .  j a  va  2 s  .c o m*/
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    try {
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root,
                    restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

        TypedQuery<Translation> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        return query.getResultList();
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public Translation readTranslation(TranslatedEntity entityType, String entityId, String fieldName,
        String localeCode, String localeCountryCode, ResultType stage) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(root);/*from   www  .java2 s .c o  m*/
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    restrictions.add(builder.equal(root.get("entityId"), entityId));
    restrictions.add(builder.equal(root.get("fieldName"), fieldName));
    restrictions.add(builder.like(root.get("localeCode").as(String.class), localeCode + "%"));
    try {
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root,
                    restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

        TypedQuery<Translation> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List<Translation> translations = query.getResultList();

        if (!translations.isEmpty()) {
            return findBestTranslation(localeCountryCode, translations);
        } else {
            return null;
        }
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}

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

@Override
public List<Product> readProductsByIds(List<Long> productIds) {
    if (productIds == null || productIds.size() == 0) {
        return null;
    }//from  w ww. ja  v  a 2s.c  om
    if (productIds.size() > 100) {
        logger.warn("Not recommended to use the readProductsByIds method for long lists of productIds, since "
                + "Hibernate is required to transform the distinct results. The list of requested"
                + "product ids was (" + productIds.size() + ") in length.");
    }
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<ProductImpl> product = criteria.from(ProductImpl.class);

    FetchParent fetchParent = product.fetch("defaultSku", JoinType.LEFT);
    if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) {
        fetchParent.fetch("skuMedia", JoinType.LEFT);
    }
    criteria.select(product);

    // We only want results that match the product IDs
    criteria.where(product.get("id").as(Long.class).in(
            sandBoxHelper.mergeCloneIds(ProductImpl.class, productIds.toArray(new Long[productIds.size()]))));
    if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) {
        criteria.distinct(true);
    }

    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

@Override
public List<Product> readProductsByName(String searchName) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_NAME", Product.class);
    query.setParameter("name", searchName + '%');
    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> 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  .ja v  a 2  s.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

protected List<Product> readActiveProductsByCategoryInternal(Long categoryId, Date currentDate) {
    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.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}