Hi,
I have an application (spring/hibernate/ehcache 4.3.5 final) where second level cache is working for the majority of objects.
But it seems to be broken at a TABLE_PER_CLASS inheritance model, there seems to be some confusion as to the entity name in the cachekey, objects get saved into the region as the super class name but when it goes to retrieve the object it is using the subclass name.
So I have 2 classes, one super and one sub...
Code:
@Entity
//@Indexed
@Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)
@Inheritance (strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name="group_link_entity")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE )
public abstract class GroupLinkEntity {
@TableGenerator(
name="sequenceGen",
table="database_sequence",
pkColumnName="GEN_KEY",
valueColumnName="GEN_VALUE",
pkColumnValue="ENTITY_ID",
allocationSize=1)
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="sequenceGen")
@Column(name = "ENTITY_ID")
protected int id;
and
Code:
@Entity
//@Indexed
@Audited(targetAuditMode=RelationTargetAuditMode.NOT_AUDITED)
@Table(name = "core_node")
public class Node extends GroupLinkEntity implements Serializable {
The region will be as expected the super class name. first time a load runs it places the object into the second level cache which I can see by running with the stats on the following code
Code:
Map cacheEntries = ((Session) em.getDelegate()).getSessionFactory().getStatistics().getSecondLevelCacheStatistics( element )
.getEntries();
if( cacheEntries.size() > 0 ){
System.out.println( " ... " + cacheEntries ) ;
}
prints out...
- com.core.dao.GroupLinkEntity
... {1=CacheEntry(com.core.dao.Node)[........
So I know that the Node object with PK 1 is in there.
The second time I run through it and expect to get the object from the cache it gets to the get method of NonStrictReadWriteEhcacheEntityRegionAccessStrategy and I can see it looking in the region for a key of...
com.core.dao.Node#1
But if I look in the cache object in the region I can see that the key it holds is...
[com.core.dao.GroupLinkEntity#1]
I know that they are both the same object but something between writing and reading from is going wrong.
The object is stored under TwoPhaseLoad using the root name...
Code:
final CacheKey cacheKey = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
But they create the cachekey using the class name for retrieval in DefaultLoadEventListener...
Code:
final CacheKey ck = source.generateCacheKey(
event.getEntityId(),
persister.getIdentifierType(),
persister.getEntityName()
);
The docs are pretty clear that the cachekey should only be made using the root name so I am unsure what is going on here. To me the code just does not work and as they chop and change between root name for writing to he cache and entity name for reading from the cache.
Caching under inheritance can just not work at all? Am I imagining this?
(this post has been previously posted on the ehcache forum but I have posted it here now as it is more a hibernate issue I believe, there were no replies on the other forum)