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/03/03 23:27:20 UTC
(grails-core) 13/24: refactor CollectionSecondPassBinder
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit c8a9dfc9e536167a8bc86843e6c3e27d8dad4abd
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 09:27:04 2026 -0600
refactor CollectionSecondPassBinder
---
.../secondpass/CollectionSecondPassBinder.java | 14 +++-----------
.../CollectionSecondPassBinderSpec.groovy | 22 ++++++++++++++++++----
2 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinder.java
index 27077eb16f..9a34c98add 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinder.java
@@ -219,20 +219,12 @@ public class CollectionSecondPassBinder {
collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
}
- private PersistentClass resolveAssociatedClass(
+ private @Nonnull PersistentClass resolveAssociatedClass(
HibernateToManyProperty property, Map<?, ?> persistentClasses) {
return Optional.ofNullable(property.getHibernateAssociatedEntity())
.map(
- referenced -> {
- PersistentClass associatedClass =
- (PersistentClass) persistentClasses.get(referenced.getName());
- if (associatedClass == null) {
- throw new MappingException(
- "Association references unmapped class: " + referenced.getName());
- }
- return associatedClass;
- })
- .orElse(null);
+ referenced -> (PersistentClass) persistentClasses.get(referenced.getName()))
+ .orElseThrow(() -> new MappingException("Association [" + property.getName() + "] has no associated class"));
}
private void bindOrderBy(
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinderSpec.groovy
index d4501b62a3..9a4ee671f9 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionSecondPassBinderSpec.groovy
@@ -109,16 +109,30 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
collection.getOrderBy() == null
}
- def "test resolveAssociatedClass throws MappingException when class is unmapped"() {
+ def "resolveAssociatedClass throws MappingException when property has no associated entity"() {
+ given:
+ def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
+ def spiedProperty = Spy(property)
+ spiedProperty.getHibernateAssociatedEntity() >> null
+
+ when:
+ binder.resolveAssociatedClass(spiedProperty, [:])
+
+ then:
+ def ex = thrown(org.hibernate.MappingException)
+ ex.message.contains("items")
+ }
+
+ def "resolveAssociatedClass throws MappingException when associated class is not in persistentClasses"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
- def persistentClasses = [:] // Empty map, so CSPBAssociatedItem will be missing
when:
- binder.resolveAssociatedClass(property, persistentClasses)
+ binder.resolveAssociatedClass(property, [:])
then:
- thrown(org.hibernate.MappingException)
+ def ex = thrown(org.hibernate.MappingException)
+ ex.message.contains("items")
}
def "test resolveAssociatedClass returns associatedClass"() {