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
The following commit(s) were added to
refs/heads/fix/hibernate5-many-to-many-warning-circular-map-7.0.x by this push:
new 83089c549b fix(hibernate5): skip Map-valued many-to-manys in the
no-owner warning, keep skipping circular ones
83089c549b is described below
commit 83089c549bd23786a9a6dc3119e19f48be55b445
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Sun Oct 18 20:27:43 2026 -0500
fix(hibernate5): skip Map-valued many-to-manys in the no-owner warning,
keep skipping circular ones
The no-owner many-to-many warning did not exclude Map-valued sides, whose
second pass makes
them write the join table, so it claimed a stored relationship was not
stored. It now skips a
relationship with a Map on either side, matching the Hibernate 7 module.
A circular many-to-many stays excluded: bindCollectionSecondPass makes
every circular collection
non-inverse, so both properties write their own join table and the
relationship is stored with or
without belongsTo. Declaring belongsTo there swaps the two join table names
and breaks an existing
schema, so the warning would have led to a failure. The guide says so, and
the circular test now
asserts that nothing is warned and that both sides read back.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
.../orm/hibernate/cfg/GrailsDomainBinder.java | 12 +++----
.../tests/ManyToManyWithoutOwnerWarningSpec.groovy | 42 +++++-----------------
.../domainClasses/gormAssociation/manyToMany.adoc | 20 +++--------
3 files changed, 20 insertions(+), 54 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 856e2be005..76c59707a9 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
@@ -1133,12 +1133,13 @@ public class GrailsDomainBinder implements
MetadataContributor {
* side declares {@code belongsTo}, both sides are inverse and the
relationship is never stored, so warn about it
* 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.
+ * and gets no warning. A circular many-to-many, whose two properties are
on the same class, gets none either:
+ * {@code bindCollectionSecondPass} makes every circular collection
non-inverse, so both properties write their own
+ * join table and the relationship is stored with or without {@code
belongsTo}. Declaring {@code belongsTo} there
+ * would swap the names of the two join tables and break an existing
schema.
*/
protected void warnIfManyToManyHasNoOwningSide(ToMany property) {
- if (!(property instanceof ManyToMany) || !property.isBidirectional()) {
+ if (!(property instanceof ManyToMany) || !property.isBidirectional()
|| property.isCircular()) {
return;
}
Association otherSide = property.getInverseSide();
@@ -1146,8 +1147,7 @@ public class GrailsDomainBinder implements
MetadataContributor {
Map.class.isAssignableFrom(property.getType()) ||
Map.class.isAssignableFrom(otherSide.getType())) {
return;
}
- int byOwner =
property.getOwner().getName().compareTo(otherSide.getOwner().getName());
- if (byOwner > 0 || byOwner == 0 &&
property.getName().compareTo(otherSide.getName()) > 0) {
+ if
(property.getOwner().getName().compareTo(otherSide.getOwner().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 4b0b52b0d4..ca209ff4a2 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,7 +53,7 @@ class ManyToManyWithoutOwnerWarningSpec extends Specification
{
!output.contains(WARNING)
}
- void 'a self-referencing many-to-many logs no warning'() {
+ void 'a unidirectional self-referencing one-to-many logs no warning'() {
when:
var output = stderrWhile {
new HibernateDatastore(WarnSelfReferencing).close()
@@ -63,39 +63,27 @@ class ManyToManyWithoutOwnerWarningSpec extends
Specification {
!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'() {
+ void 'a circular many-to-many with no belongsTo logs no warning, because
both properties write their own join table'() {
given:
var output = null
HibernateDatastore datastore = null
when:
output = stderrWhile {
- datastore = new HibernateDatastore(WarnCircularOwned)
+ datastore = new HibernateDatastore(WarnCircular)
}
- WarnCircularOwned.withNewTransaction {
- WarnCircularOwned followed = new WarnCircularOwned(name:
'followed').save(failOnError: true)
- WarnCircularOwned follower = new WarnCircularOwned(name:
'follower').save(failOnError: true)
+ WarnCircular.withNewTransaction {
+ WarnCircular followed = new WarnCircular(name:
'followed').save(failOnError: true)
+ WarnCircular follower = new WarnCircular(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']
+ WarnCircular.withNewSession {
+ WarnCircular.findByName('follower').following*.name ==
['followed'] &&
+ WarnCircular.findByName('followed').followers*.name ==
['follower']
}
cleanup:
@@ -225,18 +213,6 @@ class 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 {
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 e935064c28..3bcde9da39 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,21 +40,6 @@ 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]
@@ -77,3 +62,8 @@ new Book(name:"Groovy in Action")
This is the expected behaviour as, just like Hibernate, only one side of a
many-to-many can take responsibility for managing the relationship.
+A many-to-many with a `Map` on either side needs no `belongsTo` to be stored,
because each `Map` side writes its own join table, so GORM logs no warning for
it.
+
+A many-to-many between a class and itself, where both properties are on the
same class, also logs no warning. Each of its two properties writes its own
join table, with or without `belongsTo`, so use `addToFollowing` and
`removeFromFollowing`, which update both sides. Declaring `belongsTo` on such a
class changes which table each property uses, so do not add it to an existing
mapping that already works.
+
+