Hi,
We use hibernate inheritance extensively in our software. I have realised (plz correct me if I am wrong) that second cache in hibernate does not support caching on the subclass level. It uses the root class cache region for all subclasses. I would like to have separate caching settings for subclasses. I have looked into the hibernate-core code and tried to add support for caching subclasses in their own regions. The result seems correct and I can see subclasses being cached as required. I changed the following:
- Added new hibernate setting called
forcePerSubclassSecondLevelCache to toggle on/off the new behaviour.
- In
SessionFactoryImpl: Changed entityPersister cacheRegionName to be
Code:
cacheRegionName = cacheRegionPrefix + model.getEntityName()
instead of
Code:
cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName()
if forcePerSubclassSecondLevelCache setting is enabled.
- In
AbstractSessionImpl: Changed generateCacheKey to use
Code:
persister.getEntityName()
instead of
Code:
persister.getRootEntityName()
if forcePerSubclassSecondLevelCache settign is enabled.
- changed different classes to use the new generateCacheKey.
I am fine with keeping enabling the cache to be defined at the root class level but wanted the sublass to use different caches.
My question is, are there good reasons to force subclasses to use root class cache only and I am breaking something in hibernate by not doing so? and if this is the case, is there an alternative way to reach the same result above? I am just worried I am doing something wrong as it seems too easy to be true :-)
Thanks,
hshicx