-->
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.  [ 10 posts ] 
Author Message
 Post subject: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 1:37 am 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
I have created a query for select Operation in Hibernate using
Code:

Query query =session.createQuery("select from Book book where book.bookname =?");                           
query.setParameter(0,"Hibernate",Hibernate.STRING);
query.setCacheable(true);


Please tell me where exactly the cache with Query will get stored upon ??(I am ot using second level Cache)

Thanks in advance


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 3:12 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Query results are cached in the second-level cache, so you need to enable this. Check this documentation:

http://docs.jboss.org/hibernate/stable/ ... cache.html
http://docs.jboss.org/hibernate/stable/ ... cache.html


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 3:43 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:32 am
Posts: 25
Location: Austria/Vienna
in the hibernate config file add this:
Code:
      <property name="cache.use_second_level_cache">true</property>
      <property name="hibernate.cache.use_query_cache">true</property>
      <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>


the provider class of course depends on the impl you decide.

for each cache provider you need an extra configuration.
for example an
Quote:
ehcache.xml


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 6:06 am 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
Thank You very much cgs and nord.

I have cleared my query with Query Cache .

I have an another query also what if i use cache at Object level . Does this also require second level cache?
Code:
<class name="eg.Cat" .... >
    <cache usage="read-write"/>
    ....
    <set name="kittens" ... >
        <cache usage="read-write"/>
        ....
    </set>
</class>


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 7:22 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:32 am
Posts: 25
Location: Austria/Vienna
yes, you do.

it is called association-caching

here also is an excellent article about caching:
http://www.javalobby.org/java/forums/m92235473.html

! Be careful with read-write caching. maybe you can start with read-only first.


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 9:36 am 
Newbie

Joined: Thu Nov 15, 2007 3:17 am
Posts: 19
cgs wrote:
! Be careful with read-write caching. maybe you can start with read-only first.

Can you please explain whats wrong with read-write? With read-only caching, Hibernate throws an exception on #saveOrUpdate(). So read-only doesnt seem very practically to me in most cases.


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Tue May 12, 2009 10:07 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:32 am
Posts: 25
Location: Austria/Vienna
are you using casade insert and or update for kittens ?
what is the error you get exactly ?


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Wed May 13, 2009 11:41 am 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
Quote:
! Be careful with read-write caching. maybe you can start with read-only first




<cache usage="read-write"/>

Whats danger with read-write ?


"On this my understanding with respect to above is that " Whenever there is a change of the entity at the database level the cache is also updated simultaneously " please tell me if i am right or wrong on this .

Waiting for your replies


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Thu May 14, 2009 3:01 am 
Newbie

Joined: Thu Nov 15, 2007 3:17 am
Posts: 19
kiran7881 wrote:
Whenever there is a change of the entity at the database level the cache is also updated simultaneously " please tell me if i am right or wrong on this .

IIRC the entity will be removed from cache when an UPDATE statement changes the cached entity. So the next Session#get() on the entity results in a CACHE MISS.


Top
 Profile  
 
 Post subject: Re: How setCacheable of Query will work
PostPosted: Sat May 16, 2009 11:00 am 
Beginner
Beginner

Joined: Wed May 06, 2009 12:23 pm
Posts: 23
Great Point byto , Can You then how to handle this type of situation ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.