I am using Hibernate 3.5 and ehCache for L2 caching.
http://ehcache.org/documentation/hibernate.htmlI created a table per class hierarchy:
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/inheritance.html#inheritance-tableperclassParent abstract class
RefCode :
Quote:
@Entity
@Table(name="REF_CODE")
@DiscriminatorColumn(name="CODE_SET",discriminatorType=DiscriminatorType.STRING)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@SequenceGenerator(sequenceName="CODE_SEQ", name = "CODE_SEQ", allocationSize=1)
public abstract class RefCode
Subclasses
Country :
Quote:
@Entity
@DiscriminatorValue(value=CodeType.COUNTRY)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Country extends RefCode {
In
ehache.xml I declared regions for Country and Currency
Quote:
<cache name="Country" overflowToDisk="true" maxElementsInMemory="2000" timeToIdleSeconds="3600" timeToLiveSeconds="36000" diskPersistent="true" maxElementsOnDisk="10000" eternal="false" statistics="true" />
When I run my JUnit test I can see region files created in disk store.
However when I look at cache statistics I see zero cache hits for those classes. (Note: I can see cache hits for other classes)
Quote:
@Test
@Transactional
public void testCache() throws Exception{
CacheManager manager = CacheManager.getInstance();
Cache cache = (Cache) manager.getCache("Country");
Session session=((Session)referenceDataDao.getEntityManager().getDelegate());
for(int i=0; i<100; i++) {
Country country=(Country)session.load(Country.class,334L);
}
System.out.println("Country Name: " + country.getName());
// cache statistics
System.out.println("Country cache statistics:" + cache.getStatistics().toString());
}
Is it a feature of Hibernate that prevents second level caching in table per class hierarchy?
I added caching to the abstract base class and I got cache hits on all derived classes. However, it does not explain the problem - why child classes can not be cached individually? It implies that if you are using Hibernate table per class hierarchy and you want to add second level caching:
1. All child classes will have to share same cache region
2. All child classes will use same cache settings
Did anyone on Hibernate team done any testing on how L2 caching works when you use Hibernate Inheritance?