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

Activity

Show:
Christian Beikov
October 30, 2015, 9:40 AM

Works with hibernate 5

Guenther Demetz
November 2, 2015, 7:26 AM

Thanks for testing it Christian!

Steve Ebersole
November 2, 2015, 2:50 PM

Yep, thanks Christian!

Guenther Demetz
July 6, 2016, 6:21 AM

Hm, what did you test actually Christian?
Now I moved from Hibernate4 to Hiberante5 (5.2.1) and I face this exception again..;-(

Guenther Demetz
July 6, 2016, 7:04 AM

Ah, I see. The final decision was to consistently throw WrongClassException instead of returning null. That's ok for me.

Assignee

Unassigned

Reporter

Guenther Demetz

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Priority

Major
Configure