Example usage for org.hibernate Criteria setCacheRegion

List of usage examples for org.hibernate Criteria setCacheRegion

Introduction

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

Prototype

public Criteria setCacheRegion(String cacheRegion);

Source Link

Document

Set the name of the cache region to use for query result caching.

Usage

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testExecuteWithCacheQueriesAndCacheRegion() throws HibernateException {
    Query query1 = mock(Query.class);
    Query query2 = mock(Query.class);
    Criteria criteria = mock(Criteria.class);
    given(session.createQuery("some query")).willReturn(query1);
    given(query1.setCacheable(true)).willReturn(query1);
    given(query1.setCacheRegion("myRegion")).willReturn(query1);
    given(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setCacheable(true)).willReturn(query2);
    given(query2.setCacheRegion("myRegion")).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);
    given(criteria.setCacheRegion("myRegion")).willReturn(criteria);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myRegion");
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override/*from  w ww .j a  v  a  2 s .  c om*/
        public Object doInHibernate(org.hibernate.Session sess) throws HibernateException {
            assertNotSame(session, sess);
            assertTrue(Proxy.isProxyClass(sess.getClass()));
            sess.createQuery("some query");
            sess.getNamedQuery("some query name");
            sess.createCriteria(TestBean.class);
            // should be ignored
            sess.close();
            return null;
        }
    });

    verify(session).flush();
    verify(session).close();
}

From source file:org.springframework.orm.hibernate3.HibernateTemplateTests.java

License:Apache License

@Test
public void testLoadAllWithCacheableAndCacheRegion() throws HibernateException {
    Criteria criteria = mock(Criteria.class);
    List list = new ArrayList();
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);
    given(criteria.setCacheRegion("myCacheRegion")).willReturn(criteria);
    given(criteria.list()).willReturn(list);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.loadAll(TestBean.class);
    assertTrue("Correct result", result == list);
    verify(criteria).setCacheable(true);
    verify(criteria).setCacheRegion("myCacheRegion");
    verify(session).flush();// w ww . jav a  2s  .  c o m
    verify(session).close();
}

From source file:org.springframework.orm.hibernate4.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout./*ww w. j  a va2  s .c o  m*/
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareCriteria(Criteria criteria) {
    if (isCacheQueries()) {
        criteria.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            criteria.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        criteria.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        criteria.setMaxResults(getMaxResults());
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(getSessionFactory());
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

From source file:org.springframework.orm.hibernate4.HibernateTemplateTests.java

License:Apache License

@Test
public void testExecuteWithCacheQueriesAndCacheRegion() {
    Query query1 = mock(Query.class);
    Query query2 = mock(Query.class);
    Criteria criteria = mock(Criteria.class);
    given(session.createQuery("some query")).willReturn(query1);
    given(query1.setCacheable(true)).willReturn(query1);
    given(query1.setCacheRegion("myRegion")).willReturn(query1);
    given(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setCacheable(true)).willReturn(query2);
    given(query2.setCacheRegion("myRegion")).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);
    given(criteria.setCacheRegion("myRegion")).willReturn(criteria);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myRegion");
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override//from  ww w  . ja  va  2s . c  om
        public Object doInHibernate(Session sess) {
            assertNotSame(session, sess);
            assertTrue(Proxy.isProxyClass(sess.getClass()));
            sess.createQuery("some query");
            sess.getNamedQuery("some query name");
            sess.createCriteria(TestBean.class);
            // should be ignored
            sess.close();
            return null;
        }
    });
}

From source file:org.springframework.orm.hibernate4.HibernateTemplateTests.java

License:Apache License

@Test
public void testLoadAllWithCacheableAndCacheRegion() {
    Criteria criteria = mock(Criteria.class);
    List list = new ArrayList();
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);
    given(criteria.setCacheRegion("myCacheRegion")).willReturn(criteria);
    given(criteria.list()).willReturn(list);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.loadAll(TestBean.class);
    assertTrue("Correct result", result == list);
    verify(criteria).setCacheable(true);
    verify(criteria).setCacheRegion("myCacheRegion");
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> List<E> findByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setCacheable(query.isCached());

    //Issue #21/*from  w w w.  j  a v  a2s  . c  om*/
    criteria.setFlushMode(FlushMode.MANUAL);

    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }
    return criteria.list();
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findFirstByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(1).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }/*  ww w  .j  a  va2s . co  m*/

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() > 0) {
        return list.get(0);
    }
    return null;
}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E extends Entity> E findUniqueByQuery(Query query) {
    Criteria criteria = toDetachedCriteria(query).getExecutableCriteria(sessionFactory.getCurrentSession())
            .setMaxResults(2).setCacheable(query.isCached());
    if (StringUtils.hasText(query.getCacheRegion())) {
        criteria.setCacheRegion(query.getCacheRegion());
    }// www.  j  av a2  s.  c o  m

    //Issue #21
    criteria.setFlushMode(FlushMode.MANUAL);

    final List<E> list = criteria.list();
    if (list.size() == 0) {
        return null;
    } else if (list.size() == 1) {
        return list.get(0);
    } else {
        throw new DataIntegrityViolationException("expecting unique result but got many");
    }
}

From source file:org.workin.persistence.hibernate.v4.dao.Hibernate4DaoSupport.java

License:Apache License

/**
 * @description ??Criteria//from w  w  w  . ja  v a 2  s.  c  o  m
 * @author <a href="mailto:code727@gmail.com">?</a> 
 * @param criteria
 */
protected void prepareCriteria(Criteria criteria) {
    if (this.cacheConfiguration != null) {
        if (this.cacheConfiguration.isCacheQueries()) {
            criteria.setCacheable(true);
            if (StringUtils.isNotBlank(this.cacheConfiguration.getQueryCacheRegion())) {
                criteria.setCacheRegion(this.cacheConfiguration.getQueryCacheRegion());
            }
        }

        if (this.cacheConfiguration.getFetchSize() > 0)
            criteria.setFetchSize(this.cacheConfiguration.getFetchSize());

        if (this.cacheConfiguration.getMaxResults() > 0)
            criteria.setMaxResults(this.cacheConfiguration.getMaxResults());
    }
}