You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/10/06 18:38:18 UTC

(grails-core) 10/14: fix: take the identifier of a table per hierarchy from its root in the GORM column snapshot

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase3
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 1269efa13ec7a05dca5be2eb29f173a258715536
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Oct 6 11:46:13 2026 -0500

    fix: take the identifier of a table per hierarchy from its root in the GORM column snapshot
    
    GormColumnSnapshotGenerator looked up the first entity bound to the table of a
    column. The domain binder lists a single table subclass before its root, so the
    identifier test (root class only) failed and the id column of the hierarchy lost
    its autoIncrement in the snapshot and in the generated changelog.
    
    The generated domain classes list the root first, which the snapshot comparison
    between the two modes made visible.
    
    Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
---
 .../liquibase/GormColumnSnapshotGenerator.groovy   |  8 ++-
 .../GormColumnSnapshotGeneratorSpec.groovy         | 57 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGenerator.groovy b/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGenerator.groovy
index 39b99c8fed..bae0b42f2c 100644
--- a/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGenerator.groovy
+++ b/grails-data-hibernate7/dbmigration/src/main/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGenerator.groovy
@@ -86,8 +86,12 @@ class GormColumnSnapshotGenerator implements SnapshotGenerator {
 
         GrailsHibernatePersistentEntity gpe = (GrailsHibernatePersistentEntity) entity
 
-        if (isIdentifier(pc, column.name)) {
-            applyGormIdentitySettings(column, gpe)
+        // a subclass of a table per hierarchy shares the table of its root, and Hibernate may list it first: the identifier
+        // and its generator belong to the root
+        PersistentClass identifierOwner = pc.table.is(pc.rootClass.table) ? pc.rootClass : pc
+        if (isIdentifier(identifierOwner, column.name)) {
+            PersistentEntity owner = mappingContext.getPersistentEntity(identifierOwner.className ?: identifierOwner.entityName)
+            applyGormIdentitySettings(column, owner instanceof GrailsHibernatePersistentEntity ? (GrailsHibernatePersistentEntity) owner : gpe)
         } else {
             PersistentProperty prop = resolveGormProperty(gpe, column.name)
             if (prop) {
diff --git a/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGeneratorSpec.groovy b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGeneratorSpec.groovy
index 54f28ebcd9..4355ace280 100644
--- a/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGeneratorSpec.groovy
+++ b/grails-data-hibernate7/dbmigration/src/test/groovy/org/grails/plugins/databasemigration/liquibase/GormColumnSnapshotGeneratorSpec.groovy
@@ -40,6 +40,7 @@ import org.hibernate.boot.spi.MetadataBuildingOptions
 import org.hibernate.dialect.H2Dialect
 import org.hibernate.mapping.PersistentClass
 import org.hibernate.mapping.RootClass
+import org.hibernate.mapping.SingleTableSubclass
 import org.hibernate.mapping.BasicValue
 import org.hibernate.mapping.Table as HibernateTable
 import spock.lang.Specification
@@ -220,4 +221,60 @@ class GormColumnSnapshotGeneratorSpec extends Specification {
         !result.isNullable()
         result.getAutoIncrementInformation() != null
     }
+
+    def "the identifier of a table per hierarchy root gets the identity settings when a subclass of the table is listed first"() {
+        given:
+        Column example = new Column(name: "id")
+        Table table = new Table(name: "test_table")
+        example.setRelation(table)
+
+        Column chainResult = new Column(name: "id")
+        chainResult.setRelation(table)
+        chainResult.setNullable(true)
+
+        SnapshotGeneratorChain chain = Mock()
+        DatabaseSnapshot snapshot = Mock()
+        GormDatabase database = Mock()
+        HibernateDatastore datastore = Mock()
+        Metadata metadata = Mock()
+        HibernateMappingContext mappingContext = Mock()
+        MetadataBuildingContext buildingContext = createMetadataBuildingContext()
+
+        RootClass root = new RootClass(buildingContext)
+        root.setEntityName("TestEntity")
+        root.setClassName("com.example.TestEntity")
+        HibernateTable hTable = new HibernateTable("hibernate", "test_table")
+        root.setTable(hTable)
+        BasicValue identifier = new BasicValue(buildingContext, hTable)
+        identifier.addColumn(new org.hibernate.mapping.Column("id"))
+        root.setIdentifier(identifier)
+        SingleTableSubclass sub = new SingleTableSubclass(root, buildingContext)
+        sub.setEntityName("SubEntity")
+        sub.setClassName("com.example.SubEntity")
+
+        GrailsHibernatePersistentEntity rootEntity = Mock()
+        GrailsHibernatePersistentEntity subEntity = Mock()
+        Mapping gormMapping = Mock()
+        HibernateSimpleIdentity gormIdentity = Mock()
+
+        when:
+        Column result = generator.snapshot(example, snapshot, chain)
+
+        then:
+        1 * chain.snapshot(example, snapshot) >> chainResult
+        snapshot.database >> database
+        database.gormDatastore >> datastore
+        database.metadata >> metadata
+        datastore.mappingContext >> mappingContext
+
+        metadata.getEntityBindings() >> [sub, root]
+        mappingContext.getPersistentEntity("com.example.TestEntity") >> rootEntity
+        mappingContext.getPersistentEntity("com.example.SubEntity") >> subEntity
+        rootEntity.getMappedForm() >> gormMapping
+        gormMapping.getIdentity() >> gormIdentity
+        gormIdentity.determineGeneratorName(_) >> "identity"
+
+        !result.isNullable()
+        result.getAutoIncrementInformation() != null
+    }
 }