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: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);
        query.setCacheRegion(cacheRegion);
    }/*from   w  w w  .  jav  a 2s. c  om*/
}

From source file:no.abmu.common.persistence.hibernate3.AbstractQueryFinderSpecification.java

License:Open Source License

public Query createQuery(Session session) {

    String queryString;/*  w w w .  j av  a  2 s . co m*/

    if (StringUtil.isEmpty(getOrderBy())) {
        queryString = getQueryString();
    } else {
        queryString = getQueryString() + getOrderBy();
    }

    if (logger.isDebugEnabled()) {
        logger.debug("QueryString: [" + queryString + "]");
    }

    Query query = session.createQuery(queryString);

    if (usePropertiesBean()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using propertiesBean: " + getPropertiesBean());
        }
        query.setProperties(getPropertiesBean());
    }

    if (isPaged()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Paging with firstElement " + getFirstElement() + ", and pageSize " + getPageSize());
        }
        query.setFirstResult(getFirstElement());
        query.setMaxResults(getPageSize());
    }

    if (isCaching()) {
        String cacheRegion = getCacheRegion();
        if (logger.isDebugEnabled()) {
            logger.debug("Caching is on in cacheRegion [" + cacheRegion + "]");
        }
        Query query2 = query.setCacheable(true).setCacheRegion(cacheRegion);
        return query2;
    }

    return query;
}

From source file:org.alfresco.repo.workflow.jbpm.JBPMEngine.java

License:Open Source License

@SuppressWarnings({ "unchecked", "cast" })
private void cacheVariablesNoBatch(Session session, List<Long> contextInstanceIds,
        Map<Long, TokenVariableMap> variablesCache) {
    Query query = session.getNamedQuery("org.alfresco.repo.workflow.cacheInstanceVariables");
    query.setParameterList("ids", contextInstanceIds);
    query.setCacheMode(CacheMode.PUT);/* ww w.j  a v  a  2  s  . c om*/
    query.setFlushMode(FlushMode.MANUAL);
    query.setCacheable(true);

    List<TokenVariableMap> results = (List<TokenVariableMap>) query.list();
    for (TokenVariableMap tokenVariableMap : results) {
        variablesCache.put(tokenVariableMap.getToken().getId(), tokenVariableMap);
    }
}

From source file:org.apache.ctakes.ytex.kernel.dao.KernelEvaluationDaoImpl.java

License:Apache License

public Double getKernel(KernelEvaluation kernelEvaluation, long instanceId1, long instanceId2) {
    long instanceId1s = instanceId1 <= instanceId2 ? instanceId1 : instanceId2;
    long instanceId2s = instanceId1 <= instanceId2 ? instanceId2 : instanceId1;
    Query q = this.getSessionFactory().getCurrentSession().getNamedQuery("getKernelEvaluation");
    q.setCacheable(true);
    q.setInteger("kernelEvaluationId", kernelEvaluation.getKernelEvaluationId());
    q.setLong("instanceId1", instanceId1s);
    q.setLong("instanceId2", instanceId2s);
    KernelEvaluationInstance g = (KernelEvaluationInstance) q.uniqueResult();
    if (g != null) {
        return g.getSimilarity();
    } else {//  w ww  .ja v  a  2s .  c  o m
        return null;
    }
}

From source file:org.apache.ctakes.ytex.umls.dao.UMLSDaoImpl.java

License:Apache License

@Override
public boolean isRXNORMCui(String cui) {
    Query q = sessionFactory.getCurrentSession().getNamedQuery("isRXNORMCui");
    q.setCacheable(true);
    q.setString("cui", cui);
    long count = ((Long) q.uniqueResult());
    return count > 0;
}

From source file:org.apache.fulcrum.security.hibernate.HibernateUserManagerImpl.java

License:Apache License

/**
 * Retrieve a user from persistent storage using username as the key.
 * /*  w w  w . j  av a2  s  .com*/
 * @param userName
 *            the name of the user.
 * @return an User object.
 * @exception UnknownEntityException
 *                if the user's account does not exist in the database.
 * @exception DataBackendException
 *                if there is a problem accessing the storage.
 */
public User getUser(String userName)
        throws UnknownEntityException, DataBackendException, EntityDisabledException {
    List users = null;
    try {
        Query query = getPersistenceHelper().retrieveSession()
                .createQuery("from " + getClassName() + " su where su.name=:name");
        query.setString("name", userName.toLowerCase());
        query.setCacheable(true);

        users = query.list();
    } catch (HibernateException e) {
        throw new DataBackendException("Error retriving user information", e);
    }
    if (users.size() > 1) {
        throw new DataBackendException("Multiple Users with same username '" + userName + "'");
    }
    if (users.size() == 1) {
        User user = (User) users.get(0);

        if (user.isDisabled()) {
            throw new EntityDisabledException("User is unavailable");
        }
        return user;
    }
    throw new UnknownEntityException("Unknown user '" + userName + "'");
}

From source file:org.apache.fulcrum.security.hibernate.HibernateUserManagerImpl.java

License:Apache License

/**
 * Retrieve a User object with specified id.
 * //from  w  ww. j a  v  a  2 s. com
 * @param id
 *            the id of the User.
 * @return an object representing the User with specified id.
 * @throws DataBackendException
 *             if there was an error accessing the data backend.
 * @throws UnknownEntityException
 *             if the user does not exist.
 */
