Example usage for javax.persistence Query setHint

List of usage examples for javax.persistence Query setHint

Introduction

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

Prototype

Query setHint(String hintName, Object value);

Source Link

Document

Set a query property or hint.

Usage

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

private void applyProperties(Query pQuery) {
    Set<Entry<String, Object>> entries = properties.entrySet();
    for (Entry<String, Object> entry : entries) {
        pQuery.setHint(entry.getKey(), entry.getValue());
    }/*from  w  w w .  ja  v  a  2 s  . co m*/
}

From source file:org.opentides.dao.impl.BaseEntityDaoJpaImpl.java

/**
 * {@inheritDoc}/*from www. ja va  2s.c o m*/
 */
@Override
@SuppressWarnings("unchecked")
public final List<T> findByNamedQuery(final String name, final Map<String, Object> params, int start,
        int total) {
    String queryString = getJpqlQuery(name);
    Query queryObject = getEntityManager().createQuery(queryString);
    if (params != null) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getKey() != null && entry.getKey().startsWith("hint.")) {
                queryObject.setHint(entry.getKey().substring(5), entry.getValue());
            } else {
                queryObject.setParameter(entry.getKey(), entry.getValue());
            }
        }
    }
    if (start > -1)
        queryObject.setFirstResult(start);
    if (total > -1)
        queryObject.setMaxResults(total);
    return queryObject.getResultList();
}

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

@Override
public List<Product> findProductByURI(String uri) {
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().findProductByURI(uri, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            return (List<Product>) holder.getResult();
        }/*from   ww  w  . j  a  v a  2s.  c o  m*/
    }
    String urlKey = uri.substring(uri.lastIndexOf('/'));
    Query query;

    query = em.createNamedQuery("BC_READ_PRODUCTS_BY_OUTGOING_URL");
    query.setParameter("url", uri);
    query.setParameter("urlKey", urlKey);
    query.setParameter("currentDate", getCurrentDateAfterFactoringInDateResolution());
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    @SuppressWarnings("unchecked")
    List<Product> results = query.getResultList();
    return results;
}

From source file:net.nan21.dnet.core.presenter.action.query.QueryBuilderWithJpql.java

private void createQuery_(Query q) throws Exception {
    if (this.getDescriptor().getQueryHints() != null) {
        Map<String, Object> queryHints = this.getDescriptor().getQueryHints();
        Iterator<String> it = queryHints.keySet().iterator();
        while (it.hasNext()) {
            String p = it.next();
            q.setHint(p, queryHints.get(p));
        }/*w ww .  jav a 2 s .  c  o  m*/
    }
    addFetchGroup(q);
    bindFilterParams(q);
}

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

@Override
public List<Product> findProductByURI(String uri) {
    if (extensionManager != null) {
        ExtensionResultHolder holder = new ExtensionResultHolder();
        ExtensionResultStatusType result = extensionManager.getProxy().findProductByURI(uri, holder);
        if (ExtensionResultStatusType.HANDLED.equals(result)) {
            return (List<Product>) holder.getResult();
        }//from w w  w . j  ava2  s  .c o  m
    }
    String urlKey = uri.substring(uri.lastIndexOf('/'));
    Query query;

    query = em.createNamedQuery("BC_READ_PRODUCTS_BY_OUTGOING_URL");
    query.setParameter("url", uri);
    query.setParameter("urlKey", urlKey);
    query.setParameter("currentDate",
            DateUtil.getCurrentDateAfterFactoringInDateResolution(cachedDate, currentDateResolution));
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    @SuppressWarnings("unchecked")
    List<Product> results = query.getResultList();
    return results;
}

From source file:KitchenManagement.FoodDemandForecastingAndPlanning.FoodDemandForecastingAndPlanningBean.java

@Override
public SaleForecastEntity getSalesForecast(Long storeId, Long menuItemId, Long scheduleId) {
    System.out.println("getSalesForecast is called");
    try {/*from   www  .j  a  v  a 2s  .  c  om*/
        Query q = em.createQuery(
                "select sf from SaleForecastEntity sf where sf.menuItem.id = ?1 AND sf.store.id = ?2 AND sf.schedule.id = ?3")
                .setParameter(1, menuItemId).setParameter(2, storeId).setParameter(3, scheduleId);
        q.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
        if (!q.getResultList().isEmpty()) {
            return (SaleForecastEntity) q.getResultList().get(0);
        } else {
            return this.getSalesForecastLinearRegression(storeId, menuItemId, scheduleId);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:de.micromata.genome.jpa.Emgr.java

/**
 * Set the the query to use select for update.
 *
 * @param query the query//  w ww.j a v a 2  s .c om
 * @param lockTimetimeInMs the lock timetime in ms
 */
@Override
public void setSelectForUpdate(Query query, int lockTimetimeInMs) {
    query.setHint(AvailableSettings.LOCK_TIMEOUT, lockTimetimeInMs);
    setQueryTimeout(query, lockTimetimeInMs);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
}

From source file:com.webbfontaine.valuewebb.action.rimm.RefSelect.java

public String getCountryName(String cod) {
    if (cod == null || cod.equals("")) {
        return null;
    }/*from   w w w.  j a v  a 2s .  co  m*/

    String sql = "select rimmCty from Country rimmCty where rimmCty.cod = :cod";
    Query query = Utils.getEntityManager().createQuery(sql);
    query.setHint("org.hibernate.cacheable", true);
    query.setParameter("cod", cod);

    try {
        Country country = (Country) query.getSingleResult();
        return country.getDsc();
    } catch (NoResultException e) {
        LOGGER.warn("No country found with given code '#0'", cod, e);
    } catch (Exception e) {
        LOGGER.error("Error searching country '#0'", cod, e);
    }
    return null;
}

From source file:com.webbfontaine.valuewebb.action.rimm.RefSelect.java

public MPRates getMPRates(Date date) {
    MPRates retVal = null;//from   ww  w. j  a  v a  2  s.c  o m

    Query query = Utils.getEntityManager().createNamedQuery("mpratesByDate");
    query.setHint("org.hibernate.cacheable", true);
    query.setParameter("date", date);

    try {
        retVal = (MPRates) query.getSingleResult();
    } catch (NoResultException e) {
        LOGGER.warn("No MPRates found with given date '#0'", date, e);
    } catch (Exception e) {
        LOGGER.error("Error searching MPRates '#0'", date, e);
    }
    return retVal;
}

From source file:com.webbfontaine.valuewebb.action.rimm.RefSelect.java

public List<SopVm> getSourceOfPriceVMCodePairs() {
    Query query = Utils.getEntityManager().createQuery("select sopVm from SopVm sopVm");
    query.setHint("org.hibernate.readOnly", Boolean.TRUE);
    query.setHint("org.hibernate.cacheable", true);
    return query.getResultList();
}