When loading an entity from the cache by natural-id when the concrete type of the cached data is not compatible with the requested type, a {{WrongClassException}} should be thrown. This is consistent with when the same happens loading by id. E.g.
The test case I created utilizes two entities: a base and an extended. These entities have natural ID on the base entity.
*Scenario:* 1. Create and persist a base entity and an extended entity 2. Load the extended entity using the natural ID of the base entity. This is done using the code below.
{code} ExtendedEntity user = s.byNaturalId(ExtendedEntity.class).using("uid", "base").load(); assertNull(user); {code}
*Expected behavior:* The returned entity should be null, according to documentation in org.hibernate.NaturalIdLoadAccess<T>#load().
*Actual behavior if second level cache is _ENABLED_:* WrongClassException is thrown, because it finds the base entity in the second level cache and tries to use that.
*Actual behavior if second level cache is _DISABLED_:* The returned entity is null. This is also the expected behavior.
*Bonus behavior if second level cache is _DISABLED_ and debug logging is enabled on {{org.hibernate.event.internal.AbstractFlushingEventListener}} AND {{org.hibernate.internal.util.EntityPrinter}}:* In 5.0.11, PropertyAccessException will be thrown. In the latest master branch (based on commit 7b78ee99), PersistenceException will be thrown with PropertyAccessException as cause.
I've run the testcases on a branch based on master@7b78ee99 and on a branch based on tag 5.0.11, since 5.0.11 is what's currently being using in JBoss EAP 7.
https://github.com/hibernate/hibernate-orm/pull/1813 |
|