borinquenkid commented on code in PR #16686:
URL: https://github.com/apache/grails-core/pull/16686#discussion_r4331413119
##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java:
##########
@@ -39,19 +40,23 @@ public CollectionKeyColumnUpdater(CollectionKeyBinder
collectionKeyBinder) {
public void bind(HibernateToManyProperty property) {
DependantValue key = collectionKeyBinder.bind(property);
key.getColumns().stream().filter(Objects::nonNull).forEach(column ->
column.setNullable(true));
+ Table collectionTable = property.getCollection().getCollectionTable();
long unidirectionalCount = property.getHibernateOwner()
.getPersistentPropertiesToBind()
.stream()
.filter(HibernateToManyProperty.class::isInstance)
.map(HibernateToManyProperty.class::cast)
.filter(p -> !p.isBidirectional())
+ .filter(p -> p.getCollection() != null &&
+ Objects.equals(p.getCollection().getCollectionTable(),
collectionTable))
Review Comment:
Confirmed and fixed in 7138500ed1 with your filter:
`HibernateToManyProperty::isBoundAsCollection` replaces the `isInstance` + null
check, so a serializable or user-type to-many is neither asked for its
collection nor counted as a sharer of the join table.
`CollectionKeyColumnUpdaterSpec` now pins the contract you describe: an
owner with `tags type: 'serializable'` and `labels type: <UserType>` next to an
entity `items` is bound, both column-bound properties have no Hibernate
collection and `getCollection()` throws the `MappingException` for them, and
`bind(items)` keeps the key updatable. Before the fix the fixture failed the
spec's session factory at startup, as in your probes.
`MultipleUnidirectionalHasManySpec` adds the entity-next-to-serializable shape
end to end.
The hand-ported Groovy version on #16572 got the same change in 4773d601eb,
with the same specs.
##########
grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/MultipleUnidirectionalHasManySpec.groovy:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package grails.gorm.tests
+
+import grails.gorm.annotation.Entity
+
+/**
+ * An owner with several unidirectional hasMany associations to different
classes keeps a join table for each of
+ * them, and an element added to one of them is written to its own join table.
+ */
+class MultipleUnidirectionalHasManySpec extends HibernateGormDatastoreSpec {
+
+ void setupSpec() {
+ manager.registerDomainClasses(MuhItem, MuhRole, MuhTag)
+ }
+
+ void cleanup() {
+ MuhItem.withNewTransaction {
+ MuhItem.executeUpdate('delete from MuhItem', [:])
+ MuhRole.executeUpdate('delete from MuhRole', [:])
+ MuhTag.executeUpdate('delete from MuhTag', [:])
+ }
+ }
+
+ void "an element added to one of two unidirectional hasMany associations
is written to its join table"() {
+ given: "a persisted item with two unidirectional hasMany associations
to different classes"
+ def item = new MuhItem(name: 'Example item').save(flush: true)
+
+ when: "a role is added to the managed item and the item is flushed"
+ item.addToRoles(new MuhRole(authority: 'ROLE_EXAMPLE'))
+ item.save(flush: true)
+ MuhItem.withSession { it.clear() }
+
+ then: "the role is found through the reloaded item"
+ MuhItem.get(item.id).roles*.authority == ['ROLE_EXAMPLE']
+ MuhItem.get(item.id).tags.isEmpty()
+ }
+
+ void "elements added to both unidirectional hasMany associations are
written to their own join tables"() {
+ given:
+ def item = new MuhItem(name: 'Example item').save(flush: true)
+
+ when:
+ item.addToRoles(new MuhRole(authority: 'ROLE_A'))
+ item.addToTags(new MuhTag(name: 'tag-a'))
+ item.save(flush: true)
+ MuhItem.withSession { it.clear() }
+
+ then:
+ MuhItem.get(item.id).roles*.authority == ['ROLE_A']
+ MuhItem.get(item.id).tags*.name == ['tag-a']
+ }
+}
+
+@Entity
+class MuhItem {
+ Long id
+ String name
+ static hasMany = [roles: MuhRole, tags: MuhTag]
Review Comment:
Added in 7138500ed1: `MuhAccount` with `hasMany = [roles: MuhRole,
denyRoles: MuhRole]` and `joinTable: 'muh_account_allow_roles'` /
`'muh_account_deny_roles'`; a role added to each is read back through its own
association.
--
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]