Skip to content

Hibernate 7: restore the class discriminator column with fully qualified names - #16471

Merged
jdaugherty merged 2 commits into
apache:8.0.xfrom
jdaugherty:fix/16465-h7-default-discriminator
Oct 2, 2026
Merged

jdaugherty merged 2 commits into
apache:8.0.xfrom
jdaugherty:fix/16465-h7-default-discriminator

Conversation

@jdaugherty

@jdaugherty jdaugherty commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16465

Problem

A table-per-hierarchy tree without a discriminator mapping behaved differently in grails-data-hibernate7 than in grails-data-hibernate5:

  • The discriminator column was named DTYPE instead of class. A table created by grails-data-hibernate5 then failed dbCreate: 'validate' with missing column [DTYPE].
  • Subclasses stored their simple name (Car) instead of the fully qualified name (example.Car), so existing rows did not match.
  • A root mapping that set only the column (discriminator column: 'class') failed at startup with Could not parse discriminator value 'null'.

The Hibernate 7 docs already describe a class column, and the DTYPE default dates from the initial Hibernate 7 port, so this restores the intended default rather than adding a setting.

Changes

  • The default discriminator column is named class (GrailsDomainBinder.DEFAULT_DISCRIMINATOR_COLUMN_NAME, which replaces JPA_DEFAULT_DISCRIMINATOR_TYPE). Hibernate 7 no longer provides RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME.
  • A subclass without a discriminator value stores its fully qualified class name.
  • A root mapping that sets only the discriminator column or formula keeps the class name as the root value.
  • The inheritance docs state the default column and values.
  • The upgrade guide (section 26.10) shows 8.0.0-RC1 and RC2 users how to keep their DTYPE layout with a mapping.

Tests

  • New TablePerHierarchyDefaultDiscriminatorSpec, through the public GORM API:

    • a three-level hierarchy stores fully qualified names in a class column and loads polymorphically;
    • dbCreate: 'validate' succeeds against a hand-built grails-data-hibernate5-style table, and its existing rows load as the right classes;
    • a column-only root mapping keeps fully qualified values;
    • a table in the 8.0.0-RC layout validates and reads with the mapping from the upgrade guide.

    On the current 8.0.x code, the spec fails with both errors from the issue.

  • The binder and entity specs are updated to the new defaults, with new cases for column-only and formula-only mappings and the subclass default.

  • grails-data-hibernate7-core, -dbmigration and -dbmigration-core tests pass, and CodeNarc and Checkstyle are clean.

Release notes

Hibernate 7 again names the default table-per-hierarchy discriminator column class and stores fully qualified class names, as Grails 7 did. Tables created with Hibernate 7 on 8.0.0-RC1 or RC2 use a DTYPE column with simple subclass names. To keep that layout, map discriminator column: 'DTYPE' on the root class and each subclass's simple name as its discriminator value. See the upgrade guide, section 26.10.

Related

#16470 fixes a separate bug in the same area: a query on an intermediate class (Car) misses its subclasses' rows (SportsCar). The hierarchy in #16465 has that shape, so an application needs both fixes to read its existing data fully. This PR doesn't overlap with #16470's files.

🤖 Generated with Claude Code

A table-per-hierarchy tree without a discriminator mapping used a DTYPE
column with simple subclass names. It now uses a column named class
that stores the fully qualified class name of each row, as
grails-data-hibernate5 and the documentation do, so existing tables
validate and their rows load without a mapping change.

A root mapping that sets only the discriminator column or formula keeps
the class name as its value instead of failing to parse a null value.

Fixes apache#16465
@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.9446%. Comparing base (d13aa33) to head (f0ddfef).
⚠️ Report is 21 commits behind head on 8.0.x.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16471        +/-   ##
==================================================
+ Coverage     58.9274%   58.9446%   +0.0172%     
- Complexity      24440      24451        +11     
==================================================
  Files            2174       2175         +1     
  Lines          107366     107389        +23     
  Branches        19557      19562         +5     
==================================================
+ Hits            63268      63300        +32     
+ Misses          35235      35223        -12     
- Partials         8863       8866         +3     

see 10 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

This comment has been minimized.

@matrei matrei 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.

Reviewed at head e9e7740662. The base is 8.0.x at d13aa33282, which is also the merge-base, and the branch merges cleanly.

