Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate Inheritance caching, incorrect entity names used in CacheKey?

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...

@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

@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

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...

final CacheKey cacheKey = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );

But they create the cachekey using the class name for retrieval in DefaultLoadEventListener...

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?

like image 924
hairyCrazyMan Avatar asked Nov 01 '22 01:11

hairyCrazyMan


2 Answers

So to fix this I had to modify DefaultLoadEventListener in hibernate-core, I changed line 590 to...

final SessionFactoryImplementor factory = source.getFactory();
final CacheKey ck = source.generateCacheKey(
        event.getEntityId(),
        persister.getIdentifierType(),
        persister.getRootEntityName()
);

rather than...

final SessionFactoryImplementor factory = source.getFactory();
final CacheKey ck = source.generateCacheKey(
        event.getEntityId(),
        persister.getIdentifierType(),
        persister.getEntityName()
);

So basically the object is found using the superclass name rather than the subclass name.

The hibernate code just looks wrong to me and it as an obvious fix but surely I have done something wrong rather than found a bug?

like image 76
hairyCrazyMan Avatar answered Nov 09 '22 09:11

hairyCrazyMan


I too am facing similar issue in Single table inheritance. L2 cache works fine if I extend from a base class with @MappedSuperclass however if the subclass extends from a base abstract class marked as @Entity using single table inheritance with discriminator, there are puts on the cache but no cache hits. More information can be seen here where one of the fix version is mentioned as 4.3.6. I have tested this in 4.2.12 (also mentioned in fix list) and it is fixed there. However 4.3.6 is yet unreleased. Also the workaround proposed by you (using the Root entity name) might lead you into another issue mentioned here.

like image 39
Shailendra Avatar answered Nov 09 '22 07:11

Shailendra