Unexpected results when using class inheritance with JPA entities
Description
Attachments
Activity

Christian Beikov October 29, 2015 at 11:43 AM
Attached a runnable database independent testcase based on the given testcase. It shows that this exception still occurs in hibernate 5.
Steve Ebersole October 28, 2015 at 3:25 AM
As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.
Steve Ebersole October 27, 2015 at 7:15 PM
This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.
For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/
Consider the following JPA classes:
If I assign a
LeaderCardInstance
and aHeroCardInstance
to a player namedjohn
, the database tables contain the following values:PLAYER
CARD
CARD INSTANCE
john
leader
leader
john
hero
hero
However, if I now fetch
john
from the database, I find that all cards are of typeHeroCard
instead of their actual types. This is what I get:PLAYER
CARD
CARD INSTANCE
john
hero
leader
john
hero
hero
Note how a
HeroCard
has been assigned to aLeaderCardInstance
.After reading the JPA spec, I made the following change and everything started working as expected:
I have also examined the generated SQL with and without
targetEntity
and found that in the former case, the discriminator column ofCard
is fetched and thus the correctCard
instance is populated whereas in the later case the discriminator column is not fetched.This behaviour is seen with every 4.3.x version. However, 4.2.x versions throw the following runtime exception:
My question is why does Hibernate pick one specific subclass of
Card
on the 4.3.x branch? Is this a bug?I have attached a Gradle project. Running
gradle clean test -i
will display a table that demonstrates the problem.This code is also available on Github.