Search code examples
hibernatesecond-level-cachejboss-cache

Understanding JBossCache - Caching an object graph with many associations


I'm trying to use JBossCache as a JPA/Hibernate 2nd level cache provider to cache repeatedly called queries. The queries return entities of a specific type, lets call it FooType.

FooType looks like the following:

@Entity(name = FooType)
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class FooType {
   protected BarType barType;

   @ManyToOne(targetEntity = BarType.class, cascade = {
       CascadeType.ALL
   })
   BarType getBarType() {
       return barType;
   }
}

So, Footype has a many-to-one association. Now, when I call the query, only a very small part seems to be put in the cache. I think the reason is because I didn't mark the association with the @Cache tag. Am I correct?

But the real question is:

BarType also has a handful of associations, and these return objects which also provide associations and so on, building a big graph of associations. Now, do I need to

a) annotate all of those classes and

b) also annotate the associations

in order for the whole query to be cached?


Solution

  • All entities/collections you'd like to be cached must have @Cache so that they can be cached. Query cache works slightly different, to get the results cached, you need to make the query object cacheable.

    Btw, as always, cache if it actually makes sense!

    http://docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-cache