List of usage examples for org.hibernate Query setCacheable
Query<R> setCacheable(boolean cacheable);
From source file:org.sakaiproject.tool.assessment.facade.QuestionPoolFacadeQueries.java
License:Educational Community License
public Integer getCountItemFacades(final Long questionPoolId) { final HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery( "select count(ab) from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?"); q.setLong(0, questionPoolId.longValue()); q.setCacheable(true); return q.uniqueResult(); };//w ww. j a va 2s. c om }; Integer count = (Integer) getHibernateTemplate().execute(hcb); return count; }
From source file:org.sakaiproject.tool.assessment.facade.QuestionPoolFacadeQueries.java
License:Educational Community License
/** * Fetch a HashMap of question pool ids and counts for all pools that a user has access to. * We inner join the QuestionPoolAccessData table because the user may have access to pools * that are being shared by other users. We can't simply look for the ownerId on QuestionPoolData. * This was originally written for SAM-2463 to speed up these counts. * @param agentId Sakai internal user id. Most likely the currently logged in user *///from ww w. j ava 2 s . c o m public HashMap<Long, Integer> getCountItemFacadesForUser(final String agentId) { final HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery( "select qpi.questionPoolId, count(ab) from ItemData ab, QuestionPoolItemData qpi, QuestionPoolData qpd, QuestionPoolAccessData qpad " + "where ab.itemId=qpi.itemId and qpi.questionPoolId=qpd.questionPoolId AND qpd.questionPoolId=qpad.questionPoolId AND qpad.agentId=? AND qpad.accessTypeId!=? " + "group by qpi.questionPoolId"); q.setString(0, agentId); q.setLong(1, QuestionPoolData.ACCESS_DENIED); q.setCacheable(true); return q.list(); }; }; HashMap<Long, Integer> counts = new HashMap<Long, Integer>(); List list = getHibernateTemplate().executeFind(hcb); Iterator i1 = list.iterator(); while (i1.hasNext()) { Object[] result = (Object[]) i1.next(); counts.put((Long) result[0], (Integer) result[1]); } return counts; }
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 w w. j a v a2s . c om * @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 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/* w w w .j a v a 2 s. 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 ww.ja va2s . 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 testFindWithCacheable() 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.list()).willReturn(list); hibernateTemplate.setCacheQueries(true); List result = hibernateTemplate.find("some query string"); assertTrue("Correct list", result == list); verify(query).setCacheable(true);/*from ww w. j a va 2s . c om*/ 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 ww w. ja v a 2 s . com*/ 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 testFindByNamedQueryWithCacheable() 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.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 ava2 s . c om*/ 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);// www .j a va 2 s.c o m 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.// www . j av a 2 s . c om * @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()); } }