We're updating the issue view to help you get more done. 

Inconsistent behavior when fetching cached subclass entities from 2L

Description

Enabling the hibernate second-level cache is leading to inconsistent behavior when fetching cached subclass entities as opposed to fetching non-cached subclass entities.

For more informations, please see
https://forum.hibernate.org/viewtopic.php?f=1&t=1002880&p=2425931#p2425931

Testcase attached.

Cause:
IMHO the bug consists in the fact, that
persister.getRootEntityName() instead to persister.getEntityName() is passed as 3thrd parameter
to the constructor of CacheKey in most places where CacheKey is used, for example in DefaultLoadEventListener.java:

new CacheKey(
event.getEntityId(),
persister.getIdentifierType(),
persister.getRootEntityName(),
source.getEntityMode(),
source.getFactory()

In this way the real entity name get swallowed and the type-saveness is not guaranteed anymore.

Workaround:

Subclass2 tofind = null;
try {
tofind = (Subclass2) em.find(Subclass2.class,"identifier1");
}
catch (ClassCastException cc) {
// swallow , this is a workaround
}

Environment

Hibernate3.5.0CR2,HSQLDB, EHCache as 2L-cache Impl

Status

Assignee

Unassigned

Reporter

Guenther Demetz

Labels

Affects versions

3.2.5
3.3.1
3.5.0.Beta-1
3.5.0-Beta-2
3.5.0-Beta-3
3.5.0-Beta-4
3.5.0-CR-1
3.5.0-CR-2

Priority

Major