public User getUserById(Object id)
        throws DataBackendException, UnknownEntityException, EntityDisabledException {

    User user = null;

    if (id != null) {
        try {
            Query query = getPersistenceHelper().retrieveSession()
                    .createQuery("from " + getClassName() + " su where su.id=:id");
            query.setLong("id", (Long) id);
            query.setCacheable(true);
            List users = query.list();
            if (users.size() == 0) {
                throw new UnknownEntityException("Could not find user by id " + id);
            }
            user = (User) users.get(0);

            if (user.isDisabled()) {
                throw new EntityDisabledException("User is unavailable");
            }
        } catch (HibernateException e) {
            throw new DataBackendException("Error retriving user information", e);
        }
    }
    return user;
}

From source file:org.apache.ignite.cache.hibernate.GridHibernateL2CacheSelfTest.java

License:Apache License

/**
 * @param accessType Cache access type./*from  w w w  .  j a  v a  2  s .c  o m*/
 * @throws Exception If failed.
 */
private void testQueryCache(AccessType accessType) throws Exception {
    createSessionFactories(accessType);

    try {
        Session ses = sesFactory1.openSession();

        try {
            Transaction tx = ses.beginTransaction();

            for (int i = 0; i < 5; i++)
                ses.save(new Entity(i, "name-" + i));

            tx.commit();
        } finally {
            ses.close();
        }

        // Run some queries.

        ses = sesFactory1.openSession();

        try {
            Query qry1 = ses.createQuery("from " + ENTITY_NAME + " where id > 2");

            qry1.setCacheable(true);

            assertEquals(2, qry1.list().size());

            Query qry2 = ses.createQuery("from " + ENTITY_NAME + " where name = 'name-0'");

            qry2.setCacheable(true);

            assertEquals(1, qry2.list().size());
        } finally {
            ses.close();
        }

        assertEquals(0, sesFactory1.getStatistics().getQueryCacheHitCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCacheMissCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCachePutCount());

        // Run queries using another SessionFactory.

        ses = sesFactory2.openSession();

        try {
            Query qry1 = ses.createQuery("from " + ENTITY_NAME + " where id > 2");

            qry1.setCacheable(true);

            assertEquals(2, qry1.list().size());

            Query qry2 = ses.createQuery("from " + ENTITY_NAME + " where name = 'name-0'");

            qry2.setCacheable(true);

            assertEquals(1, qry2.list().size());

            Query qry3 = ses.createQuery("from " + ENTITY_NAME + " where id > 1");

            qry3.setCacheable(true);

            assertEquals(3, qry3.list().size());
        } finally {
            ses.close();
        }

        assertEquals(2, sesFactory2.getStatistics().getQueryCacheHitCount());
        assertEquals(1, sesFactory2.getStatistics().getQueryCacheMissCount());
        assertEquals(1, sesFactory2.getStatistics().getQueryCachePutCount());

        // Update entity, it should invalidate query cache.

        ses = sesFactory1.openSession();

        try {
            Transaction tx = ses.beginTransaction();

            ses.save(new Entity(5, "name-5"));

            tx.commit();
        } finally {
            ses.close();
        }

        // Run queries.

        ses = sesFactory1.openSession();

        sesFactory1.getStatistics().clear();

        try {
            Query qry1 = ses.createQuery("from " + ENTITY_NAME + " where id > 2");

            qry1.setCacheable(true);

            assertEquals(3, qry1.list().size());

            Query qry2 = ses.createQuery("from " + ENTITY_NAME + " where name = 'name-0'");

            qry2.setCacheable(true);

            assertEquals(1, qry2.list().size());
        } finally {
            ses.close();
        }

        assertEquals(0, sesFactory1.getStatistics().getQueryCacheHitCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCacheMissCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCachePutCount());

        // Clear query cache using another SessionFactory.

        sesFactory2.getCache().evictDefaultQueryRegion();

        ses = sesFactory1.openSession();

        // Run queries again.

        sesFactory1.getStatistics().clear();

        try {
            Query qry1 = ses.createQuery("from " + ENTITY_NAME + " where id > 2");

            qry1.setCacheable(true);

            assertEquals(3, qry1.list().size());

            Query qry2 = ses.createQuery("from " + ENTITY_NAME + " where name = 'name-0'");

            qry2.setCacheable(true);

            assertEquals(1, qry2.list().size());
        } finally {
            ses.close();
        }

        assertEquals(0, sesFactory1.getStatistics().getQueryCacheHitCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCacheMissCount());
        assertEquals(2, sesFactory1.getStatistics().getQueryCachePutCount());
    } finally {
        cleanup();
    }
}

From source file:org.beanfuse.persist.hibernate.EntityDaoHibernate.java

License:Open Source License

public List searchNamedQuery(final String queryName, final Map params, boolean cacheable) {
    Query query = this.getSession().getNamedQuery(queryName);
    query.setCacheable(cacheable);
    return HibernateQuerySupport.setParameter(query, params).list();
}

From source file:org.beanfuse.persist.hibernate.EntityDaoHibernate.java

License:Open Source License

public List searchHQLQuery(String hql, final Map params, boolean cacheable) {
    Query query = this.getSession().createQuery(hql);
    query.setCacheable(cacheable);
    return HibernateQuerySupport.setParameter(query, params).list();
}