Example usage for org.hibernate Query setCacheRegion

List of usage examples for org.hibernate Query setCacheRegion

Introduction

In this page you can find the example usage for org.hibernate Query setCacheRegion.

Prototype

Query<R> setCacheRegion(String cacheRegion);

Source Link

Document

Set the name of the cache region where query results should be cached (if cached at all).

Usage

From source file:nl.strohalm.cyclos.utils.hibernate.HibernateQueryHandler.java

License:Open Source License

private void setCacheRegion(final Query query, final String cacheRegion) {
    if (cacheRegion != null) {
        query.setCacheable(true);//  w w w . j a  v  a 2  s.  c om
        query.setCacheRegion(cacheRegion);
    }
}

From source file:org.codehaus.grepo.query.hibernate.generator.QueryGeneratorBase.java

License:Apache License

protected void applyCachingSetting(HibernateQueryOptions queryOptions, HibernateQueryExecutionContext context,
        Query query) {
    if (HibernateGeneratorUtils.isCachingEnabled(queryOptions, context)) {
        query.setCacheable(true);/*w w  w .  j av a2 s  . c  o  m*/
        String cacheRegion = HibernateGeneratorUtils.getCacheRegion(queryOptions, context);
        if (cacheRegion != null) {
            query.setCacheRegion(cacheRegion);
        }
    }
}

From source file:org.devproof.portal.core.module.common.repository.DataProviderRepositoryImpl.java

License:Apache License

private void handleCacheConfiguration(Query q, CacheQuery cacheAnnotation) {
    q.setCacheable(cacheAnnotation.enabled());
    if (!"".equals(cacheAnnotation.region())) {
        q.setCacheMode(CacheMode.parse(cacheAnnotation.cacheMode()));
    }//from   www . j a  v  a  2  s  .c  om
    if (!"".equals(cacheAnnotation.region())) {
        q.setCacheRegion(cacheAnnotation.region());
    }
}

From source file:org.hyperic.hq.measurement.server.session.MeasurementDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
List<Measurement> findAvailMeasurementsByInstances(int type, Integer[] ids) {
    boolean checkIds = (ids != null && ids.length > 0);
    String sql = new StringBuilder().append("select m from Measurement m ").append("join m.template t ")
            .append("join t.monitorableType mt ").append("where mt.appdefType = :type and ")
            .append("m.resource is not null and ").append((checkIds ? "m.instanceId in (:ids) and " : ""))
            .append(ALIAS_CLAUSE).toString();

    Query q = getSession().createQuery(sql).setInteger("type", type);

    if (checkIds) {
        q.setParameterList("ids", ids);
    }/*from  www. ja v a  2 s.c  o  m*/

    q.setCacheable(true);
    q.setCacheRegion("Measurement.findAvailMeasurementsByInstances");
    return q.list();
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.AccountDAO.java

License:LGPL

public int getCountForBranch(String branch, boolean useRegion) throws Exception {
    return withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session
                .createQuery("select account from Account as account where account.branch = :branch");
        query.setString("branch", branch);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }//  w  ww  . j a  v a  2s  . c o m
        query.setCacheable(true);
        return query.list().size();
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.AccountDAO.java

License:LGPL

public String getBranch(Object holder, boolean useRegion) throws Exception {
    return withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session
                .createQuery("select account.branch from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);/*from   w  ww.  j  a v a2 s. c o m*/
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return (String) query.list().get(0);
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.AccountDAO.java

License:LGPL

public int getTotalBalance(AccountHolder holder, boolean useRegion) throws Exception {
    List results = (List) withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session
                .createQuery("select account.balance from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);/* www .j a  v  a2 s  .  c  o  m*/
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return query.list();
    });
    int total = 0;
    if (results != null) {
        for (Iterator it = results.iterator(); it.hasNext();) {
            total += ((Integer) it.next()).intValue();
            System.out.println("Total = " + total);
        }
    }
    return total;
}

From source file:org.inwiss.platform.persistence.hibernate.core.BaseDAOHibernate.java

License:Apache License

/**
 * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
  * with additional query info; with arguments and their types
 *
 * @param hql         Query to execute//from  w ww.  j a v a  2 s .  co m
 * @param queryInfo   Object with additional information for query, currently offset and limit
 * @param args        Arguments to add to query. If <code>null</code>, nothing will be added.
 * @param types       Types of arguments. If <code>null</code>, Hibernate will determine types by itself.
 * @param cacheable   <code>true</code> if the query is cacheable
 * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
 * @return List of found entities
 * @see org.springframework.orm.hibernate3.HibernateCallback
  * @see com.blandware.atleap.common.util.QueryInfo
 */
protected List executeFind(final String hql, final QueryInfo queryInfo, final Object[] args, final Type[] types,
        final boolean cacheable, final String cacheRegion) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            Query query = session.createQuery(hql);
            query.setCacheable(cacheable);
            if (cacheRegion != null) {
                query.setCacheRegion(cacheRegion);
            }
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    Object arg = args[i];
                    Type type = null;
                    if (types != null && i < types.length) {
                        type = types[i];
                    }
                    if (type == null) {
                        query.setParameter(i, arg);
                    } else {
                        query.setParameter(i, arg, type);
                    }
                }
            }
            if (queryInfo != null) {
                if (queryInfo.getLimit() != null) {
                    query.setMaxResults(queryInfo.getLimit().intValue());
                }
                if (queryInfo.getOffset() != null) {
                    query.setFirstResult(queryInfo.getOffset().intValue());
                }
            }
            return query.list();
        }
    });
}

From source file:org.jpos.gl.GLSession.java

License:Open Source License

private void addRules(Map<String, Object> ruleMap, Journal journal, List acctHierarchy, int offset)
        throws HibernateException, GLException {
    Query q = session.createQuery(
            "from org.jpos.gl.RuleInfo where journal=:journal and account in (:accts) order by id");
    q.setParameter("journal", journal);
    q.setParameterList("accts", acctHierarchy, new LongType());
    q.setCacheable(true);/* w  w w .j av  a 2s.c o m*/
    q.setCacheRegion("rules");
    Iterator iter = q.iterate();

    while (iter.hasNext()) {
        RuleInfo ri = (RuleInfo) iter.next();
        RuleEntry k = new RuleEntry(ri, ri.getAccount());
        RuleEntry re = (RuleEntry) ruleMap.get(k.getKey());
        if (re == null)
            ruleMap.put(k.getKey(), re = k);

        re.addOffset(offset);
    }
}

From source file:org.sakaiproject.component.common.manager.TypeManagerImpl.java

License:Educational Community License

public Type getType(final String uuid) {
    if (uuid == null || uuid.length() < 1) {
        throw new IllegalArgumentException("uuid");
    }//from ww  w.j a v  a 2 s  . c  o m

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FIND_TYPE_BY_UUID);
            q.setString(UUID, uuid);
            q.setCacheable(cacheFindTypeByUuid);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}