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:org.sakaiproject.component.common.manager.TypeManagerImpl.java

License:Educational Community License

public Type getType(final String authority, final String domain, final String keyword) {
    // validation
    if (authority == null || authority.length() < 1)
        throw new IllegalArgumentException("authority");
    if (domain == null || domain.length() < 1)
        throw new IllegalArgumentException("domain");
    if (keyword == null || keyword.length() < 1)
        throw new IllegalArgumentException("keyword");

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FIND_TYPE_BY_TUPLE);
            q.setString(AUTHORITY, authority);
            q.setString(DOMAIN, domain);
            q.setString(KEYWORD, keyword);
            q.setCacheable(cacheFindTypeByTuple);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }/*from  www  . j  a  va  2s .c o m*/
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

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

License:Educational Community License

/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String)
 *///from  w ww.  jav  a 2s . co  m
public Type getType(final String uuid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getType(String " + uuid + ")");
    }
    if (uuid == null || uuid.length() < 1) {
        throw new IllegalArgumentException("uuid");
    }

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

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

License:Educational Community License

/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String, java.lang.String, java.lang.String)
 *//*  w  ww. java 2s. c om*/
public Type getType(final String authority, final String domain, final String keyword) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getType(String " + authority + ", String " + domain + ", String " + keyword + ")");
    }
    // validation
    if (authority == null || authority.length() < 1)
        throw new IllegalArgumentException("authority");
    if (domain == null || domain.length() < 1)
        throw new IllegalArgumentException("domain");
    if (keyword == null || keyword.length() < 1)
        throw new IllegalArgumentException("keyword");

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FINDTYPEBYTUPLE);
            q.setString(AUTHORITY, authority);
            q.setString(DOMAIN, domain);
            q.setString(KEYWORD, keyword);
            q.setCacheable(cacheFindTypeByTuple);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

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

License:Apache License

/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout.//from  w ww . ja  v  a  2  s  .  co m
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }
    SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory());
}

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  w w. j a v  a2s . co  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 testFindWithCacheableAndCacheRegion() throws HibernateException {
    Query query = mock(Query.class);
    List list = new ArrayList();
    given(session.createQuery("some query string")).willReturn(query);
    given(query.setCacheable(true)).willReturn(query);
    given(query.setCacheRegion("myCacheRegion")).willReturn(query);
    given(query.list()).willReturn(list);
    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.find("some query string");
    assertTrue("Correct list", result == list);
    verify(query).setCacheable(true);/*from   w  w w .j  a v a  2  s  . c  om*/
    verify(query).setCacheRegion("myCacheRegion");
    verify(session).flush();
    verify(session).close();
}

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

License:Apache License

@Test
public void testFindByNamedQueryWithCacheableAndCacheRegion() throws HibernateException {
    Query query = mock(Query.class);
    List list = new ArrayList();
    given(session.getNamedQuery("some query name")).willReturn(query);
    given(query.setCacheable(true)).willReturn(query);
    given(query.setCacheRegion("myCacheRegion")).willReturn(query);
    given(query.list()).willReturn(list);
    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.findByNamedQuery("some query name");
    assertTrue("Correct list", result == list);
    verify(query).setCacheable(true);/*from   ww w  .  j ava  2s .  c  om*/
    verify(query).setCacheRegion("myCacheRegion");
    verify(session).flush();
    verify(session).close();
}

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

License:Apache License

/**
 * Prepare the given Query object, applying cache settings and/or
 * a transaction timeout./*  w  w w  .j  a  v a  2s.co m*/
 * @param queryObject the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 */
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(getSessionFactory());
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        queryObject.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   w w w .  java 2  s .c o  m*/
        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 testFindWithCacheableAndCacheRegion() {
    Query query = mock(Query.class);
    List list = new ArrayList();
    given(session.createQuery("some query string")).willReturn(query);
    given(query.setCacheable(true)).willReturn(query);
    given(query.setCacheRegion("myCacheRegion")).willReturn(query);
    given(query.list()).willReturn(list);
    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.find("some query string");
    assertTrue("Correct list", result == list);
    verify(query).setCacheable(true);/*from  ww  w .j  a  va  2s  .  co  m*/
    verify(query).setCacheRegion("myCacheRegion");
}