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/04/06 19:16:56 UTC

(grails-core) 06/12: H7: rewrite ListSecondPassBinder using fluent and modular method structure

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

borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit a8d5db2ed3317b0f9451da4593f4317bd2271411
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sat Apr 4 23:03:47 2026 -0500

    H7: rewrite ListSecondPassBinder using fluent and modular method structure
---
 .../secondpass/ListSecondPassBinder.java           | 156 +++++++++++++--------
 1 file changed, 94 insertions(+), 62 deletions(-)

diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
index 8ec6a0c493..d4c04e11e0 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
@@ -46,11 +46,14 @@ import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBind
 @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
 public class ListSecondPassBinder {
 
+    private static final String DEFAULT_INDEX_TYPE = "integer";
+
     private final MetadataBuildingContext metadataBuildingContext;
     private final CollectionSecondPassBinder collectionSecondPassBinder;
     private final PersistentEntityNamingStrategy namingStrategy;
     private final SimpleValueColumnBinder simpleValueColumnBinder;
     private final InFlightMetadataCollector mappings;
+    private final BackticksRemover backticksRemover = new BackticksRemover();
 
     public ListSecondPassBinder(
             MetadataBuildingContext metadataBuildingContext,
@@ -66,79 +69,108 @@ public class ListSecondPassBinder {
     }
 
     public void bindListSecondPass(@Nonnull HibernateToManyProperty property) {
-
         List list = (List) property.getCollection();
+
         collectionSecondPassBinder.bindCollectionSecondPass(property);
-        String columnName = property.getIndexColumnName(namingStrategy);
-        final boolean isManyToMany = property instanceof HibernateManyToManyProperty;
 
-        if (isManyToMany && !property.isOwningSide()) {
+        validateOwningSide(property);
+
+        bindIndexColumn(property, list);
+
+        list.setBaseIndex(0);
+        list.setInverse(false);
+
+        Value element = list.getElement();
+        element.createForeignKey();
+
+        bindBackReferences(property, list);
+    }
+
+    private void validateOwningSide(HibernateToManyProperty property) {
+        if (property instanceof HibernateManyToManyProperty && !property.isOwningSide()) {
             throw new MappingException("Invalid association [" + property +
                     "]. List collection types only supported on the owning side of a many-to-many relationship.");
         }
+    }
 
+    private void bindIndexColumn(HibernateToManyProperty property, List list) {
         Table collectionTable = list.getCollectionTable();
-        String type = property.getIndexColumnType("integer");
-        BasicValue iv = simpleValueColumnBinder.bindSimpleValue(
+        String columnName = property.getIndexColumnName(namingStrategy);
+        String type = property.getIndexColumnType(DEFAULT_INDEX_TYPE);
+
+        BasicValue indexValue = simpleValueColumnBinder.bindSimpleValue(
                 metadataBuildingContext, collectionTable, type, columnName, true);
-        list.setIndex(iv);
-        list.setBaseIndex(0);
-        list.setInverse(false);
+        list.setIndex(indexValue);
+    }
 
-        Value v = list.getElement();
-        v.createForeignKey();
-
-        if (property.isBidirectional()) {
-
-            HibernateAssociation inverseSide = property.getHibernateInverseSide();
-            String entityName = inverseSide.getHibernateOwner().getName();
-
-            PersistentClass referenced = mappings.getEntityBinding(entityName);
-
-            if (referenced != null) {
-                boolean compositeIdProperty = property.getHibernateInverseSide().isCompositeIdProperty();
-                if (!compositeIdProperty) {
-                    Backref prop = new Backref();
-                    final PersistentEntity owner = property.getOwner();
-                    prop.setEntityName(owner.getName());
-                    String s2 = property.getName();
-                    prop.setName(UNDERSCORE +
-                            new BackticksRemover().apply(owner.getJavaClass().getSimpleName()) +
-                            UNDERSCORE +
-                            new BackticksRemover().apply(s2) +
-                            "Backref");
-                    prop.setSelectable(false);
-                    prop.setUpdatable(false);
-                    if (isManyToMany) {
-                        prop.setInsertable(false);
-                    }
-                    prop.setCollectionRole(list.getRole());
-                    prop.setValue(list.getKey());
-
-                    DependantValue value = (DependantValue) prop.getValue();
-                    if (!property.isCircular()) {
-                        value.setNullable(false);
-                    }
-                    value.setUpdateable(true);
-                    prop.setOptional(false);
-
-                    referenced.addProperty(prop);
-                }
-
-                if ((!list.getKey().isNullable() && !list.isInverse()) || compositeIdProperty) {
-                    IndexBackref ib = new IndexBackref();
-                    ib.setName(UNDERSCORE + property.getName() + "IndexBackref");
-                    ib.setUpdatable(false);
-                    ib.setSelectable(false);
-                    if (isManyToMany) {
-                        ib.setInsertable(false);
-                    }
-                    ib.setCollectionRole(list.getRole());
-                    ib.setEntityName(list.getOwner().getEntityName());
-                    ib.setValue(list.getIndex());
-                    referenced.addProperty(ib);
-                }
+    private void bindBackReferences(HibernateToManyProperty property, List list) {
+        if (!property.isBidirectional()) {
+            return;
+        }
+
+        HibernateAssociation inverseSide = property.getHibernateInverseSide();
+        String entityName = inverseSide.getHibernateOwner().getName();
+        PersistentClass referenced = mappings.getEntityBinding(entityName);
+
+        if (referenced != null) {
+            boolean isManyToMany = property instanceof HibernateManyToManyProperty;
+            boolean compositeIdProperty = inverseSide.isCompositeIdProperty();
+
+            if (!compositeIdProperty) {
+                addBackref(property, list, referenced, isManyToMany);
             }
+
+            if (shouldAddIndexBackref(list, compositeIdProperty)) {
+                addIndexBackref(property, list, referenced, isManyToMany);
+            }
+        }
+    }
+
+    private void addBackref(HibernateToManyProperty property, List list, PersistentClass referenced, boolean isManyToMany) {
+        Backref prop = new Backref();
+        final PersistentEntity owner = property.getOwner();
+        prop.setEntityName(owner.getName());
+
+        String name = UNDERSCORE +
+                backticksRemover.apply(owner.getJavaClass().getSimpleName()) +
+                UNDERSCORE +
+                backticksRemover.apply(property.getName()) +
+                "Backref";
+
+        prop.setName(name);
+        prop.setSelectable(false);
+        prop.setUpdatable(false);
+        if (isManyToMany) {
+            prop.setInsertable(false);
+        }
+        prop.setCollectionRole(list.getRole());
+        prop.setValue(list.getKey());
+
+        DependantValue value = (DependantValue) prop.getValue();
+        if (!property.isCircular()) {
+            value.setNullable(false);
+        }
+        value.setUpdateable(true);
+        prop.setOptional(false);
+
+        referenced.addProperty(prop);
+    }
+
+    private boolean shouldAddIndexBackref(List list, boolean compositeIdProperty) {
+        return (!list.getKey().isNullable() && !list.isInverse()) || compositeIdProperty;
+    }
+
+    private void addIndexBackref(HibernateToManyProperty property, List list, PersistentClass referenced, boolean isManyToMany) {
+        IndexBackref ib = new IndexBackref();
+        ib.setName(UNDERSCORE + property.getName() + "IndexBackref");
+        ib.setUpdatable(false);
+        ib.setSelectable(false);
+        if (isManyToMany) {
+            ib.setInsertable(false);
         }
+        ib.setCollectionRole(list.getRole());
+        ib.setEntityName(list.getOwner().getEntityName());
+        ib.setValue(list.getIndex());
+        referenced.addProperty(ib);
     }
 }