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

borinquenkid pushed a commit to branch merge-hibernate6
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 7f39051a3022178c641d5f06a22563e26879f174
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Wed Jul 9 21:45:10 2025 -0500

    refactoring and testing SimpleValueBinder
---
 .../orm/hibernate/cfg/GrailsDomainBinder.java      | 37 ++++-----------
 .../cfg/domainbinding/SimpleValueBinder.java       | 30 ++++++++++++
 .../cfg/domainbinding/SimpleValueBinderSpec.groovy | 53 ++++++++++++++++++++++
 3 files changed, 91 insertions(+), 29 deletions(-)

diff --git 
a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
 
b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
index accec50d97..124525a3fd 100644
--- 
a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
+++ 
b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
@@ -217,8 +217,9 @@ public class GrailsDomainBinder implements 
MetadataContributor {
 
         SimpleValue value = new SimpleValue(metadataBuildingContext, 
map.getCollectionTable());
 
-        bindSimpleValue(getIndexColumnType(property, STRING_TYPE), value, true,
-                getIndexColumnName(property, sessionFactoryBeanName), 
mappings);
+        String type = getIndexColumnType(property, STRING_TYPE);
+        String columnName1 = getIndexColumnName(property, 
sessionFactoryBeanName);
+        new SimpleValueBinder().bindSimpleValue(value, type, columnName1, 
true);
         PropertyConfig pc = getPropertyConfig(property);
         if (pc != null && pc.getIndexColumn() != null) {
             bindColumnConfigToColumn(property, getColumnForSimpleValue(value), 
getSingleColumnConfig(pc.getIndexColumn()));
@@ -245,7 +246,8 @@ public class GrailsDomainBinder implements 
MetadataContributor {
             if(typeName == null || typeName.equals(Object.class.getName())) {
                 typeName = StandardBasicTypes.STRING.getName();
             }
-            bindSimpleValue(typeName, elt, false, getMapElementName(property, 
sessionFactoryBeanName), mappings);
+            String columnName = getMapElementName(property, 
sessionFactoryBeanName);
+            new SimpleValueBinder().bindSimpleValue(elt, typeName, columnName, 
false);
 
             elt.setTypeName(typeName);
         }
@@ -278,7 +280,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
 
         Table collectionTable = list.getCollectionTable();
         SimpleValue iv = new SimpleValue(metadataBuildingContext, 
collectionTable);
-        bindSimpleValue("integer", iv, true, columnName, mappings);
+        new SimpleValueBinder().bindSimpleValue(iv, "integer", columnName, 
true);
         iv.setTypeName("integer");
         list.setIndex(iv);
         list.setBaseIndex(0);
@@ -700,7 +702,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
                     throw new MappingException("Missing type or column for 
column["+columnName+"] on domain["+domainName+"] referencing["+className+"]");
                 }
 
-                bindSimpleValue(typeName, element,true, columnName, mappings);
+                new SimpleValueBinder().bindSimpleValue(element, typeName, 
columnName, true);
                 if (hasJoinColumnMapping) {
                     bindColumnConfigToColumn(property, 
getColumnForSimpleValue(element), config.getJoinTable().getColumn());
                 }
@@ -722,7 +724,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
                     columnName = 
namingStrategy.propertyToColumnName(NameUtils.decapitalize(domainClass.getName()))
 + FOREIGN_KEY_SUFFIX;
                 }
 
-                bindSimpleValue("long", element,true, columnName, mappings);
+                new SimpleValueBinder().bindSimpleValue(element, "long", 
columnName, true);
             }
         }
 
@@ -2934,29 +2936,6 @@ public class GrailsDomainBinder implements 
MetadataContributor {
         }
     }
 
-    /**
-     * Binds a value for the specified parameters to the meta model.
-     *
-     * @param type        The type of the property
-     * @param simpleValue The simple value instance
-     * @param nullable    Whether it is nullable
-     * @param columnName  The property name
-     * @param mappings    The mappings
-     */
-    protected void bindSimpleValue(String type, SimpleValue simpleValue, 
boolean nullable,
-                                   String columnName, 
InFlightMetadataCollector mappings) {
-
-        simpleValue.setTypeName(type);
-        Table t = simpleValue.getTable();
-        Column column = new Column();
-        column.setNullable(nullable);
-        column.setValue(simpleValue);
-        column.setName(columnName);
-        if (t != null) t.addColumn(column);
-
-        simpleValue.addColumn(column);
-    }
-
     /**
      * Binds a Column instance to the Hibernate meta model
      *
diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinder.java
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinder.java
new file mode 100644
index 0000000000..e72f1db106
--- /dev/null
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinder.java
@@ -0,0 +1,30 @@
+package org.grails.orm.hibernate.cfg.domainbinding;
+
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.SimpleValue;
+
+import static java.util.Optional.ofNullable;
+
+public class SimpleValueBinder {
+
+    /**
+     * Binds a value for the specified parameters to the meta model.
+     *
+     * @param simpleValue The simple value instance
+     * @param type        The type of the property
+     * @param columnName  The property name
+     * @param nullable    Whether it is nullable
+     */
+    public void bindSimpleValue(SimpleValue simpleValue, String type, String 
columnName, boolean nullable) {
+        Column column = new Column();
+        column.setNullable(nullable);
+        column.setValue(simpleValue);
+        column.setName(columnName);
+        ofNullable(simpleValue.getTable())
+                .ifPresent(
+                        table -> table.addColumn(column)
+                );
+        simpleValue.getSelectables().add(column);
+        simpleValue.setTypeName(type);
+    }
+}
diff --git 
a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinderSpec.groovy
 
b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinderSpec.groovy
new file mode 100644
index 0000000000..7764285492
--- /dev/null
+++ 
b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleValueBinderSpec.groovy
@@ -0,0 +1,53 @@
+package org.grails.orm.hibernate.cfg.domainbinding
+
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.hibernate.mapping.BasicValue
+import org.hibernate.mapping.Column
+import org.hibernate.mapping.Table
+
+class SimpleValueBinderSpec extends HibernateGormDatastoreSpec {
+
+    void "Test defaults"() {
+        when:
+        def type = "String"
+        def columnName = "columnName"
+        def tableName = "table"
+        def contributor = "contributor"
+        def nullable = false
+        def simpleValueBinder = new SimpleValueBinder()
+        Table table = new Table(contributor,tableName);
+        table.setName(tableName)
+        def grailsDomainBinder = getGrailsDomainBinder()
+        BasicValue simpleValue = new 
BasicValue(grailsDomainBinder.metadataBuildingContext, table);
+        simpleValueBinder.bindSimpleValue(simpleValue, type, columnName, 
nullable)
+
+        def column = (Column) simpleValue.column
+        then:
+        column
+        column.value == simpleValue
+        column.name == columnName
+        !column.nullable
+        simpleValue.column == column
+        table.getColumn(0) == column
+    }
+
+    void "Test no table"() {
+        when:
+        def type = "String"
+        def columnName = "columnName"
+        def nullable = true
+        def simpleValueBinder = new SimpleValueBinder()
+        def grailsDomainBinder = getGrailsDomainBinder()
+        BasicValue simpleValue = new 
BasicValue(grailsDomainBinder.metadataBuildingContext, null);
+        simpleValueBinder.bindSimpleValue(simpleValue, type, columnName, 
nullable)
+
+        def column = (Column) simpleValue.column
+        then:
+        column
+        column.value == simpleValue
+        column.name == columnName
+        column.nullable
+        simpleValue.column == column
+        !simpleValue.table
+    }
+}

Reply via email to