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/10/16 02:08:52 UTC

(grails-core) branch fix/hibernate5-many-to-many-warning-circular-map-7.0.x created (now ab54bd76b1)

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

borinquenkid pushed a change to branch fix/hibernate5-many-to-many-warning-circular-map-7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git


      at ab54bd76b1 fix(hibernate5): warn about a circular many-to-many and skip Map-valued ones

This branch includes the following new commits:

     new ab54bd76b1 fix(hibernate5): warn about a circular many-to-many and skip Map-valued ones

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(grails-core) 01/01: fix(hibernate5): warn about a circular many-to-many and skip Map-valued ones

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch fix/hibernate5-many-to-many-warning-circular-map-7.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit ab54bd76b16d9dc8166327252ad0fae48a8c02d2
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Thu Oct 15 20:55:48 2026 -0500

    fix(hibernate5): warn about a circular many-to-many and skip Map-valued ones
    
    The no-owner many-to-many warning excluded circular mappings, so following/followers on one class
    lost data silently, and it did not exclude Map-valued sides, whose second pass makes them write the
    join table, so it claimed a stored relationship was not stored. Warn for the circular shape (the
    property names break the owner-name tie, keeping it once per relationship) and skip a relationship
    with a Map on either side, matching the Hibernate 7 module.
    
    Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
---
 .../orm/hibernate/cfg/GrailsDomainBinder.java      |  14 +-
 .../tests/ManyToManyWithoutOwnerWarningSpec.groovy | 185 +++++++++++++++++++++
 .../domainClasses/gormAssociation/manyToMany.adoc  |  15 ++
 3 files changed, 210 insertions(+), 4 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 33f2c5060e..856e2be005 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
