Skip to content

fix(hibernate5): join an abstract root that follows a joined subclass, and place a joined subclass in its schema - #16904

Open
borinquenkid wants to merge 10 commits into
8.1.xfrom
fix/h5-joined-subclass-8.1.x
Open

borinquenkid wants to merge 10 commits into
8.1.xfrom
fix/h5-joined-subclass-8.1.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Splits the table-layout changes out of #16845, which fits a patch release only without them. These two change the tables of an application that maps a joined hierarchy:

Upgrade note. A hierarchy with an abstract root and a child that sets tablePerHierarchy false now has a table for the child and no class column on the root; an application with a managed schema has to add the table and move its rows. To keep the old layout, declare tablePerHierarchy true on the root: a root that declares its strategy keeps it. A joined subclass whose base class declares table schema: now lives in that schema. The 8.1 upgrade guide has the entry (6).

The catalog of a joined subclass is tested on a real MariaDB (Testcontainers), where a catalog is the database a table is created in; the spec fails without the catalog branch of the binder.

Fixes #14423
Fixes #14427

Continues #16845.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…ed under an abstract base class

An abstract base class with the default strategy made a subclass that sets
tablePerHierarchy false a single-table subclass, while that subclass's own children
were joined, so loading them asked for a discriminator column the joined table does
not have. The abstract base class now follows the subclass and the hierarchy is joined.

Fixes #14423

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
…og it declares

The table of a joined subclass always took the default schema, so a subclass that
declares its own schema was created, and queried, in the wrong one. It now uses the
schema and catalog from its table mapping, as the root class and the table-per-concrete
class subclass already do.

Fixes #14427

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
…s on the datasource being built

An abstract base class that says nothing about its strategy is joined when a subclass sets
tablePerHierarchy false. The binder wrote that into the mapping of the base class, which every
datasource shares, so a datasource built later saw a joined base class it never inferred, and the
unconfigured subclasses of an unconfigured child stayed single-table under a base class that had lost
its discriminator.

The inference now belongs to the binder, which builds one datasource, and every read of the strategy
goes through it: the whole hierarchy under an inferred base class is joined on that datasource only.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…eps it

A base class that says tablePerHierarchy true, or tablePerSubclass false, was inferred as joined when a
subclass sets tablePerHierarchy false, and its own rows stopped being returned. Mapping now records
whether the strategy was declared, and only a base class that did not declare one follows its subclass.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…r the inherited schema and catalog

The inferred joined hierarchy is found with the root entity of the mapping model and
isAssignableFrom, not by walking the Java classes, so an abstract root that extends a
class that is not a domain class is joined. The declared-strategy flag on Mapping is
private with a getter. Specs: a plain parent class, a schema that only the base class
declares, and the catalog of a joined subclass.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

… and add the upgrade note

The catalog of a joined subclass is observed on a real MariaDB (Testcontainers), where a catalog is
the database a table is created in; the spec fails without the catalog branch of the binder. The
8.1 upgrade guide describes the joined hierarchy under an abstract root and the schema and catalog
of a joined subclass.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A formula property on a joined subclass is stored as a column, so the computed value is lost.

Mapping m = getMapping(parent.getMappedClass());
Subclass subClass;
boolean tablePerSubclass = m != null && !m.getTablePerHierarchy() && !m.isTablePerConcreteClass();
boolean tablePerSubclass = m != null && !isTablePerHierarchy(parent.getMappedClass(), m) && !m.isTablePerConcreteClass();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An abstract root with no declared strategy, and a subclass that sets tablePerHierarchy false and maps doubled as formula amount * 2. Saving amount 7 stores doubled as 0. The formula is never marked derived, so it is bound as a column. Mark derived properties before choosing the subclass strategy.

@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:12
…derived, not bound as a column

The properties with a formula were marked derived only for the root of a hierarchy and for the
subclasses of a single table. A joined subclass, and a union one, bound a formula property as a
column of its own table, so saving stored nothing for it and reading returned null instead of the
formula. The properties are marked before the strategy of the subclass is chosen.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Reproduced, and fixed in the new commit. InferredJoinedHierarchySpec has your case on Hibernate 5: an abstract root with no declared strategy, and a subclass that sets tablePerHierarchy false and maps doubled as the formula amount * 2. Saving amount 7 and reading the entity back returned null for doubled, because the formula was bound as a column of the subclass table that nothing fills.

The properties with a formula were marked derived for the root of a hierarchy and for the subclasses of a single table, and not for the others, so a joined subclass bound them as columns. They are marked before the strategy of the subclass is chosen now. A union subclass (tablePerConcreteClass) had the same gap, so the spec has a row for it too; both rows fail without the change.

The Hibernate 5 module (1,181 tests) and codeStyle pass.

If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A formula on an enum property of a joined subclass is still stored as a column.

// column of its own table, which holds no value for it.
Mapping subMapping = getMapping(sub);
if (subMapping != null) {
configureDerivedProperties(sub, subMapping);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A joined subclass has an enum property whose value comes from a formula. This marks other formula properties as derived. The enum still gets a column, because the enum path does not check that. Use the formula.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 33a8502. You are right: this pull request marks every formula property of a joined or union subclass as derived (configureDerivedProperties(sub, subMapping), line 1656), and bindSimpleValue reads a derived property from its formula (line 3030), but the enum path, bindEnumType, never looked at it and created a column that holds no value. bindEnumType, line 2293 now adds the formula and returns without a column for a derived property, as bindSimpleValue does (a tenant id stays a column, as it does there too). That is not specific to a joined subclass: an enum with a formula in a root class had the same column, so it changes there too, and the upgrade note says so (upgrading81x.adoc line 101).

The test: JoinedSubclassFormulaSpec line 47, a joined subclass with a text and an enum property that each have a formula: its table has neither column and keeps its own, and line 70 reads both values from the formulas. The enum rows fail without the change. grails-data-hibernate5-core runs 1,184 tests green and codeStyle is clean. Nothing in the guide describes a formula on an enum property; the upgrade note is the only text.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants