Java Code Examples for org.hibernate.Query#setCacheRegion()
The following examples show how to use
org.hibernate.Query#setCacheRegion() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateTemplate.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @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()); }
Example 2
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @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()); }
Example 3
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @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()); } }
Example 4
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @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()); } }
Example 5
Source File: HibernateTemplate.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @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()); } }
Example 6
Source File: TypeManagerImpl.java From sakai with Educational Community License v2.0 | 6 votes |
/** * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String) */ 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 { 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; }
Example 7
Source File: TypeManagerImpl.java From sakai with Educational Community License v2.0 | 6 votes |
/** * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String) */ 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 { 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; }
Example 8
Source File: TypeManagerImpl.java From sakai with Educational Community License v2.0 | 5 votes |
/** * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String, java.lang.String, java.lang.String) */ 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 { 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; }
Example 9
Source File: TransactionalTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemTransactional item = new ItemTransactional("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemTransactional) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemTransactional) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 10
Source File: ReadWriteTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemReadWrite item = new ItemReadWrite("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemReadWrite) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemReadWrite) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 11
Source File: TransactionalTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemTransactional item = new ItemTransactional("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemTransactional) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemTransactional) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 12
Source File: ReadWriteTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemReadWrite item = new ItemReadWrite("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemReadWrite) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemReadWrite) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 13
Source File: TransactionalTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemTransactional item = new ItemTransactional("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemTransactional) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemTransactional) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 14
Source File: ReadWriteTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemReadWrite item = new ItemReadWrite("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemReadWrite) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemReadWrite) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 15
Source File: TransactionalTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemTransactional item = new ItemTransactional("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemTransactional) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemTransactional) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 16
Source File: ReadWriteTest.java From redisson with Apache License 2.0 | 5 votes |
@Test public void testQuery() { Statistics stats = sessionFactory().getStatistics(); Session s = openSession(); s.beginTransaction(); ItemReadWrite item = new ItemReadWrite("data"); item.getEntries().addAll(Arrays.asList("a", "b", "c")); s.save(item); s.flush(); s.getTransaction().commit(); s = openSession(); s.beginTransaction(); Query query = s.getNamedQuery("testQuery"); query.setCacheable(true); query.setCacheRegion("myTestQuery"); query.setParameter("name", "data"); item = (ItemReadWrite) query.uniqueResult(); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount()); s = openSession(); s.beginTransaction(); Query query2 = s.getNamedQuery("testQuery"); query2.setCacheable(true); query2.setCacheRegion("myTestQuery"); query2.setParameter("name", "data"); item = (ItemReadWrite) query2.uniqueResult(); s.delete(item); s.getTransaction().commit(); s.close(); Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount()); stats.logSummary(); }
Example 17
Source File: TypeManagerImpl.java From sakai with Educational Community License v2.0 | 5 votes |
/** * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String, java.lang.String, java.lang.String) */ 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 { 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; }
Example 18
Source File: AbstractSessionImpl.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
private void initQuery(Query query, NamedQueryDefinition nqd) { query.setCacheable( nqd.isCacheable() ); query.setCacheRegion( nqd.getCacheRegion() ); if ( nqd.getTimeout()!=null ) query.setTimeout( nqd.getTimeout().intValue() ); if ( nqd.getFetchSize()!=null ) query.setFetchSize( nqd.getFetchSize().intValue() ); if ( nqd.getCacheMode() != null ) query.setCacheMode( nqd.getCacheMode() ); query.setReadOnly( nqd.isReadOnly() ); if ( nqd.getComment() != null ) query.setComment( nqd.getComment() ); }
Example 19
Source File: SQLFunctionsInterSystemsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testCachedQueryRegion() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString("name", "Simple 1"); assertTrue( q.list().size()==1 ); simple = (Simple) q.list().get(0); q.setString("name", "Simple 2"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); simple.setName("Simple 2"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple, new Long(10) ); s.delete(simple); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); t.commit(); s.close(); }
Example 20
Source File: SQLFunctionsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testCachedQueryRegion() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString("name", "Simple 1"); assertTrue( q.list().size()==1 ); simple = (Simple) q.list().get(0); q.setString("name", "Simple 2"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); simple.setName("Simple 2"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple, new Long(10) ); s.delete(simple); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); t.commit(); s.close(); }