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 8c0b2ef330a66af89a3148252ec8f9d7cc9fa2aa
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Mon Jul 14 23:22:56 2025 -0500

    refactor more
---
 .../orm/hibernate/cfg/GrailsDomainBinder.java      | 37 +++++++---------------
 .../grails/orm/hibernate/cfg/PropertyConfig.groovy |  4 +++
 .../cfg/domainbinding/TypeNameProviderSpec.groovy  |  7 ++--
 .../mapping/model/PersistentProperty.java          |  6 ++++
 4 files changed, 25 insertions(+), 29 deletions(-)

diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
index 9fe86e8ac9..b92aa26aaf 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
@@ -118,7 +118,6 @@ import java.util.StringTokenizer;
 import static java.util.Optional.ofNullable;
 import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
 
-;
 
 /**
  * Handles the binding Grails domain classes and properties to the Hibernate 
runtime meta model.
@@ -496,7 +495,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
         if(referenced != null && !isManyToMany && referenced.isMultiTenant()) {
             String filterCondition = 
getMultiTenantFilterCondition(sessionFactoryBeanName, referenced);
             if(filterCondition != null) {
-                if (isUnidirectionalOneToMany(property)) {
+                if (property.isUnidirectionalOneToMany()) {
                     
collection.addManyToManyFilter(GormProperties.TENANT_IDENTITY, filterCondition, 
true, Collections.emptyMap(), Collections.emptyMap());
                 } else {
                     collection.addFilter(GormProperties.TENANT_IDENTITY, 
filterCondition, true, Collections.emptyMap(), Collections.emptyMap());
@@ -557,7 +556,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
         } else if (shouldCollectionBindWithJoinColumn(property)) {
             bindCollectionWithJoinTable(property, mappings, collection, 
propConfig, sessionFactoryBeanName);
 
-        } else if (isUnidirectionalOneToMany(property)) {
+        } else if 
(ofNullable(property).map(PersistentProperty::isUnidirectionalOneToMany).orElse(false))
 {
             // for non-inverse one-to-many, with a not-null fk, add a backref!
             // there are problems with list and map mappings and join columns 
relating to duplicate key constraints
             // TODO change this when HHH-1268 is resolved
@@ -740,13 +739,13 @@ public class GrailsDomainBinder implements 
MetadataContributor {
 
         String columnName;
 
-        final boolean hasJoinColumnMapping = hasJoinColumnMapping(config);
+        var joinColumnMappingOptional = 
Optional.ofNullable(config).map(PropertyConfig::getJoinTableColumnConfig);
         if (isBasicCollectionType) {
             final Class<?> referencedType = 
((Basic)property).getComponentType();
             String className = referencedType.getName();
             final boolean isEnum = referencedType.isEnum();
-            if (hasJoinColumnMapping) {
-                columnName = config.getJoinTable().getColumn().getName();
+            if (joinColumnMappingOptional.isPresent()) {
+                columnName = joinColumnMappingOptional.get().getName();
             }
             else {
                 var clazz = 
namingStrategy.toPhysicalColumnName(toIdentifier(className),getJdbcEnvironment());
@@ -773,9 +772,9 @@ public class GrailsDomainBinder implements 
MetadataContributor {
                 }
 
                 new SimpleValueBinder().bindSimpleValue(element, typeName, 
columnName, true);
-                if (hasJoinColumnMapping) {
+                if (joinColumnMappingOptional.isPresent()) {
                     Column column = getColumnForSimpleValue(element);
-                    ColumnConfig columnConfig = 
config.getJoinTable().getColumn();
+                    ColumnConfig columnConfig = 
joinColumnMappingOptional.get();
                     final PropertyConfig mappedForm = new 
PersistentPropertyToPropertyConfig().apply(property);
                     new 
ColumnConfigToColumnBinder().bindColumnConfigToColumn(column, columnConfig, 
mappedForm);
                 }
@@ -790,8 +789,8 @@ public class GrailsDomainBinder implements 
MetadataContributor {
                         EMPTY_PATH, sessionFactoryBeanName);
             }
             else {
-                if (hasJoinColumnMapping) {
-                    columnName = config.getJoinTable().getColumn().getName();
+                if (joinColumnMappingOptional.isPresent()) {
+                    columnName = joinColumnMappingOptional.get().getName();
                 }
                 else {
                     Identifier decapitalize = 
toIdentifier(NameUtils.decapitalize(domainClass.getName()));
@@ -819,15 +818,11 @@ public class GrailsDomainBinder implements 
MetadataContributor {
         return element.getColumns().iterator().next();
     }
 
-    private boolean hasJoinColumnMapping(PropertyConfig config) {
-        return config != null && config.getJoinTable() != null && 
config.getJoinTable().getColumn() != null;
-    }
-
     private boolean shouldCollectionBindWithJoinColumn(ToMany property) {
         PropertyConfig config = new 
PersistentPropertyToPropertyConfig().apply(property);
         JoinTable jt = config != null ? config.getJoinTable() : new 
JoinTable();
 
-        return (isUnidirectionalOneToMany(property) || (property instanceof 
Basic)) && jt != null;
+        return 
(ofNullable(property).map(PersistentProperty::isUnidirectionalOneToMany).orElse(false)
 || (property instanceof Basic)) && jt != null;
     }
 
     /**
@@ -871,16 +866,6 @@ public class GrailsDomainBinder implements 
MetadataContributor {
         }
     }
 
-    /**
-     * Checks whether a property is a unidirectional non-circular one-to-many
-     *
-     * @param property The property to check
-     * @return true if it is unidirectional and a one-to-many
-     */
-    private boolean isUnidirectionalOneToMany(PersistentProperty property) {
-        return ((property instanceof 
org.grails.datastore.mapping.model.types.OneToMany) && 
!((Association)property).isBidirectional());
-    }
-
     /**
      * Binds the primary key value column
      *
@@ -3008,7 +2993,7 @@ public class GrailsDomainBinder implements 
MetadataContributor {
     }
 
     private boolean supportsJoinColumnMapping(PersistentProperty grailsProp) {
-        return grailsProp instanceof ManyToMany || 
isUnidirectionalOneToMany(grailsProp) || grailsProp instanceof Basic;
+        return grailsProp instanceof ManyToMany || 
ofNullable(grailsProp).map(PersistentProperty::isUnidirectionalOneToMany).orElse(false)
 || grailsProp instanceof Basic;
     }
 
     private String getDefaultColumnName(PersistentProperty property, String 
sessionFactoryBeanName) {
diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/PropertyConfig.groovy
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/PropertyConfig.groovy
index a0cb72291e..6dff9f2233 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/PropertyConfig.groovy
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/PropertyConfig.groovy
@@ -198,6 +198,10 @@ class PropertyConfig extends Property {
      */
     JoinTable joinTable = new JoinTable()
 