What I ran locally:

  • The grails.gorm.tests.inheritance specs, including the new TablePerHierarchyDefaultDiscriminatorSpec, plus the four discriminator and subclass binder specs, GrailsHibernatePersistentEntitySpec and HibernateToManyEntityOrderByBinderSpec: all pass.
  • TablePerHierarchyDefaultDiscriminatorSpec against the 8.0.x main sources: all 3 features fail with the errors from #16465 (wrong stored values, missing column [DTYPE], and Could not parse discriminator value 'null'). So the spec pins the fix.
  • A scratch spec on grails-data-hibernate5, since deleted, with a root that maps only discriminator column: 'kind' (see "Verified as correct").
  • A scratch spec on this head, since deleted, with a hasMany to a table-per-hierarchy subclass. It covers a named column, an unnamed column: [length: 200] and insert: true only. Each collection gets the right where clause and loads its rows.

Looks good to me. Restoring the grails-data-hibernate5 default is the right call, and the change is small and well covered. I only have one suggestion and a nit, and neither blocks.

Suggestions

1. Tell 8.0.0-RC users with Hibernate 7 tables what changes

The upgrade guide doesn't need anything, since an application coming from Grails 7 now gets the grails-data-hibernate5 default again. Anyone who built tables with grails-data-hibernate7 on 8.0.0-RC1 or RC2 has the opposite problem, though. Their tables have a DTYPE column holding example.Vehicle, Car and SportsCar. After this change, validate fails with missing column [class], and subclass rows no longer match.

A line in the release notes would cover it. Thanks to the column-only fix in this PR, those users can keep their RC layout with a mapping and no data migration:

class Vehicle {
    static mapping = {
        discriminator column: 'DTYPE'    // the root value stays the fully qualified name, as on the RCs
    }
}

class Car extends Vehicle {
    static mapping = {
        discriminator 'Car'
    }
}

2. Nit: the "Customising the Discriminator" example uses dtype

inheritanceStrategies.adoc now says the default column is class, and a few lines below, the closure example still names its column dtype. Readers may take that for the old default. Using content_type, as the other examples on the page do, would avoid the confusion.

Verified as correct

  • Without a mapping, the root gets its fully qualified name as the value (DefaultDiscriminatorBinder), and so does each subclass, through getDiscriminatorValue() and SingleTableSubclassBinder:58. The class column matches RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, which grails-data-hibernate5 uses.
  • On grails-data-hibernate5, a root that maps only the column stores the literal string 'null' for root rows, and list() then fails with Object [id=1] was not of the specified subclass ... Discriminator: null. So no existing Hibernate 5 data depends on that mapping, and using the class name for the root is a strict improvement. It's new behaviour rather than restored behaviour, which the PR description correctly doesn't claim.
  • getDiscriminatorColumnName() falls back to class when the column config has no name (Optional.map returns empty), so a mapping like column: [length: 200] or insert: true, which used to fail at startup and now binds, still gets a correct collection where clause.
  • class is not in Hibernate 7's Template keyword list, so the collection where fragment is aliased like any other column, as it was on Hibernate 5.
  • JPA_DEFAULT_DISCRIMINATOR_TYPE has no remaining users in the repository. Dropping it before 8.0.0 GA is fine. The DTYPE assertion in HibernateEjb3DatabaseTest is about a plain JPA entity, so it's Hibernate's own default and is unaffected.

Tables created by 8.0.0-RC1 or RC2 have a DTYPE column with simple
subclass names. The upgrade guide shows the mapping that keeps that
layout, and a spec checks it validates and reads such a table. The
discriminator customisation example no longer names its column dtype.
@jdaugherty
jdaugherty merged commit 2b2afb8 into apache:8.0.x Oct 2, 2026
85 checks passed
@jdaugherty
jdaugherty deleted the fix/16465-h7-default-discriminator branch October 2, 2026 13:04
borinquenkid added a commit that referenced this pull request Oct 5, 2026
The merged default discriminator column is named class instead of DTYPE
(#16471). Regenerate the snapshot; nothing else in it changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
borinquenkid added a commit that referenced this pull request Oct 10, 2026
The merged default discriminator column is named class instead of DTYPE
(#16471). Regenerate the snapshot; nothing else in it changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

grails-data-hibernate7: the default discriminator changed from a class column with fully qualified names to a DTYPE column with simple names

2 participants