Repository navigation
Hibernate 7: restore the class discriminator column with fully qualified names - #16471
Conversation
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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 🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
matrei
left a comment
There was a problem hiding this comment.
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.inheritancespecs, including the newTablePerHierarchyDefaultDiscriminatorSpec, plus the four discriminator and subclass binder specs,GrailsHibernatePersistentEntitySpecandHibernateToManyEntityOrderByBinderSpec: all pass. TablePerHierarchyDefaultDiscriminatorSpecagainst the8.0.xmain sources: all 3 features fail with the errors from #16465 (wrong stored values,missing column [DTYPE], andCould 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 onlydiscriminator column: 'kind'(see "Verified as correct"). - A scratch spec on this head, since deleted, with a
hasManyto a table-per-hierarchy subclass. It covers a named column, an unnamedcolumn: [length: 200]andinsert: trueonly. Each collection gets the rightwhereclause 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, throughgetDiscriminatorValue()andSingleTableSubclassBinder:58. Theclasscolumn matchesRootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME, whichgrails-data-hibernate5uses. - On
grails-data-hibernate5, a root that maps only the column stores the literal string'null'for root rows, andlist()then fails withObject [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 toclasswhen the column config has no name (Optional.mapreturns empty), so a mapping likecolumn: [length: 200]orinsert: true, which used to fail at startup and now binds, still gets a correct collectionwhereclause.classis not in Hibernate 7'sTemplatekeyword list, so the collectionwherefragment is aliased like any other column, as it was on Hibernate 5.JPA_DEFAULT_DISCRIMINATOR_TYPEhas no remaining users in the repository. Dropping it before 8.0.0 GA is fine. TheDTYPEassertion inHibernateEjb3DatabaseTestis 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.
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>
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>
Fixes #16465
Problem
A table-per-hierarchy tree without a discriminator mapping behaved differently in
grails-data-hibernate7than ingrails-data-hibernate5:DTYPEinstead ofclass. A table created bygrails-data-hibernate5then faileddbCreate: 'validate'withmissing column [DTYPE].Car) instead of the fully qualified name (example.Car), so existing rows did not match.discriminator column: 'class') failed at startup withCould not parse discriminator value 'null'.The Hibernate 7 docs already describe a
classcolumn, and theDTYPEdefault dates from the initial Hibernate 7 port, so this restores the intended default rather than adding a setting.Changes
class(GrailsDomainBinder.DEFAULT_DISCRIMINATOR_COLUMN_NAME, which replacesJPA_DEFAULT_DISCRIMINATOR_TYPE). Hibernate 7 no longer providesRootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME.DTYPElayout with a mapping.Tests
New
TablePerHierarchyDefaultDiscriminatorSpec, through the public GORM API:classcolumn and loads polymorphically;dbCreate: 'validate'succeeds against a hand-builtgrails-data-hibernate5-style table, and its existing rows load as the right classes;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,-dbmigrationand-dbmigration-coretests pass, and CodeNarc and Checkstyle are clean.Release notes
Hibernate 7 again names the default table-per-hierarchy discriminator column
classand stores fully qualified class names, as Grails 7 did. Tables created with Hibernate 7 on 8.0.0-RC1 or RC2 use aDTYPEcolumn with simple subclass names. To keep that layout, mapdiscriminator 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