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/08/30 22:31:55 UTC
(grails-core) 05/07: Cover composite-identity join-key derivation in ManyToOneBinder
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch test/grails-data-hibernate7-coverage
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 8bf603dc0a030a67c139256b3d4d7277984e491d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Aug 30 16:53:37 2026 -0500
Cover composite-identity join-key derivation in ManyToOneBinder
prepareCircularManyToMany's composite-identity branch (building join
table keys for a self-referential many-to-many owned by an entity with
a composite identity) was untested: neither the "reuse existing
matching join keys" path nor the "derive keys from property names via
the naming strategy" path was exercised.
ManyToOneBinder: 78.2% -> 100%.
Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAjfckcY4EJsxranv1RyGV
---
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 100 +++++++++++++++++++++
1 file changed, 100 insertions(+)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ManyToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ManyToOneBinderSpec.groovy
index 78416ba1cc..a0007e555c 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ManyToOneBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ManyToOneBinderSpec.groovy
@@ -19,8 +19,10 @@
package org.grails.orm.hibernate.cfg.domainbinding
import grails.gorm.tests.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.ColumnConfig
import org.grails.orm.hibernate.cfg.HibernateCompositeIdentity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.*
+import org.grails.orm.hibernate.cfg.JoinTable
import org.grails.orm.hibernate.cfg.Mapping
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.grails.orm.hibernate.cfg.PropertyConfig
@@ -200,6 +202,104 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
1 * propertyConfig.setJoinTable({ it.keys && it.keys[0].name == "new_prop_id" })
}
+ def "prepareCircularManyToMany reuses existing join-table keys when their count matches the composite identity"() {
+ given: "a circular many-to-many owned by an entity with a composite identity and pre-configured join keys"
+ def property = Mock(HibernateManyToManyProperty)
+ def otherSide = Mock(HibernateManyToManyProperty)
+ def collectionTable = new Table("coll_table")
+
+ PersistentClass ownerClass = new RootClass(metadataBuildingContext)
+ def realCollection = new HibernateMap(metadataBuildingContext, ownerClass)
+ realCollection.setCollectionTable(collectionTable)
+
+ property.getCollection() >> realCollection
+ property.getHibernateInverseSide() >> otherSide
+
+ def compositeId = new HibernateCompositeIdentity()
+ compositeId.setPropertyNames(["partA", "partB"] as String[])
+ def mapping = new Mapping()
+ mapping.setIdentity(compositeId)
+ mapping.setColumns([:])
+
+ def ownerEntity = Mock(GrailsHibernatePersistentEntity) {
+ getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.of(compositeId)
+ getHibernateMappedForm() >> mapping
+ }
+
+ def existingJoinTable = new JoinTable()
+ existingJoinTable.setKeys([new ColumnConfig(name: "custom_part_a"), new ColumnConfig(name: "custom_part_b")])
+
+ def propertyConfig = Mock(PropertyConfig)
+ propertyConfig.hasJoinKeyMapping() >> false
+ propertyConfig.getJoinTable() >> existingJoinTable
+
+ otherSide.getHibernateOwner() >> ownerEntity
+ otherSide.getOwner() >> ownerEntity
+ ownerEntity.getName() >> "OwnerEntity"
+
+ otherSide.isCircular() >> true
+ otherSide.getName() >> "compositeProp"
+ otherSide.getMappedForm() >> propertyConfig
+ otherSide.getHibernateMappedForm() >> propertyConfig
+
+ when:
+ def result = binder.bindManyToOne(property, "/test")
+
+ then:
+ result instanceof ManyToOne
+ 1 * propertyConfig.setJoinTable({ it.keys*.name == ["custom_part_a", "custom_part_b"] })
+ }
+
+ def "prepareCircularManyToMany derives join keys from property names when existing keys are absent"() {
+ given: "a circular many-to-many owned by an entity with a composite identity and no pre-configured join keys"
+ def property = Mock(HibernateManyToManyProperty)
+ def otherSide = Mock(HibernateManyToManyProperty)
+ def collectionTable = new Table("coll_table")
+
+ PersistentClass ownerClass = new RootClass(metadataBuildingContext)
+ def realCollection = new HibernateMap(metadataBuildingContext, ownerClass)
+ realCollection.setCollectionTable(collectionTable)
+
+ property.getCollection() >> realCollection
+ property.getHibernateInverseSide() >> otherSide
+
+ def compositeId = new HibernateCompositeIdentity()
+ compositeId.setPropertyNames(["partA", "partB"] as String[])
+ def mapping = new Mapping()
+ mapping.setIdentity(compositeId)
+ mapping.setColumns([:])
+
+ def ownerEntity = Mock(GrailsHibernatePersistentEntity) {
+ getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.of(compositeId)
+ getHibernateMappedForm() >> mapping
+ }
+
+ def propertyConfig = Mock(PropertyConfig)
+ propertyConfig.hasJoinKeyMapping() >> false
+ propertyConfig.getJoinTable() >> null
+
+ otherSide.getHibernateOwner() >> ownerEntity
+ otherSide.getOwner() >> ownerEntity
+ ownerEntity.getName() >> "OwnerEntity"
+
+ otherSide.isCircular() >> true
+ otherSide.getName() >> "compositeProp"
+ otherSide.getMappedForm() >> propertyConfig
+ otherSide.getHibernateMappedForm() >> propertyConfig
+
+ namingStrategy.resolveColumnName("partA") >> "part_a"
+ namingStrategy.resolveColumnName("partB") >> "part_b"
+
+ when:
+ def result = binder.bindManyToOne(property, "/test")
+
+ then:
+ result instanceof ManyToOne
+ 1 * propertyConfig.setJoinTable({ it.keys*.name == ["part_a_id", "part_b_id"] })
+ }
+
private List mockEntity(boolean composite) {
def mapping = new Mapping()
def compositeId = composite ? new HibernateCompositeIdentity() : null