HHH-20718 Fix dangling table alias when joining to a discriminator-less JOINED-inheritance entity via its identifier#13103
Open
febeling wants to merge 2 commits into
Conversation
|
Thanks for your pull request! This pull request appears to follow the contribution rules. › This message was automatically generated. |
febeling
force-pushed
the
fix/joined-inheritance-toone-alias-pruning
branch
2 times, most recently
from
July 23, 2026 16:10
0759b6e to
c87c1fd
Compare
…classes A to-one join whose foreign key targets the identifier of a JOINED-inheritance entity qualifies the id column with the root table alias. pruneForSubclasses removed that root-table join as unused, leaving the join predicate with a dangling alias. Retain the identifier table of each used persister when it differs from the leaf main table. Affects JOINED targets without a discriminator column. Related: HHH-20688.
febeling
force-pushed
the
fix/joined-inheritance-toone-alias-pruning
branch
from
July 24, 2026 16:49
c87c1fd to
bbe5a25
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to HHH-20688, but a distinct case. On current main the model in that report — a JOINED hierarchy with a
@DiscriminatorColumn— already seems to generate valid SQL. This PR fixes a closely related case that still fails: a JOINED target without a discriminator column.Tracked under HHH-20718.
Note this is a valid, default-supported mapping: JOINED inheritance does not require a
@DiscriminatorColumn. When absent, Hibernate derives the concrete subtype from an implicitCASEover subclass-table row presence, and the test suite already exercises JOINED-without-discriminator hierarchies. So the broken SQL below is generated for a legitimate mapping.A Criteria path traversal through a to-one association whose target uses JOINED inheritance (no discriminator column), and whose foreign key targets the entity identifier, generates a join whose ON clause references an undeclared table alias:
The query then fails on H2 with
Column "MA1_1.ID" not found.Root cause: for a JOINED entity the identifier lives on the root table, so the FK join predicate qualifies the id column with the root-table alias.
JoinedSubclassEntityPersister.pruneForSubclassesthen removes the root-table join: it retains a table reference join only when it is needed for the used entity subtypes, and does not account for the enclosing association's ON predicate still referencing the root table. That leaves the predicate with a dangling alias. (With a discriminator column the foreign key resolves to the leaf table instead, so the root table is not referenced and the problem does not arise.)Fix: in
pruneForSubclasses, also retain each used persister's identifier table when it differs from the leaf main table. It is looked up withresolve=false, so it is only retained when that join already exists in the group — when the root table isn't needed nothing is retained and the existing optimization is unchanged.Both
@OneToOneand@ManyToOnereproduce this. In the SQL we observed, collections (@OneToMany,@ManyToMany) and a default to-one whose FK targets the leaf primary key do not reference the root table in the join predicate, and are not affected.Test
CriteriaJoinedInheritanceToOneJoinAliasTestcovers both to-one kinds; both fail before the fix.HHH-20718 (related: HHH-20688)
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.
Please make sure that the following tasks are completed:
Tasks specific to HHH-20718 (Bug):
migration-guide.adocOR check there are no breaking changeshttps://hibernate.atlassian.net/browse/HHH-20718