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.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/*w  w  w  .j  a v  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 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/*  www .  j  a v  a2 s. co 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 testFindWithCacheable() {
    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.list()).willReturn(list);
    hibernateTemplate.setCacheQueries(true);
    List result = hibernateTemplate.find("some query string");
    assertTrue("Correct list", result == list);
    verify(query).setCacheable(true);/*www. j  av  a 2  s . c o m*/
}

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 va2s .  c o m
    verify(query).setCacheRegion("myCacheRegion");
}

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

License:Apache License

@Test
public void testFindByNamedQueryWithCacheable() {
    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.list()).willReturn(list);
    hibernateTemplate.setCacheQueries(true);
    List result = hibernateTemplate.findByNamedQuery("some query name");
    assertTrue("Correct list", result == list);
    verify(query).setCacheable(true);/*w w w .j  a v  a  2 s.  c om*/
}

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

License:Apache License

@Test
public void testFindByNamedQueryWithCacheableAndCacheRegion() {
    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 w w w .  j av  a2  s  .  c  om*/
    verify(query).setCacheRegion("myCacheRegion");
}

From source file:org.tolweb.dao.PageDAOImpl.java

public Collection getNodeIdsWithPages(Collection nodeIds) {
    Query query = getSession().createQuery(
            "select p.mappedNode.nodeId from org.tolweb.hibernate.MappedPage p where p.mappedNode.nodeId "
                    + StringUtils.returnSqlCollectionString(nodeIds));
    query.setCacheable(true);
    List list = query.list();//from  w ww.j a va  2s .  c  om
    return list;
}

From source file:org.tolweb.dao.PageDAOImpl.java

public MappedNode getNodeForPageNodeIsOn(MappedNode nd) {
    Long id = nd.getPageId();//from   w  w w.  ja v  a 2 s .  com
    List list;
    try {
        Session session = getSession();
        Query pageQuery = session.createQuery(
                "select n from org.tolweb.hibernate.MappedNode n, org.tolweb.hibernate.MappedPage p where p.pageId="
                        + id + " and n.nodeId=p.mappedNode.nodeId");
        pageQuery.setCacheable(true);
        list = pageQuery.list();
    } catch (Exception e) {
        list = new ArrayList();
        e.printStackTrace();
    }
    if (list != null && list.size() > 0) {
        return (MappedNode) list.get(0);
    } else {
        return null;
    }
}

From source file:org.tolweb.dao.PageDAOImpl.java

protected Object getPageForNode(Long nodeId, boolean onlyId) {
    String idString = onlyId ? "select p.pageId " : "";
    List list;/*  w w w. j a v  a2  s .c om*/
    try {
        Session session = getSession();
        Query pageQuery = session.createQuery(
                idString + "from org.tolweb.hibernate.MappedPage p where p.mappedNode.nodeId=" + nodeId);
        pageQuery.setCacheable(!getIsWorking());
        list = pageQuery.list();
    } catch (Exception e) {
        list = new ArrayList();
        e.printStackTrace();
    }
    if (list != null && list.size() > 0) {
        return list.get(0);
    } else {
        return null;
    }
}

From source file:org.tolweb.dao.PageDAOImpl.java

public List<Long> getTillusVersionIds() {
    String queryString = "select t.versionId from org.tolweb.hibernate.MappedPage p join p.titleIllustrations as t";
    Query query = getSession().createQuery(queryString);
    query.setCacheable(true);
    return query.list();
}