Hibernate version: 3.1
Code:
class ContactDAO{
private HibernateTemplate hibernateTemplate; // automatically get injected
/*************************************************
* Set the HibernateTemplate.
* Note: do not call this method..The hibernate template will be injected
* into this class as specified by the Spring xml file.
*************************************************/
public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}
public List getState(){
// this query should be cache
hibernateTemplate.setCacheQuery(true);
return hibernateTemplate.find("from State s order by s.abbr DESC");
}
public List getContacts(){
// this query should be cache
hibernateTemplate.setCacheQuery(true); // problem is here
return hibernateTemplate.find("from Contact c where c.activeFlag = 1");
}
public List getUsers(){
// don't want this query to be cache
return hibernateTemplate.find("from User u where u.type = client");
}
}
What i want is to control what is cache and what is not..hipefully, get it cache in 2L (EHCache). The problem is, when i use setCacheQuery(true),
every query is cache. how can i control caching to certan query?
if i have definied an ehcache xml properties:
Code:
<ehcache>
<cache name="query.States"
maxElementsInMemory="60"
eternal="true"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
/>
</ehcache>
public getContacts(){
hibernateTemplate.setQueryCacheRegion("query.States");
hibernateTemplate.setCacheQuery(true);
return hibernateTemplate.find("from Contact c where c.activeFlag =
}
Will this set the EhCache to "query.States? or am i doing it wrong?
dsorry..i'm pretty new to this. I googled, but did not find a solution to
caching the collection from a Query using HibernateTemplate..only find solution to using Query = session.createQuery()...etc, which is not feasible for my app..we're hiding the Session through HibernateTemplate)