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 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString("name", "Simple 1"); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1");
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 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString("name", "Simple 1"); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1");
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @param queryObject the Query object to prepare * @see #setCacheQueries * @see #setQueryCacheRegion */ @SuppressWarnings({"rawtypes", "deprecation"}) protected void prepareQuery(org.hibernate.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()); } ResourceHolderSupport sessionHolder = (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory()); if (sessionHolder != null && sessionHolder.hasTimeout()) { queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }
/** * Prepare the given Query object, applying cache settings and/or * a transaction timeout. * @param queryObject the Query object to prepare * @see #setCacheQueries * @see #setQueryCacheRegion */ @SuppressWarnings({"rawtypes", "deprecation"}) protected void prepareQuery(org.hibernate.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()); } ResourceHolderSupport sessionHolder = (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory()); if (sessionHolder != null && sessionHolder.hasTimeout()) { queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }
@Override public Query setCacheRegion(String s) { return query.setCacheRegion(s); }
public TypedQuery<T> setCacheRegion(String cacheRegion) { query.setCacheRegion(cacheRegion); return this; }
@Override public Query setCacheRegion(final String cacheRegion) { return query.setCacheRegion(cacheRegion); }
@Override protected void applyCacheRegion(String regionName) { query.setCacheRegion( regionName ); }
Query qry = session.createQuery("select name from tableName where Id=1"); qry.setCacheRegion("cacheReginName");
@Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) class Employee { @Column Integer employeeCode; } public Integer getEmployeeCode(String userName) { Session session = sessionfactory.getCurrentSession(); Query q = session.createQuery("from Employee emp where emp.userName = :username"); q.setString("username", userName); Employee emp = q.setCacheRegion("Employee").setCacheable(true).uniqueResult(); return emp.getEmployeeCode(); }
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(); } };
public String getBranch(Object holder, boolean useRegion) throws Exception { return withTxSessionApply(useJta, sessionFactory, session -> { Query query = session.createQuery( "select account.branch from Account as account where account.accountHolder = ?"); query.setParameter(0, holder); if (useRegion) { query.setCacheRegion("AccountRegion"); } query.setCacheable(true); return (String) query.list().get(0); }); }
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(); } };
public int getTotalBalance(AccountHolder holder, boolean useRegion) throws Exception { List results = (List) withTxSessionApply(useJta, sessionFactory, session -> { Query query = session.createQuery( "select account.balance from Account as account where account.accountHolder = ?"); query.setParameter(0, holder); if (useRegion) { query.setCacheRegion("AccountRegion"); } query.setCacheable(true); return query.list(); }); int total = 0; if (results != null) { for (Iterator it = results.iterator(); it.hasNext();) { total += ((Integer) it.next()).intValue(); System.out.println("Total = " + total); } } return total; }
public int getCountForBranch(String branch, boolean useRegion) throws Exception { return withTxSessionApply(useJta, sessionFactory, session -> { Query query = session.createQuery( "select account from Account as account where account.branch = :branch"); query.setString("branch", branch); if (useRegion) { query.setCacheRegion("AccountRegion"); } query.setCacheable(true); return query.list().size(); }); }
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() ); }
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() ); }
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() ); }
protected org.hibernate.Query createQuery() { parseEjbql(); evaluateAllParameters(); org.hibernate.Query query = getSession().createQuery( getRenderedEjbql() ); setParameters( query, getQueryParameterValues(), 0 ); setParameters( query, getRestrictionParameterValues(), getQueryParameterValues().size() ); if ( getFirstResult()!=null) query.setFirstResult( getFirstResult() ); if ( getMaxResults()!=null) query.setMaxResults( getMaxResults()+1 ); //add one, so we can tell if there is another page if ( getCacheable()!=null ) query.setCacheable( getCacheable() ); if ( getCacheRegion()!=null ) query.setCacheRegion( getCacheRegion() ); if ( getFetchSize()!=null ) query.setFetchSize( getFetchSize() ); return query; }
private void prepareQuery(Query queryObject) { if(getHibernateTemplate().isCacheQueries()) { queryObject.setCacheable(true); if(getHibernateTemplate().getQueryCacheRegion() != null) { queryObject.setCacheRegion(getHibernateTemplate().getQueryCacheRegion()); } } if(getHibernateTemplate().getFetchSize() > 0) { queryObject.setFetchSize(getHibernateTemplate().getFetchSize()); } if(getHibernateTemplate().getMaxResults() > 0) { queryObject.setMaxResults(getHibernateTemplate().getMaxResults()); } }