-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: hibernateTemplate caching
PostPosted: Fri Dec 02, 2005 2:17 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 pm
Posts: 5
Location: DC
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)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 4:19 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 pm
Posts: 5
Location: DC
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); 
    List list = hibernateTemplate.find("from Contact c where c.activeFlag = 1");
    hibernateTemplate.setCacheQuery(false); 
}



Duh!!! i did not set the cache query to "false" after invoking find(String query). That would explain why every Query was cache.

sorry to bother people...i'm new at this


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 1:59 am 
Newbie

Joined: Mon Feb 23, 2009 1:56 am
Posts: 1
How does this affect thread-safety? I am using HibernateTemplate as a singleton and the above code doesn't seem thread-safe.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.