-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate L2 caching and table per class hierarchy
PostPosted: Thu Jan 27, 2011 3:51 pm 
Newbie

Joined: Mon Jan 24, 2011 1:04 pm
Posts: 1
I am using Hibernate 3.5 and ehCache for L2 caching.
http://ehcache.org/documentation/hibernate.html

I created a table per class hierarchy:
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/inheritance.html#inheritance-tableperclass

Parent 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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.