-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate second level cache questions
PostPosted: Thu Jan 28, 2010 1:41 pm 
Beginner
Beginner

Joined: Mon Dec 21, 2009 3:43 pm
Posts: 24
I am trying to find out if enabling the second level cache provided by hibernate will help my performance and after reading through the documentation I am still a little confused and i have a couple of questions.

1. Do entities get stored in the second level cache even when you are asking for an entity by something other than its primary key?

i.e.
Code:
   
public class Field implements Serializable {
   
   private long id;
   private String name;
   private String category;
.
.


I search for Field's by name or name and category based on different criteria. So I cannot make name and category a composite primary key because category can sometimes be null. Lets say I do the following multiple times
Code:
   
String query = "SELECT f FROM Field f WHERE f.name = :name and f.category = :category";
        
entityManager.createNamedQuery(query)
            .setParameter("name", name)
                .setParameter("category", category)
            .getSingleResult()


The name and category are the same for a lot of the queries, so I really do not want to go out to the db every time I perform this query. If I turn on the second level cache will I avoid going to the db every time I make this query?


Top
 Profile  
 
 Post subject: Re: Hibernate second level cache questions
PostPosted: Fri Jan 29, 2010 10:55 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You will only hit the cache if you ask for an entity by its id.

Hibernate FAQ
Discussion

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Hibernate second level cache questions
PostPosted: Fri Jan 29, 2010 11:05 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
You will only hit the cache if you ask for an entity by its id.


This is not completely true. Because beyond
Code:
hibernate.cache.use_second_level_cache=true


you could also activate the second level query cache
Code:
hibernate.cache.use_query_cache=true


Activating the second level query cache allows you to cache also queries (together which its result set)
into 2L-cache.
Furthermore you must tell hibernate explicitely to put the query into 2L cache
mynamedQuery.setCacheable(true);
Also it is important to have the 2L itself enabled and regarding persistent class configured for being cached into 2Lcache.
Otherwise second level query cache is contraproductive.


Top
 Profile  
 
 Post subject: Re: Hibernate second level cache questions
PostPosted: Fri Jan 29, 2010 11:18 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
1. Do entities get stored in the second level cache even when you are asking for an entity by something other than its primary key?


Yes. (provided that regarding entity class is configured for being cached into 2L Cache)


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