1

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
helpermethod
  • 59,493
  • 71
  • 188
  • 276

1 Answers1

1

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

Galder Zamarreño
  • 5,027
  • 2
  • 26
  • 34
  • So, if I make the query object cacheable, I don't need to make the Entities cacheable it returns? – helpermethod Jul 29 '11 at 09:13
  • The problem is, the inital query gets cached but Hibernate also creates a giant set off additional queries which follow the inital query. These don't get cached. – helpermethod Jul 29 '11 at 09:30
  • 1
    You need both the query to be cacheable, and Entities to be marked with @Cache. Not sure I understand where those additional queries come from. Are they created by you? Or internally by Hibernate? If they're internal, you can't do anything about it AFAIK. – Galder Zamarreño Aug 04 '11 at 11:27