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


The following commit(s) were added to refs/heads/9.0.x-h7-improvement-phase3 by 
this push:
     new cd301cbeab fix(hibernate7): resolve every column of an enum 
unique-group member in the native binding
cd301cbeab is described below

commit cd301cbeab8ac717aae6464786d58a45343ae0e9
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Thu Oct 22 13:38:40 2026 -0500

    fix(hibernate7): resolve every column of an enum unique-group member in the 
native binding
    
    Mirror of #16720 (fix/enum-unique-group-multicolumn-8.0.x, #16713) on the
    native-binding branch, ahead of its merge-up. CreateKeyForProps, which the
    generator's constraint facets still use, adds each configured column of a
    unique-group member to the key instead of asking the single-column accessor,
    which threw for a to-one association mapped to more than one column; a
    member without explicit columns keeps the fallback.
    
    EnumUniqueGroupAssociationSpec from that pull request runs against the
    native binding here, and the classic oracle records its fixtures.
---
 .../domainbinding/util/CreateKeyForProps.groovy    | 15 +++-
 .../tests/EnumUniqueGroupAssociationSpec.groovy    | 86 ++++++++++++++++++++++
 .../cfg/domainbinding/CreateKeyForPropsSpec.groovy | 26 ++++---
 .../resources/classic-oracle/ddl-differential.txt  |  9 +++
 .../classic-oracle/generator-differential.txt      |  3 +
 5 files changed, 126 insertions(+), 13 deletions(-)

diff --git 
a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.groovy
 
b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.groovy
index 6eeb6455e7..5c3f02384e 100644
--- 
a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.groovy
+++ 
b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.groovy
@@ -23,6 +23,7 @@ import org.hibernate.MappingException
 import org.hibernate.mapping.Column
 import org.hibernate.mapping.Table
 
+import org.grails.orm.hibernate.cfg.ColumnConfig
 import org.grails.orm.hibernate.cfg.PropertyConfig
 import 
org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
 import 
org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
@@ -69,9 +70,17 @@ class CreateKeyForProps {
                     throw new MappingException(
                             "${owner.javaClass.name} references an unknown 
property ${propertyName}".toString())
                 }
-                String otherColumnName =
-                        
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, null)
-                keyList.add(new Column(otherColumnName))
+                // every configured column of the member joins the key: a 
to-one association mapped to more than one
+                // column has no single column name to ask for; a member 
without explicit columns keeps the fallback
+                List<ColumnConfig> columns = 
otherProp.hibernateMappedForm.columns
+                if (columns.isEmpty()) {
+                    columns = [null]
+                }
+                for (ColumnConfig column : columns) {
+                    String otherColumnName =
+                            
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, column)
+                    keyList.add(new Column(otherColumnName))
+                }
             }
 
             uniqueKeyForColumnsCreator.createUniqueKeyForColumns(table, 
keyList)
diff --git 
a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumUniqueGroupAssociationSpec.groovy
 
b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumUniqueGroupAssociationSpec.groovy
new file mode 100644
index 0000000000..61651d7421
--- /dev/null
+++ 
b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumUniqueGroupAssociationSpec.groovy
@@ -0,0 +1,86 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package grails.gorm.tests
+
+import grails.gorm.annotation.Entity
+import grails.gorm.hibernate.HibernateEntity
+import groovy.transform.EqualsAndHashCode
+import spock.lang.Issue
+
+@Issue('https://github.com/apache/grails-core/issues/16713')
+class EnumUniqueGroupAssociationSpec extends HibernateGormDatastoreSpec {
+
+    void setupSpec() {
+        manager.registerDomainClasses(EnumUniqueCompositeParent, 
EnumUniqueCompositeChild)
+    }
+
+    void "an enum unique group includes every mapped column of a composite 
parent"() {
+        given:
+        def table = 
datastore.metadata.getEntityBinding(EnumUniqueCompositeChild.name).table
+
+        expect:
+        table.uniqueKeys.values().collect { it.columns*.name.toSet() } ==
+                [['child_state', 'parent_code', 'parent_revision'] as Set]
+        table.getForeignKeys().values().any { it.columns*.name.toSet() == 
['parent_code', 'parent_revision'] as Set }
+    }
+
+    void "the same enum can be saved for parents differing in either 
identifier column"() {
+        when:
+        [['a', 1], ['a', 2], ['b', 1]].each { code, revision ->
+            def parent = new EnumUniqueCompositeParent(code: code, revision: 
revision).save(failOnError: true)
+            new EnumUniqueCompositeChild(parent: parent, state: 
EnumUniqueCompositeState.ON).save(failOnError: true)
+        }
+        manager.session.flush()
+        manager.session.clear()
+
+        then:
+        EnumUniqueCompositeChild.count() == 3
+        EnumUniqueCompositeChild.list()*.parent.collect { [it.code, 
it.revision] }.toSet() ==
+                [['a', 1], ['a', 2], ['b', 1]] as Set
+    }
+}
+
+enum EnumUniqueCompositeState {
+    ON, OFF
+}
+
+@Entity
+@EqualsAndHashCode(includes = ['code', 'revision'])
+class EnumUniqueCompositeParent implements 
HibernateEntity<EnumUniqueCompositeParent>, Serializable {
+    String code
+    Integer revision
+
+    static mapping = {
+        id composite: ['code', 'revision']
+    }
+}
+
+@Entity
+class EnumUniqueCompositeChild implements 
HibernateEntity<EnumUniqueCompositeChild> {
+    EnumUniqueCompositeParent parent
+    EnumUniqueCompositeState state
+
+    static mapping = {
+        state column: 'child_state', unique: 'parent'
+        parent {
+            column name: 'parent_code'
+            column name: 'parent_revision'
+        }
+    }
+}
diff --git 
a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
 
b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
index 260ffaadda..73acf1f9cb 100644
--- 
a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
+++ 
b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy
@@ -19,6 +19,7 @@
 
 package org.grails.orm.hibernate.cfg.domainbinding
 
+import org.grails.orm.hibernate.cfg.PropertyConfig
 import 
org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
 import 
org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
 import 
org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
@@ -50,8 +51,12 @@ class CreateKeyForPropsSpec extends Specification {
         }
         grailsProp.getMappedForm() >> mappedForm
 
-        def otherProp1 = Mock(HibernatePersistentProperty)
-        def otherProp2 = Mock(HibernatePersistentProperty)
+        def otherProp1 = Mock(HibernatePersistentProperty) {
+            getHibernateMappedForm() >> new PropertyConfig()
+        }
+        def otherProp2 = Mock(HibernatePersistentProperty) {
+            getHibernateMappedForm() >> new PropertyConfig()
+        }
         owner.getHibernatePropertyByName("p1") >> otherProp1
         owner.getHibernatePropertyByName("p2") >> otherProp2
 
@@ -59,9 +64,6 @@ class CreateKeyForPropsSpec extends Specification {
         def table = new Table("t")
         String baseColumnName = "base_col"
 
-        columnNameFetcher.getColumnNameForPropertyAndPath(otherProp1, path, 
null) >> "col1"
-        columnNameFetcher.getColumnNameForPropertyAndPath(otherProp2, path, 
null) >> "col2"
-
         when:
         subject.createKeyForProps(grailsProp, path, table, baseColumnName)
 
@@ -73,9 +75,9 @@ class CreateKeyForPropsSpec extends Specification {
         1 * mappedForm.getUniquenessGroup() >> ["p1", "p2"]
         1 * owner.getHibernatePropertyByName("p1") >> otherProp1
         1 * owner.getHibernatePropertyByName("p2") >> otherProp2
-        1 * columnNameFetcher.getColumnNameForPropertyAndPath(otherProp1, 
path, null)
-        1 * columnNameFetcher.getColumnNameForPropertyAndPath(otherProp2, 
path, null)
-        1 * uniqueKeyCreator.createUniqueKeyForColumns(table, _ as List)
+        1 * columnNameFetcher.getColumnNameForPropertyAndPath(otherProp1, 
path, null) >> "col1"
+        1 * columnNameFetcher.getColumnNameForPropertyAndPath(otherProp2, 
path, null) >> "col2"
+        1 * uniqueKeyCreator.createUniqueKeyForColumns(table, { List cols -> 
cols*.name == ["base_col", "col1", "col2"] })
     }
 
     def "does nothing when property is not unique within group"() {
@@ -165,7 +167,9 @@ class CreateKeyForPropsSpec extends Specification {
         def uniqueKeyCreator = Mock(UniqueKeyForColumnsCreator)
         def subject = new CreateKeyForProps(columnNameFetcher, 
uniqueKeyCreator)
         def owner = Mock(GrailsHibernatePersistentEntity)
-        def otherProp = Mock(HibernatePersistentProperty)
+        def otherProp = Mock(HibernatePersistentProperty) {
+            getHibernateMappedForm() >> new PropertyConfig()
+        }
         def mappedForm = Mock(org.grails.orm.hibernate.cfg.PropertyConfig) {
             isUnique() >> true
             isUniqueWithinGroup() >> true
@@ -193,7 +197,9 @@ class CreateKeyForPropsSpec extends Specification {
         def uniqueKeyCreator = Mock(UniqueKeyForColumnsCreator)
         def subject = new CreateKeyForProps(columnNameFetcher, 
uniqueKeyCreator)
         def owner = Mock(GrailsHibernatePersistentEntity)
-        def otherProp = Mock(HibernatePersistentProperty)
+        def otherProp = Mock(HibernatePersistentProperty) {
+            getHibernateMappedForm() >> new PropertyConfig()
+        }
         def mappedForm = Mock(org.grails.orm.hibernate.cfg.PropertyConfig) {
             isUnique() >> true
             isUniqueWithinGroup() >> true
diff --git 
a/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt
 
b/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt
index 08917b1734..f89fc8ddf6 100644
--- 
a/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt
+++ 
b/grails-data-hibernate7/core/src/test/resources/classic-oracle/ddl-differential.txt
@@ -113,6 +113,15 @@ script             "ALTER TABLE \"PUBLIC\".\"LICENSE\" ADD 
CONSTRAINT \"PUBLIC\".\"FK45XAAT
 table  enum_entity_domain      
{"checks":[],"collections":[],"columnOrder":["id","version","status"],"columns":{"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":true,"nullable":false,"type":"bigint","unique":false},"status":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":true,"type":"varchar(255)","unique":false},"version":{"checks":[],"collation":null,"comment":null,"default":null,"generat
 [...]
 script         "CREATE MEMORY TABLE \"PUBLIC\".\"ENUM_ENTITY_DOMAIN\"(\n    
\"ID\" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL,\n    
\"VERSION\" BIGINT NOT NULL,\n    \"STATUS\" CHARACTER VARYING(255)\n);"
 script         "ALTER TABLE \"PUBLIC\".\"ENUM_ENTITY_DOMAIN\" ADD CONSTRAINT 
\"PUBLIC\".\"CONSTRAINT_#\" PRIMARY KEY(\"ID\");"
+@group grails.gorm.tests.EnumUniqueCompositeChild      
{"members":["grails.gorm.tests.EnumUniqueCompositeChild","grails.gorm.tests.EnumUniqueCompositeParent"],"source":"native"}
+table  enum_unique_composite_child     
{"checks":[],"collections":[],"columnOrder":["parent_revision","id","version","parent_code","child_state"],"columns":{"child_state":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":true,"type":"enum
 
('OFF','ON')","unique":false},"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":true,"nullable":false,"type":"bigint","unique":false},"parent_code":{"checks
 [...]
+table  enum_unique_composite_parent    
{"checks":[],"collections":[],"columnOrder":["revision","version","code"],"columns":{"code":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":false,"type":"varchar(255)","unique":false},"revision":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":false,"type":"integer","unique":false},"version":{"checks":[],"collation":null,"comment":null,"d
 [...]
+script         "CREATE MEMORY TABLE 
\"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_CHILD\"(\n    \"PARENT_REVISION\" INTEGER,\n 
   \"ID\" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL,\n    
\"VERSION\" BIGINT NOT NULL,\n    \"PARENT_CODE\" CHARACTER VARYING(255),\n    
\"CHILD_STATE\" ENUM('OFF', 'ON')\n);"
+script         "ALTER TABLE \"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_CHILD\" ADD 
CONSTRAINT \"PUBLIC\".\"CONSTRAINT_#\" PRIMARY KEY(\"ID\");"
+script         "CREATE MEMORY TABLE 
\"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_PARENT\"(\n    \"REVISION\" INTEGER NOT 
NULL,\n    \"VERSION\" BIGINT NOT NULL,\n    \"CODE\" CHARACTER VARYING(255) 
NOT NULL\n);"
+script         "ALTER TABLE \"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_PARENT\" ADD 
CONSTRAINT \"PUBLIC\".\"CONSTRAINT_#\" PRIMARY KEY(\"REVISION\", \"CODE\");"
+script         "ALTER TABLE \"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_CHILD\" ADD 
CONSTRAINT \"PUBLIC\".\"CONSTRAINT_#\" UNIQUE NULLS DISTINCT 
(\"PARENT_REVISION\", \"PARENT_CODE\", \"CHILD_STATE\");"
+script         "ALTER TABLE \"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_CHILD\" ADD 
CONSTRAINT \"PUBLIC\".\"FK7WHVPB86UO2LJ079QXEYB20NX\" FOREIGN 
KEY(\"PARENT_CODE\", \"PARENT_REVISION\") REFERENCES 
\"PUBLIC\".\"ENUM_UNIQUE_COMPOSITE_PARENT\"(\"CODE\", \"REVISION\") NOCHECK;"
 @group grails.gorm.tests.FetchJoinChild        
{"members":["grails.gorm.tests.FetchJoinChild","grails.gorm.tests.FetchJoinParent"]}
 table  fetch_join_child        {"checks":[],"collections":["Set of OneToMany 
(inverse)"],"columnOrder":["id","parent_id","version","name"],"columns":{"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":true,"nullable":false,"type":"bigint","unique":false},"name":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":true,"type":"varchar(255)","unique":false},"parent_id":{"checks":[],"collation":null,"c
 [...]
 table  fetch_join_parent       
{"checks":[],"collections":[],"columnOrder":["id","version","name"],"columns":{"id":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":true,"nullable":false,"type":"bigint","unique":false},"name":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs":null,"identity":false,"nullable":true,"type":"varchar(255)","unique":false},"version":{"checks":[],"collation":null,"comment":null,"default":null,"generatedAs"
 [...]
diff --git 
a/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt
 
b/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt
index f64d63d6c9..6fab99c96f 100644
--- 
a/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt
+++ 
b/grails-data-hibernate7/core/src/test/resources/classic-oracle/generator-differential.txt
@@ -43,6 +43,9 @@ entity        grails.gorm.tests.Driver        
{"abstractClass":false,"batchSize":0,"cached":fa
 entity grails.gorm.tests.License       
{"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","driver"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.License","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"License","kind":"RootClass","lazyPropertiesCacheable":true,"mu
 [...]
 @group grails.gorm.tests.EnumEntityDomain      
{"members":["grails.gorm.tests.EnumEntityDomain"]}
 entity grails.gorm.tests.EnumEntityDomain      
{"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","status"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.EnumEntityDomain","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"EnumEntityDomain","kind":"RootClass","lazyPr
 [...]
+@group grails.gorm.tests.EnumUniqueCompositeChild      
{"members":["grails.gorm.tests.EnumUniqueCompositeChild","grails.gorm.tests.EnumUniqueCompositeParent"],"source":"native"}
+entity grails.gorm.tests.EnumUniqueCompositeChild      
{"abstractClass":false,"batchSize":-1,"cacheConcurrencyStrategy":"read-write","cached":false,"declaredNames":["parent","state","version"],"discriminatorInsertable":true,"discriminatorValue":"EnumUniqueCompositeChild","dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.EnumUniqueCompositeChild","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"Ba
 [...]
+entity grails.gorm.tests.EnumUniqueCompositeParent     
{"abstractClass":false,"batchSize":-1,"cacheConcurrencyStrategy":"read-write","cached":false,"declaredNames":["_identifierMapper","version"],"discriminatorInsertable":true,"discriminatorValue":"EnumUniqueCompositeParent","dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.EnumUniqueCompositeParent","identifier":{"embedded":true,"kind":"Component","members":[{"backref":false,"insertable":true,"lazy":false,"name":"c
 [...]
 @group grails.gorm.tests.FetchJoinChild        
{"members":["grails.gorm.tests.FetchJoinChild","grails.gorm.tests.FetchJoinParent"]}
 entity grails.gorm.tests.FetchJoinChild        
{"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","name","parent"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.FetchJoinChild","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"FetchJoinChild","kind":"RootClass","lazyP
 [...]
 entity grails.gorm.tests.FetchJoinParent       
{"abstractClass":false,"batchSize":0,"cached":false,"declaredNames":["version","name","children"],"discriminatorInsertable":true,"dynamicInsert":false,"dynamicUpdate":false,"entityName":"grails.gorm.tests.FetchJoinParent","generator":{"className":"org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsNativeGenerator"},"identifierKind":"BasicValue","identifierProperty":"id","joinTables":{},"jpaEntityName":"FetchJoinParent","kind":"RootClass","
 [...]

Reply via email to