2

Supose that there multiple Java applications which share a common entity module (Entity classes + Hibernate XML mappings). Currently, the entities are not enabled for caching (no <cache.../> elements within the mappings).

Most of the applications are largly concerned with editing single entities an thus it cannot relie on second level cache.

Now, a new application is implemented which

  1. Should use the same entity mappings
  2. but must use 2nd level and query cache.

How to configure the cache?

Several observations:

  1. I cannot add <cache.../> elements to the mappings since this would break the other applications which do not configure such a cache and which are not under my influence:

    Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]

  2. I can't find a way to activate an entity for caching outside of its mapping. Setting up a cache region for an entity in ehcache.xml does not help
  3. Even if I could change all the other applications and add <cache.../> to the entity mappings, it does not work to disable the cache by setting hibernate.cache.use_second_level_cache=false although it is said so in http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html, table 3.5
Reporter
  • 3,897
  • 5
  • 33
  • 47
rainer198
  • 3,195
  • 2
  • 27
  • 42

1 Answers1

1

It looks like you can use <class-cache> elements to configure caching in hibernate.cfg.xml rather than in entity mappings, see 3.8. XML configuration file.

I guess you can afford creating a custom hibernate.cfg.xml that would use existing mappings, and if not, there are Configuration.setCacheConcurrencyStrategy() methods that might help as well.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Thank you, must have been blind, but I really tried to find such a configuration option. For those who like programmatic Spring configuration using e.g. a `LocalSessionFactoryBean`, the corresponsing method is `setEntityCacheStrategies(Properties entityCacheStrategies)` http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/LocalSessionFactoryBean.html#setEntityCacheStrategies(java.util.Properties) – rainer198 Dec 20 '11 at 16:25