Example usage for org.hibernate Criteria setCacheable

List of usage examples for org.hibernate Criteria setCacheable

Introduction

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

Prototype

public Criteria setCacheable(boolean cacheable);

Source Link

Document

Enable caching of this query result, provided query caching is enabled for the underlying session factory.

Usage

From source file:org.projectforge.core.BaseDao.java

License:Open Source License

private void setCacheRegion(final Criteria criteria) {
    criteria.setCacheable(true);
    if (useOwnCriteriaCacheRegion() == false) {
        return;//from www.  ja v  a  2 s  . c om
    }
    criteria.setCacheRegion(this.getClass().getName());
}

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

License:Apache License

/**
 * Prepare the given Criteria object, applying cache settings and/or
 * a transaction timeout.//from  www  . j av a2 s . c o  m
 * @param criteria the Criteria object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
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());
    }
    SessionFactoryUtils.applyTransactionTimeout(criteria, getSessionFactory());
}

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

License:Apache License

@Test
public void testExecuteWithCacheQueries() 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(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setCacheable(true)).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override/*from ww  w  . j a  v  a  2s  . c o m*/
        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 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/*  w  w w  .  java2s  .c  o m*/
        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 testLoadAllWithCacheable() 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.list()).willReturn(list);

    hibernateTemplate.setCacheQueries(true);
    List result = hibernateTemplate.loadAll(TestBean.class);
    assertTrue("Correct result", result == list);
    verify(criteria).setCacheable(true);
    verify(session).flush();/*from  ww w .  j av  a 2s. c  om*/
    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();/*from w w  w.  jav  a 2 s  .co 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./*w w  w.j a v  a  2s .  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 testExecuteWithCacheQueries() {
    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(session.getNamedQuery("some query name")).willReturn(query2);
    given(query2.setCacheable(true)).willReturn(query2);
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.execute(new HibernateCallback<Object>() {
        @Override//from  www  .ja v a  2 s.com
        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 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.j av a2 s.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 testLoadAllWithCacheable() {
    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.list()).willReturn(list);

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