jamesfredley commented on code in PR #16532:
URL: https://github.com/apache/grails-core/pull/16532#discussion_r4210586173
##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java:
##########
@@ -212,6 +217,34 @@ private void bindCollectionTable(HibernateToManyProperty
property, Collection co
collection.setCollectionTable(
mappings.addTable(schemaName, catalogName, tableName, null,
false, metadataBuildingContext, false));
collection.setInverse(property.isBidirectional() &&
!property.isOwningSide());
+ if (collection.isInverse()) {
+ warnIfManyToManyHasNoOwningSide(property);
+ }
+ }
+
+ /**
+ * 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.
+ */
+ private void warnIfManyToManyHasNoOwningSide(HibernateToManyProperty
property) {
+ if (!(property instanceof HibernateManyToManyProperty manyToMany) ||
+ !manyToMany.isBidirectional() || manyToMany.isCircular()) {
+ return;
+ }
+ Association<?> otherSide = manyToMany.getInverseSide();
+ if (otherSide == null || otherSide.isOwningSide()) {
+ return;
+ }
+ if
(manyToMany.getOwner().getName().compareTo(otherSide.getOwner().getName()) > 0)
{
+ return;
+ }
+ LOG.warn("Neither side of the many-to-many between [{}.{}] and [{}.{}]
declares belongsTo, so the relationship " +
Review Comment:
For a Map-valued many-to-many this warning runs from `bindCollectionTable`
while `isOwningSide()` is still false. `MapSecondPassBinder` then calls
`map.setInverse(false)`, so that side writes the join table anyway. The text
says the relationship is not stored, which is not true for a map.
Skip map-valued sides, or emit the warning only after the second pass has
set the final inverse flags. Please add a regression for a bidirectional Map
many-to-many with no `belongsTo`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]