1
votes

I am using Hibernate 4.17 and use ehcache as second level cache. In the definition of hbm file, I declare the cache at the class level.

<class name="com.test.Program" table="program" mutable="false">

    <cache usage="nonstrict-read-write" />
 ....

I have 1-to-many association defined as follows

 <list name="parameters" cascade="none" batch-size="100">
    <cache usage="nonstrict-read-write" />
    <key column="program_oid"/>
    <index column="sequence" />
    <one-to-many class="com.test.ProgramParameter"/>
  </list>

I have a region defined for main class - Program in the ehcache.xml. The issue is whenever I evict the 2nd level cache using

HibernateUtil.getSessionFactory().getCache().evictEntityRegion("com.test.Program");

All the entities of com.test.Program are evicted but not com.test.Program.parameters Further more, If I try to evict com.test.Program.parameters entities like above, I get the exception "Unknown Entity".

How do I evict the entities of associated class?

1

1 Answers

2
votes

Try:

HibernateUtil.getSessionFactory().getCache().evictCollectionRegion("com.test.Program.parameters");

The cache setting on the parameters list will store just the identifiers of the entities in that list in a separate collection cache, using the identifier of the parent Program entity as the key.

The ProgramParameter entities themselves will not be cached unless you have a corresponding cache setting on that entity, e.g.

<class name="com.test.ProgramParameter" table="program_parameter" mutable="false">

    <cache usage="nonstrict-read-write" />
 ....

Whenever you use a collection cache, you should cache the entities as well otherwise Hibernate will still need to fetch each entity in the collection from the database.