+    ColumnConfig getJoinTableColumnConfig() {
+        return this.joinTable?.column
+    }
+
     /**
      * The join table configuration
      */
diff --git 
a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/TypeNameProviderSpec.groovy
 
b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/TypeNameProviderSpec.groovy
index ad8bde5622..abc38cd1a7 100644
--- 
a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/TypeNameProviderSpec.groovy
+++ 
b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/TypeNameProviderSpec.groovy
@@ -4,6 +4,7 @@ import grails.gorm.specs.HibernateGormDatastoreSpec
 import grails.persistence.Entity
 import org.grails.orm.hibernate.cfg.HibernatePersistentEntity
 import org.grails.orm.hibernate.cfg.Mapping
+import org.grails.orm.hibernate.cfg.PropertyConfig
 import org.hibernate.type.descriptor.java.BasicJavaType
 import org.hibernate.type.descriptor.jdbc.JdbcType
 import org.hibernate.usertype.BaseUserTypeSupport
@@ -22,7 +23,7 @@ class TypeNameProviderSpec extends HibernateGormDatastoreSpec 
{
         def property = persistentEntity.getPersistentProperties()[0]
         Mapping mapping = new Mapping()
         mapping.setUserTypes(["foo.Bar": persistentEntity.getJavaClass()])
-        def config = grailsDomainBinder.getPropertyConfig(property)
+        PropertyConfig config = new 
PersistentPropertyToPropertyConfig().apply(property)
         def name = new TypeNameProvider().getTypeName(property,config , 
mapping)
 
         then:
@@ -39,7 +40,7 @@ class TypeNameProviderSpec extends HibernateGormDatastoreSpec 
{
         def persistentEntity = createPersistentEntity(grailsDomainBinder, 
simpleName, fieldProperties, mappingProperties)
         def property = persistentEntity.getPersistentProperties()[0]
         Mapping mapping = new Mapping()
-        def config = grailsDomainBinder.getPropertyConfig(property)
+        PropertyConfig config = new 
PersistentPropertyToPropertyConfig().apply(property)
         def name = new TypeNameProvider().getTypeName(property,config , 
mapping)
 
         then:
@@ -55,7 +56,7 @@ class TypeNameProviderSpec extends HibernateGormDatastoreSpec 
{
         def property = persistentEntity.getPersistentProperties()[0]
         Mapping mapping = new Mapping()
         mapping.setUserTypes([(Salary): SalaryType])
-        def config = grailsDomainBinder.getPropertyConfig(property)
+        PropertyConfig config = new 
PersistentPropertyToPropertyConfig().apply(property)
         def name = new TypeNameProvider().getTypeName(property,config , 
mapping)
 
         then:
diff --git 
a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/model/PersistentProperty.java
 
b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/model/PersistentProperty.java
index ec37169dac..2ebc8fe747 100644
--- 
a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/model/PersistentProperty.java
+++ 
b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/model/PersistentProperty.java
@@ -20,6 +20,8 @@
 package org.grails.datastore.mapping.model;
 
 import org.grails.datastore.mapping.config.Property;
+import org.grails.datastore.mapping.model.types.Association;
+import org.grails.datastore.mapping.model.types.OneToMany;
 import org.grails.datastore.mapping.reflect.EntityReflector;
 
 import java.util.Optional;
@@ -90,4 +92,8 @@ public interface PersistentProperty<T extends Property> {
      * @return The writer for this property
      */
     EntityReflector.PropertyWriter getWriter();
+
+    default boolean isUnidirectionalOneToMany() {
+        return ((this instanceof OneToMany) && 
!((Association)this).isBidirectional());
+    }
 }

Reply via email to