@@ -1131,17 +1131,23 @@ public class GrailsDomainBinder implements MetadataContributor {
     /**
      * A bidirectional many-to-many is written by its owning side, which {@code belongsTo} designates. When neither
      * side declares {@code belongsTo}, both sides are inverse and the relationship is never stored, so warn about it
-     * once per relationship.
+     * once per relationship. A Map-valued side is never inverse once its second pass has run, because
+     * {@code bindMapSecondPass} makes it write the join table, so a relationship with a Map on either side is stored
+     * and gets no warning. A circular many-to-many, whose two properties are on the same class, is warned about as
+     * well, because it loses data the same way; its two sides tie on the owner name, so the property names decide
+     * which side warns.
      */
     protected void warnIfManyToManyHasNoOwningSide(ToMany property) {
-        if (!(property instanceof ManyToMany) || !property.isBidirectional() || property.isCircular()) {
+        if (!(property instanceof ManyToMany) || !property.isBidirectional()) {
             return;
         }
         Association otherSide = property.getInverseSide();
-        if (otherSide == null || otherSide.isOwningSide()) {
+        if (otherSide == null || otherSide.isOwningSide() ||
+                Map.class.isAssignableFrom(property.getType()) || Map.class.isAssignableFrom(otherSide.getType())) {
             return;
         }
-        if (property.getOwner().getName().compareTo(otherSide.getOwner().getName()) > 0) {
+        int byOwner = property.getOwner().getName().compareTo(otherSide.getOwner().getName());
+        if (byOwner > 0 || byOwner == 0 && property.getName().compareTo(otherSide.getName()) > 0) {
             return;
         }
         LOG.warn("Neither side of the many-to-many between [{}.{}] and [{}.{}] declares belongsTo, so the relationship " +
diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
index aa0ca0e748..4b0b52b0d4 100644
--- a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
+++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
@@ -53,6 +53,107 @@ class ManyToManyWithoutOwnerWarningSpec extends Specification {
         !output.contains(WARNING)
     }
 
+    void 'a self-referencing many-to-many logs no warning'() {
+        when:
+        var output = stderrWhile {
+            new HibernateDatastore(WarnSelfReferencing).close()
+        }
+
+        then:
+        !output.contains(WARNING)
+    }
+
+    void 'a circular many-to-many with no belongsTo logs one warning, because nothing is stored'() {
+        when:
+        var output = stderrWhile {
+            new HibernateDatastore(WarnCircular).close()
+        }
+
+        then:
+        output.readLines().count { it.contains(WARNING) } == 1
+        output.contains('[grails.gorm.tests.WarnCircular.followers] and [grails.gorm.tests.WarnCircular.following]')
+        output.contains('for example in WarnCircular: static belongsTo = WarnCircular')
+    }
+
+    void 'a circular many-to-many with belongsTo logs no warning and stores the relationship'() {
+        given:
+        var output = null
+        HibernateDatastore datastore = null
+
+        when:
+        output = stderrWhile {
+            datastore = new HibernateDatastore(WarnCircularOwned)
+        }
+        WarnCircularOwned.withNewTransaction {
+            WarnCircularOwned followed = new WarnCircularOwned(name: 'followed').save(failOnError: true)
+            WarnCircularOwned follower = new WarnCircularOwned(name: 'follower').save(failOnError: true)
+            follower.addToFollowing(followed)
+            follower.save(failOnError: true)
+        }
+
+        then:
+        !output.contains(WARNING)
+        WarnCircularOwned.withNewSession {
+            WarnCircularOwned.findByName('follower').following*.name == ['followed'] &&
+                    WarnCircularOwned.findByName('followed').followers*.name == ['follower']
+        }
+
+        cleanup:
+        datastore?.close()
+    }
+
+    void 'a unidirectional one-to-many and a basic collection log no warning'() {
+        when:
+        var output = stderrWhile {
+            new HibernateDatastore(WarnParent, WarnChild).close()
+        }
+
+        then:
+        !output.contains(WARNING)
+    }
+
+    void 'a many-to-many with a Map on one side logs no warning, because the Map side writes the join table'() {
+        when:
+        var output = stderrWhile {
+            new HibernateDatastore(WarnMapLeft, WarnMapRight).close()
+        }
+
+        then:
+        !output.contains(WARNING)
+    }
+
+    void 'a many-to-many with a Map on both sides logs no warning'() {
+        when:
+        var output = stderrWhile {
+            new HibernateDatastore(WarnMapBothLeft, WarnMapBothRight).close()
+        }
+
+        then:
+        !output.contains(WARNING)
+    }
+
+    void 'the relationship is stored when it is written through the Map side'() {
+        given:
+        HibernateDatastore datastore = new HibernateDatastore(WarnMapLeft, WarnMapRight)
+
+        when:
+        WarnMapLeft.withNewTransaction {
+            WarnMapRight right = new WarnMapRight(name: 'right').save(failOnError: true)
+            new WarnMapLeft(name: 'left', rights: [first: right]).save(failOnError: true)
+        }
+
+        then:
+        WarnMapLeft.withNewSession {
+            WarnMapLeft.findByName('left').rights.keySet() == ['first'] as Set
+        }
+        WarnMapRight.withNewSession {
+            WarnMapRight.findByName('right').lefts*.name == ['left']
+        }
+
+        cleanup:
+        datastore?.close()
+    }
+
     private static String stderrWhile(Closure<?> work) {
         var original = System.err
         var buffer = new ByteArrayOutputStream()
@@ -103,3 +204,87 @@ class WarnOwnerRight {
 
     static hasMany = [lefts: WarnOwnedLeft]
 }
+
+@Entity
+class WarnSelfReferencing {
+
+    String name
+    Set<WarnSelfReferencing> friends
+
+    static hasMany = [friends: WarnSelfReferencing]
+}
+
+@Entity
+class WarnCircular {
+
+    String name
+    Set<WarnCircular> following
+    Set<WarnCircular> followers
+
+    static hasMany = [following: WarnCircular, followers: WarnCircular]
+    static mappedBy = [following: 'followers', followers: 'following']
+}
+
+@Entity
+class WarnCircularOwned {
+
+    String name
+    Set<WarnCircularOwned> following
+    Set<WarnCircularOwned> followers
+
+    static hasMany = [following: WarnCircularOwned, followers: WarnCircularOwned]
+    static mappedBy = [following: 'followers', followers: 'following']
+    static belongsTo = WarnCircularOwned
+}
+
+@Entity
+class WarnParent {
+
+    String name
+    Set<WarnChild> children
+    Set<String> labels
+
+    static hasMany = [children: WarnChild, labels: String]
+}
+
+@Entity
+class WarnChild {
+
+    String name
+}
+
+@Entity
+class WarnMapLeft {
+
+    String name
+    Map<String, WarnMapRight> rights
+
+    static hasMany = [rights: WarnMapRight]
+}
+
+@Entity
+class WarnMapRight {
+
+    String name
+    Set<WarnMapLeft> lefts
+
+    static hasMany = [lefts: WarnMapLeft]
+}
+
+@Entity
+class WarnMapBothLeft {
+
+    String name
+    Map<String, WarnMapBothRight> rights
+
+    static hasMany = [rights: WarnMapBothRight]
+}
+
+@Entity
+class WarnMapBothRight {
+
+    String name
+    Map<String, WarnMapBothLeft> lefts
+
+    static hasMany = [lefts: WarnMapBothLeft]
+}
diff --git a/grails-data-hibernate5/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc b/grails-data-hibernate5/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
index 1a0dc01224..e935064c28 100644
--- a/grails-data-hibernate5/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
+++ b/grails-data-hibernate5/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
@@ -40,6 +40,21 @@ GORM maps a many-to-many using a join table at the database level. The owning si
 
 WARNING: One side must declare `belongsTo`. If neither side does, neither side writes the join table, so the relationship is not stored. GORM logs a warning at startup for such a mapping.
 
+This also applies to a many-to-many between a class and itself, where both properties are on the same class. Without `belongsTo`, the relationship is not stored and GORM logs the same warning at startup:
+
+[source,groovy]
+----
+class User {
+    String name
+    static hasMany   = [following: User, followers: User]
+    static mappedBy  = [following: 'followers', followers: 'following']
+    static belongsTo = User             // <1>
+}
+----
+<1> Without this line, `user.addToFollowing(other); user.save()` writes nothing, and both `following` and `followers` read back empty.
+
+A many-to-many with a `Map` on either side needs no `belongsTo` to be stored, because the `Map` side writes the join table, so it logs no warning.
+
 For example this will work and cascade saves:
 
 [source,groovy]