Example usage for org.hibernate Query setCacheable

List of usage examples for org.hibernate Query setCacheable

Introduction

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

Prototype

Query<R> setCacheable(boolean cacheable);

Source Link

Document

Enable/disable second level query (result) caching for this query.

Usage

From source file:org.inbio.m3s.dao.core.impl.ProjectDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Project> findAllByPartialNamePaginated(final String partialName, final int maxResults) {
    logger.debug("findAllByPartialNamePaginated with[" + partialName + "]");
    HibernateTemplate template = getHibernateTemplate();
    return (List<Project>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session.createQuery("select p from Project as p" + " where p.name like :projectName");
            query.setParameter("projectName", partialName);
            query.setFirstResult(0);//from w  w w. jav  a  2 s  . c o  m
            query.setMaxResults(maxResults);
            query.setCacheable(true);
            return query.list();
        }
    });
}

From source file:org.inbio.neoportal.core.dao.impl.GenericSearchDAOImpl.java

License:Open Source License

/**
 * Returns the total of elements that could be retrived using that search
 * criteria// w ww  .  jav a  2s .  c  o  m
 * 
 * @return number of results
 */
public Integer getTotalResults(final String HSQL) throws IllegalArgumentException {

    logger.debug("query: " + HSQL);
    HibernateTemplate template = getHibernateTemplate();

    return (Integer) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session.createQuery(HSQL);
            query.setCacheable(false);
            logger.debug("result: " + new Integer(query.uniqueResult().toString()));

            return new Integer(query.uniqueResult().toString());
        }
    });

}

From source file:org.inbio.neoportal.core.dao.impl.GenericSearchDAOImpl.java

License:Open Source License

/**
 * /*from w ww.jav a  2 s . c  om*/
 * @param searchCriteria
 * @param first
 * @param last
 * @return
 * 
 */
@SuppressWarnings("unchecked")
public List<Integer> getResults(final String HSQL, final int first, final int last)
        throws IllegalArgumentException {

    logger.debug("query getResults: " + HSQL);
    HibernateTemplate template = getHibernateTemplate();

    return (List<Integer>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session.createQuery(HSQL);
            //query.setParameter(0, nomenclaturalGroupId);
            query.setCacheable(false);
            query.setFirstResult(first - 1);
            query.setMaxResults(last - (first - 1));
            return query.list();
        }
    });
}

From source file:org.inbio.neoportal.core.dao.impl.OccurrenceDAOImpl.java

License:Open Source License

@Override
public List<String> getSexValues() {
    Session session = getSessionFactory().getCurrentSession();
    Query query = session.createQuery("select distinct oc.sex from OccurrenceDwc as oc");
    query.setCacheable(true);
    return query.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");
        }//  ww  w.  j  av a 2s. co  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 ava  2s  .co 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);//from  w w w . j av a  2s .  co  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//  w  w  w.  j  ava  2s. c om
 * @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.jahia.services.workflow.jbpm.JahiaProcessDefinitionQueryImpl.java

License:Open Source License

@Override
protected void applyParameters(Query query) {
    super.applyParameters(query); //To change body of overridden methods use File | Settings | File Templates.
    query.setCacheable(true);
}

From source file:org.jahia.services.workflow.jbpm.JahiaRepositorySessionImpl.java

License:Open Source License

@Override
public DeploymentProperty findDeploymentPropertyByProcessDefinitionId(String processDefinitionId) {
    Query query = session.createQuery("select deploymentProperty " + "from "
            + DeploymentProperty.class.getName() + " as deploymentProperty "
            + "where deploymentProperty.key = '" + DeploymentImpl.KEY_PROCESS_DEFINITION_ID + "' "
            + "  and deploymentProperty.stringValue = '" + processDefinitionId + "' ");
    query.setCacheable(true);
    query.setMaxResults(1);/*from   w w  w .  j  a  v  a  2 s . c  om*/
    DeploymentProperty deploymentProperty = (DeploymentProperty) query.uniqueResult();
    return deploymentProperty;
}