Unexpected results when using class inheritance with JPA entities

Description

Consider the following JPA classes:

If I assign a LeaderCardInstance and a HeroCardInstance to a player named john, 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 type HeroCard 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 a LeaderCardInstance.

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 of Card is fetched and thus the correct Card 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.

Attachments

2

Activity

Show:

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/

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Created July 9, 2015 at 1:55 PM
Updated April 19, 2019 at 4:34 PM