Skip to content

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
hibernate:mainfrom
febeling:fix/joined-inheritance-toone-alias-pruning
Open

HHH-20718 Fix dangling table alias when joining to a discriminator-less JOINED-inheritance entity via its identifier#13103
febeling wants to merge 2 commits into
hibernate:mainfrom
febeling:fix/joined-inheritance-toone-alias-pruning

Conversation

@febeling

@febeling febeling commented Jul 23, 2026

Copy link
Copy Markdown

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 implicit CASE over 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:

join mappings_a ma1_0 on ma1_1.id=r1_0.mapping_id   -- ma1_1 is never declared

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.pruneForSubclasses then 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 with resolve=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 @OneToOne and @ManyToOne reproduce 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 CriteriaJoinedInheritanceToOneJoinAliasTest covers 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):

  • Add test reproducing the bug
  • Add entries as relevant to migration-guide.adoc OR check there are no breaking changes

https://hibernate.atlassian.net/browse/HHH-20718

@hibernate-github-bot

hibernate-github-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

@febeling febeling changed the title Fix dangling table alias when joining to a discriminator-less JOINED-inheritance entity via its identifier HHH-20718 Fix dangling table alias when joining to a discriminator-less JOINED-inheritance entity via its identifier Jul 23, 2026
@febeling
febeling force-pushed the fix/joined-inheritance-toone-alias-pruning branch 2 times, most recently from 0759b6e to c87c1fd Compare July 23, 2026 16:10
febeling added 2 commits July 24, 2026 18:49
…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
febeling force-pushed the fix/joined-inheritance-toone-alias-pruning branch from c87c1fd to bbe5a25 Compare July 24, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant