jamesfredley commented on code in PR #16720:
URL: https://github.com/apache/grails-core/pull/16720#discussion_r4406977597


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java:
##########
@@ -70,9 +72,15 @@ public void createKeyForProps(HibernatePersistentProperty 
grailsProp, String pat
                     throw new MappingException(
                             owner.getJavaClass().getName() + " references an 
unknown property " + propertyName);
                 }
-                String otherColumnName =
-                        
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, null);
-                keyList.add(new Column(otherColumnName));
+                List<ColumnConfig> columns = 
otherProp.getHibernateMappedForm().getColumns();
+                if (columns.isEmpty()) {
+                    columns = Collections.singletonList(null);
+                }
+                for (ColumnConfig column : columns) {
+                    String otherColumnName =
+                            
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, column);
+                    keyList.add(new Column(otherColumnName));
+                }

Review Comment:
   Left the mapped form unguarded. The binding contract does not produce a null 
mapped form or a null columns list here.



##########
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]

Review Comment:
   Kept the exact unique-key assertion. This table exists for that one key, so 
an extra key would mean the mapping is wrong.



##########
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

Review Comment:
   CreateKeyForPropsSpec now uses the imported PropertyConfig name throughout.



##########
grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy:
##########
@@ -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) {

Review Comment:
   CreateKeyForPropsSpec now uses the imported PropertyConfig name throughout.



##########
grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CreateKeyForPropsSpec.groovy:
##########
@@ -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) {

Review Comment:
   CreateKeyForPropsSpec now uses the imported PropertyConfig name throughout.



##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java:
##########
@@ -70,9 +72,15 @@ public void createKeyForProps(HibernatePersistentProperty 
grailsProp, String pat
                     throw new MappingException(
                             owner.getJavaClass().getName() + " references an 
unknown property " + propertyName);
                 }
-                String otherColumnName =
-                        
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, null);
-                keyList.add(new Column(otherColumnName));
+                List<ColumnConfig> columns = 
otherProp.getHibernateMappedForm().getColumns();
+                if (columns.isEmpty()) {

Review Comment:
   Unique-group columns are resolved in Hibernate's second pass, after a 
composite association has its generated columns. EnumUniqueGroupAssociationSpec 
covers declared-first enum and String members, and a second child with the same 
parent and state is rejected.



##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/CreateKeyForProps.java:
##########
@@ -70,9 +72,15 @@ public void createKeyForProps(HibernatePersistentProperty 
grailsProp, String pat
                     throw new MappingException(
                             owner.getJavaClass().getName() + " references an 
unknown property " + propertyName);
                 }
-                String otherColumnName =
-                        
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, null);
-                keyList.add(new Column(otherColumnName));
+                List<ColumnConfig> columns = 
otherProp.getHibernateMappedForm().getColumns();
+                if (columns.isEmpty()) {
+                    columns = Collections.singletonList(null);
+                }
+                for (ColumnConfig column : columns) {
+                    String otherColumnName =
+                            
columnNameForPropertyAndPathFetcher.getColumnNameForPropertyAndPath(otherProp, 
path, column);
+                    keyList.add(new Column(otherColumnName));
+                }
             }
 
             uniqueKeyForColumnsCreator.createUniqueKeyForColumns(table, 
keyList);

Review Comment:
   Left the constrained multi-column association for a follow-up, as suggested. 
This PR stays on the group-member columns for #16713.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to