This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch
fix/hibernate7-many-to-many-without-owner-8.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git
The following commit(s) were added to
refs/heads/fix/hibernate7-many-to-many-without-owner-8.0.x by this push:
new 8ca766e5a5 test, docs(hibernate7): assert nothing is stored for a
circular many-to-many and document its side effects
8ca766e5a5 is described below
commit 8ca766e5a5d234e9ba7d568fa22d160e13d01a68
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Sun Oct 18 19:58:32 2026 -0500
test, docs(hibernate7): assert nothing is stored for a circular
many-to-many and document its side effects
The circular warning test now saves through addToFollowing and asserts that
both
sides read back empty. The guide explains that adding belongsTo changes the
columns of both
join tables, that each property then writes its own table, and that a Map
side needs no belongsTo.
The self-referencing test is renamed to say what it covers.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
.../tests/ManyToManyWithoutOwnerWarningSpec.groovy | 23 +++++++++++++++++++---
.../domainClasses/gormAssociation/manyToMany.adoc | 13 ++++++++++++
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git
a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
index 4b0b52b0d4..ad9aedcdcd 100644
---
a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/ManyToManyWithoutOwnerWarningSpec.groovy
+++
b/grails-data-hibernate7/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()
@@ -64,15 +64,32 @@ class ManyToManyWithoutOwnerWarningSpec extends
Specification {
}
void 'a circular many-to-many with no belongsTo logs one warning, because
nothing is stored'() {
+ given:
+ var output = null
+ HibernateDatastore datastore = null
+
when:
- var output = stderrWhile {
- new HibernateDatastore(WarnCircular).close()
+ output = stderrWhile {
+ datastore = new HibernateDatastore(WarnCircular)
+ }
+ 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.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')
+ WarnCircular.withNewSession {
+ WarnCircular.findByName('follower').following.isEmpty() &&
+ WarnCircular.findByName('followed').followers.isEmpty()
+ }
+
+ cleanup:
+ datastore?.close()
}
void 'a circular many-to-many with belongsTo logs no warning and stores
the relationship'() {
diff --git
a/grails-data-hibernate7/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
b/grails-data-hibernate7/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
index 3d6de8635c..563bd86986 100644
---
a/grails-data-hibernate7/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
+++
b/grails-data-hibernate7/docs/src/docs/asciidoc/domainClasses/gormAssociation/manyToMany.adoc
@@ -55,6 +55,19 @@ class User {
----
<1> Without this line, `user.addToFollowing(other); user.save()` writes
nothing, and both `following` and `followers` read back empty.
+Adding `belongsTo` to an existing mapping of this kind changes the columns of
both join tables. They hold no rows, because nothing was stored without it, so
drop them and let GORM recreate them, or add a migration. `dbCreate: 'update'`
does not remove the old columns, and the first save then fails.
+
+With `belongsTo`, each of the two properties writes its own join table, so use
`addToFollowing` and `removeFromFollowing`, which update both sides. Assigning
`user.following = [other] as Set` stores the row for `following` only. Before
deleting a `User`, remove it from both sides:
+
+[source,groovy]
+----
+user.following.toList().each { user.removeFromFollowing(it) }
+user.followers.toList().each { it.removeFromFollowing(user) }
+user.delete()
+----
+
+A many-to-many with a `Map` on either side needs no `belongsTo`, because a
`Map` side always writes its join table, so GORM logs no warning for it.
+
=== Saving a Many-to-Many
Always save from the owning side (the side that does **not** have `belongsTo`):