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:07 UTC
(grails-core) branch 8.0.x-hibernate7-dev created (now 2900b582ee)
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a change to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git
at 2900b582ee refactor: extract resolveJoinTableForeignKeyColumnName into HibernateToManyProperty
This branch includes the following new commits:
new 0ac03dca75 fix: share single InFlightMetadataCollector across binders in test specs
new d35d831b72 Refactor CollectionSecondPassBinder and ManyToOneBinder for typed dispatch
new 9441d2ed26 Introduce HibernateEnumProperty marker interface; replace isEnumType() checks with instanceof
new 1b0e61cdf7 refactor isValidHibernateOneToOne and isValidHibernateManyToOne
new 05fcd164f0 Clean up GrailsPropertyBinder/ManyToOneBinder/OneToOneBinder to use concrete types; eliminate OneToOne cast and HibernateToOneProperty overload
new 0d90e57e40 Move InFlightMetadataCollector into ComponentBinder constructor; remove it from bindProperty, bindComponent, bindClassProperties, and bindCompositeId method signatures
new 12f7fb8e2f refactor methods into HibernateOneToOneProperty
new 1b20508581 refactor: split ManyToOneBinder into ManyToOneBinder and ForeignKeyOneToOneBinder
new 9153fed8ad refactor: replace isCompositeIdentifier with Optional<CompositeIdentity> in ManyToOneBinder
new f71eac0999 refactor: CompositeIdentifierToManyToOneBinder
new 4b7a179641 test OrderByClauseBuilder
new dfc567747e refactor CollectionSecondPassBinder
new c8a9dfc9e5 refactor CollectionSecondPassBinder
new f2427ba3fa Extract bindOrderBy into CollectionOrderByBinder
new ec1ca22399 refactor: extract bindOrderBy and applyMultiTenantFilter from CollectionSecondPassBinder
new 7150425135 refactor: extract bindCollectionKey from CollectionSecondPassBinder into CollectionKeyBinder
new 380769aaf6 refactor: extract bindBidirectionalMapElement into BidirectionalMapElementBinder
new 6cf5923907 refactor: extract bindManyToManyElement into ManyToManyElementBinder
new 1b0d7a5b38 refactor: move DependantValue creation into CollectionKeyBinder
new b893b4348a refactor: HibernateToManyProperty.getCacheUSage
new 66435d5a35 refactor: move CollectionKeyBinder into CollectionKeyColumnUpdater
new b965e8aefd efactor: extract BasicCollectionElementBinder from CollectionWithJoinTableBinder
new 244a595487 refactor: move ManyToOne element creation into UnidirectionalOneToManyInverseValuesBinder
new 2900b582ee refactor: extract resolveJoinTableForeignKeyColumnName into HibernateToManyProperty
The 24 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(grails-core) 06/24: Move InFlightMetadataCollector into ComponentBinder constructor; remove it from bindProperty, bindComponent, bindClassProperties, and bindCompositeId method signatures
Posted by bo...@apache.org.
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 0d90e57e40b7e3d97ae27d409e1a4e7870ce77fc
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 10:59:23 2026 -0600
Move InFlightMetadataCollector into ComponentBinder constructor; remove it from bindProperty, bindComponent, bindClassProperties, and bindCompositeId method signatures
---
.../domainbinding/binder/ClassPropertiesBinder.java | 14 ++------------
.../cfg/domainbinding/binder/ComponentBinder.java | 9 +++++----
.../cfg/domainbinding/binder/CompositeIdBinder.java | 6 ++----
.../domainbinding/binder/GrailsDomainBinder.java | 2 +-
.../domainbinding/binder/GrailsPropertyBinder.java | 6 ++----
.../cfg/domainbinding/binder/IdentityBinder.java | 2 +-
.../RootPersistentClassCommonValuesBinder.java | 2 +-
.../domainbinding/binder/SubclassMappingBinder.java | 2 +-
.../cfg/domainbinding/CollectionBinderSpec.groovy | 3 ++-
.../cfg/domainbinding/ComponentBinderSpec.groovy | 13 +++++++------
.../cfg/domainbinding/CompositeIdBinderSpec.groovy | 10 +++++-----
.../domainbinding/GrailsPropertyBinderSpec.groovy | 21 +++++++++++----------
.../cfg/domainbinding/IdentityBinderSpec.groovy | 4 ++--
.../binder/ClassPropertiesBinderSpec.groovy | 6 +++---
...RootPersistentClassCommonValuesBinderSpec.groovy | 2 +-
.../binder/SubclassMappingBinderSpec.groovy | 6 +++---
.../secondpass/ListSecondPassBinderSpec.groovy | 3 ++-
.../secondpass/MapSecondPassBinderSpec.groovy | 3 ++-
18 files changed, 53 insertions(+), 61 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinder.java
index a92b21f4da..68dc35b9f0 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinder.java
@@ -22,7 +22,6 @@ import jakarta.annotation.Nonnull;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.PropertyFromValueCreator;
-import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
@@ -57,17 +56,9 @@ public class ClassPropertiesBinder {
this(grailsPropertyBinder, propertyFromValueCreator, new NaturalIdentifierBinder());
}
- /**
- * Binds the properties of the specified Grails domain class to the Hibernate meta-model.
- *
- * @param domainClass The Grails domain class
- * @param persistentClass The Hibernate PersistentClass instance
- * @param mappings The Hibernate InFlightMetadataCollector instance
- */
public void bindClassProperties(
@Nonnull GrailsHibernatePersistentEntity domainClass,
- PersistentClass persistentClass,
- @Nonnull InFlightMetadataCollector mappings) {
+ PersistentClass persistentClass) {
persistentClass.getTable().setComment(domainClass.getMappedForm().getComment());
Table table = persistentClass.getTable();
@@ -80,8 +71,7 @@ public class ClassPropertiesBinder {
table,
GrailsDomainBinder.EMPTY_PATH,
null,
- currentGrailsProp,
- mappings);
+ currentGrailsProp);
persistentClass.addProperty(
propertyFromValueCreator.createProperty(value, currentGrailsProp));
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ComponentBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ComponentBinder.java
index 837ae0257f..8765653017 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ComponentBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ComponentBinder.java
@@ -18,7 +18,6 @@
*/
package org.grails.orm.hibernate.cfg.domainbinding.binder;
-import jakarta.annotation.Nonnull;
import org.grails.orm.hibernate.cfg.GrailsHibernateUtil;
import org.grails.orm.hibernate.cfg.MappingCacheHolder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
@@ -36,15 +35,18 @@ public class ComponentBinder {
private final MetadataBuildingContext metadataBuildingContext;
private final MappingCacheHolder mappingCacheHolder;
private final ComponentUpdater componentUpdater;
+ private final InFlightMetadataCollector metadataCollector;
private GrailsPropertyBinder grailsPropertyBinder;
public ComponentBinder(
MetadataBuildingContext metadataBuildingContext,
MappingCacheHolder mappingCacheHolder,
- ComponentUpdater componentUpdater) {
+ ComponentUpdater componentUpdater,
+ InFlightMetadataCollector metadataCollector) {
this.metadataBuildingContext = metadataBuildingContext;
this.mappingCacheHolder = mappingCacheHolder;
this.componentUpdater = componentUpdater;
+ this.metadataCollector = metadataCollector;
}
public void setGrailsPropertyBinder(GrailsPropertyBinder grailsPropertyBinder) {
@@ -54,7 +56,6 @@ public class ComponentBinder {
public Component bindComponent(
PersistentClass owner,
HibernateEmbeddedProperty embeddedProperty,
- @Nonnull InFlightMetadataCollector mappings,
String path) {
Component component = new Component(metadataBuildingContext, owner);
Class<?> type = embeddedProperty.getType();
@@ -80,7 +81,7 @@ public class ComponentBinder {
domainClass.getHibernatePersistentProperties(propertyType)) {
var value =
grailsPropertyBinder.bindProperty(
- persistentClass, table, currentPath, embeddedProperty, peerProperty, mappings);
+ persistentClass, table, currentPath, embeddedProperty, peerProperty);
componentUpdater.updateComponent(component, embeddedProperty, peerProperty, value);
}
return component;
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdBinder.java
index 46cd356b8f..e5787eb15a 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdBinder.java
@@ -23,7 +23,6 @@ import org.grails.orm.hibernate.cfg.CompositeIdentity;
import org.grails.orm.hibernate.cfg.GrailsHibernateUtil;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
-import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.RootClass;
@@ -47,8 +46,7 @@ public class CompositeIdBinder {
public void bindCompositeId(
@Nonnull GrailsHibernatePersistentEntity domainClass,
RootClass root,
- CompositeIdentity compositeIdentity,
- @Nonnull InFlightMetadataCollector mappings) {
+ CompositeIdentity compositeIdentity) {
Component id = new Component(metadataBuildingContext, root);
id.setNullValue("undefined");
root.setIdentifier(id);
@@ -71,7 +69,7 @@ public class CompositeIdBinder {
for (HibernatePersistentProperty property : composite) {
var value =
grailsPropertyBinder.bindProperty(
- root, root.getTable(), "", identifierProp, property, mappings);
+ root, root.getTable(), "", identifierProp, property);
componentUpdater.updateComponent(id, identifierProp, property, value);
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
index d411b136be..c7ae65c34d 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
@@ -158,7 +158,7 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
metadataCollector);
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator);
ComponentBinder componentBinder =
- new ComponentBinder(metadataBuildingContext, getMappingCacheHolder(), componentUpdater);
+ new ComponentBinder(metadataBuildingContext, getMappingCacheHolder(), componentUpdater, metadataCollector);
GrailsPropertyBinder grailsPropertyBinder =
new GrailsPropertyBinder(
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index e7332f0b24..c970dd5a32 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -25,7 +25,6 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOnePr
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
-import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
@@ -64,8 +63,7 @@ public class GrailsPropertyBinder {
Table table,
String path,
HibernatePersistentProperty parentProperty,
- @Nonnull HibernatePersistentProperty currentGrailsProp,
- @Nonnull InFlightMetadataCollector mappings) {
+ @Nonnull HibernatePersistentProperty currentGrailsProp) {
if (LOG.isDebugEnabled()) {
LOG.debug(
"[GrailsPropertyBinder] Binding persistent property ["
@@ -91,7 +89,7 @@ public class GrailsPropertyBinder {
// HibernateToManyProperty
value = collectionBinder.bindCollection(toMany, persistentClass, path);
} else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) {
- value = componentBinder.bindComponent(persistentClass, embedded, mappings, path);
+ value = componentBinder.bindComponent(persistentClass, embedded, path);
} else {
// HibernateSimpleProperty
value = simpleValueBinder.bindSimpleValue(currentGrailsProp, parentProperty, table, path);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/IdentityBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/IdentityBinder.java
index 46f214af41..fd06e02768 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/IdentityBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/IdentityBinder.java
@@ -46,7 +46,7 @@ public class IdentityBinder {
HibernateIdentity id = gormMapping != null ? gormMapping.getIdentity() : null;
if (id instanceof CompositeIdentity
|| (id == null && domainClass.getCompositeIdentity() != null)) {
- compositeIdBinder.bindCompositeId(domainClass, root, (CompositeIdentity) id, mappings);
+ compositeIdBinder.bindCompositeId(domainClass, root, (CompositeIdentity) id);
} else {
Identity identity = id instanceof Identity ? (Identity) id : null;
if (identity != null && identity.getName() == null) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
index 4433c496b1..1b8efc451d 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
@@ -111,7 +111,7 @@ public class RootPersistentClassCommonValuesBinder {
identityBinder.bindIdentity(domainClass, root, mappings, gormMapping);
versionBinder.bindVersion(domainClass.getVersion(), root);
root.createPrimaryKey();
- classPropertiesBinder.bindClassProperties(domainClass, root, mappings);
+ classPropertiesBinder.bindClassProperties(domainClass, root);
return root;
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
index 8a80745b3e..4ec70d17d8 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
@@ -82,7 +82,7 @@ public class SubclassMappingBinder {
subClass.setAbstract(subEntity.isAbstract());
subClass.setEntityName(subEntity.getName());
subClass.setJpaEntityName(GrailsHibernateUtil.unqualify(subEntity.getName()));
- classPropertiesBinder.bindClassProperties(subEntity, subClass, mappings);
+ classPropertiesBinder.bindClassProperties(subEntity, subClass);
return subClass;
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
index fdfbd839c9..629c3e9f6f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
@@ -91,7 +91,8 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
ComponentBinder componentBinder = new ComponentBinder(
metadataBuildingContext,
binder.getMappingCacheHolder(),
- componentUpdater
+ componentUpdater,
+ collector
)
GrailsPropertyBinder propertyBinder = new GrailsPropertyBinder(
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
index 52d939a864..352c4bb694 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
@@ -79,7 +79,8 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
binder = new ComponentBinder(
metadataBuildingContext,
mappingCacheHolder,
- componentUpdater
+ componentUpdater,
+ metadataBuildingContext.getMetadataCollector()
)
binder.setGrailsPropertyBinder(grailsPropertyBinder)
}
@@ -126,13 +127,13 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
def mappings = metadataBuildingContext.getMetadataCollector()
when:
- def component = binder.bindComponent(root, embeddedProp, mappings, "")
+ def component = binder.bindComponent(root, embeddedProp, "")
then:
component.getComponentClassName() == Address.name
component.getRoleName() == Address.name + ".address"
1 * mappingCacheHolder.cacheMapping(associatedEntity)
- 1 * grailsPropertyBinder.bindProperty(root, root.getTable(), "address", embeddedProp, prop1, mappings) >> new BasicValue(metadataBuildingContext, root.getTable())
+ 1 * grailsPropertyBinder.bindProperty(root, root.getTable(), "address", embeddedProp, prop1) >> new BasicValue(metadataBuildingContext, root.getTable())
1 * componentUpdater.updateComponent(_ as Component, embeddedProp, prop1, _ as Value)
}
@@ -173,12 +174,12 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
def mappings = metadataBuildingContext.getMetadataCollector()
when:
- binder.bindComponent(root, embeddedProp, mappings, "")
+ binder.bindComponent(root, embeddedProp, "")
then:
0 * componentUpdater.updateComponent(_, _, idProp, _)
0 * componentUpdater.updateComponent(_, _, versionProp, _)
- 1 * grailsPropertyBinder.bindProperty(root, root.getTable(), "address", embeddedProp, normalProp, mappings) >> new BasicValue(metadataBuildingContext, root.getTable())
+ 1 * grailsPropertyBinder.bindProperty(root, root.getTable(), "address", embeddedProp, normalProp) >> new BasicValue(metadataBuildingContext, root.getTable())
1 * componentUpdater.updateComponent(_, _, normalProp, _)
}
@@ -213,7 +214,7 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
def mappings = metadataBuildingContext.getMetadataCollector()
when:
- def component = binder.bindComponent(root, embeddedProp, mappings, "")
+ def component = binder.bindComponent(root, embeddedProp, "")
then:
component.getParentProperty() == "myEntity"
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdBinderSpec.groovy
index 95293a2a1d..3725607186 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdBinderSpec.groovy
@@ -51,13 +51,13 @@ class CompositeIdBinderSpec extends HibernateGormDatastoreSpec {
root.setTable(table)
when:
- binder.bindCompositeId(domainClass, root, compositeIdentity, mappings)
+ binder.bindCompositeId(domainClass, root, compositeIdentity)
then:
root.getIdentifier() instanceof Component
root.getIdentifierMapper() instanceof Component
root.hasEmbeddedIdentifier()
- 2 * grailsPropertyBinder.bindProperty(root, table, "", identifierProp, _ as HibernatePersistentProperty, mappings) >> Mock(Value)
+ 2 * grailsPropertyBinder.bindProperty(root, table, "", identifierProp, _ as HibernatePersistentProperty) >> Mock(Value)
2 * componentUpdater.updateComponent(_ as Component, identifierProp, _ as HibernatePersistentProperty, _ as Value)
}
@@ -79,10 +79,10 @@ class CompositeIdBinderSpec extends HibernateGormDatastoreSpec {
root.setTable(table)
when:
- binder.bindCompositeId(domainClass, root, null, mappings)
+ binder.bindCompositeId(domainClass, root, null)
then:
- 1 * grailsPropertyBinder.bindProperty(root, table, "", identifierProp, prop1, mappings) >> Mock(Value)
+ 1 * grailsPropertyBinder.bindProperty(root, table, "", identifierProp, prop1) >> Mock(Value)
1 * componentUpdater.updateComponent(_ as Component, identifierProp, prop1, _ as Value)
}
@@ -97,7 +97,7 @@ class CompositeIdBinderSpec extends HibernateGormDatastoreSpec {
domainClass.getName() >> "MyEntity"
when:
- binder.bindCompositeId(domainClass, root, null, mappings)
+ binder.bindCompositeId(domainClass, root, null)
then:
thrown(org.hibernate.MappingException)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
index e41830b042..4c01fa8d17 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
@@ -156,7 +156,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
ComponentBinder componentBinder = new ComponentBinder(
metadataBuildingContext,
binder.getMappingCacheHolder(),
- componentUpdater
+ componentUpdater,
+ collector
)
GrailsPropertyBinder propertyBinder = new GrailsPropertyBinder(
@@ -236,7 +237,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def titleProp = persistentEntity.getPropertyByName("title") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, titleProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, titleProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, titleProp))
then:
@@ -263,7 +264,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
statusProp.isValidHibernateManyToOne() >> false
when:
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, statusProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, statusProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, statusProp))
then:
@@ -290,7 +291,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def ownerProp = petEntity.getPropertyByName("owner") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, ownerProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, ownerProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, ownerProp))
then:
@@ -313,7 +314,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def addressProp = persistentEntity.getPropertyByName("homeAddress") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, addressProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, addressProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, addressProp))
then:
@@ -341,7 +342,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def petsProp = personEntity.getPropertyByName("pets") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, petsProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, petsProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, petsProp))
then:
@@ -379,7 +380,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def booksProp = authorEntity.getPropertyByName("books") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, booksProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, booksProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, booksProp))
collector.processSecondPasses(binder.getMetadataBuildingContext())
@@ -418,7 +419,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def booksProp = authorEntity.getPropertyByName("books") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, booksProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, booksProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, booksProp))
collector.processSecondPasses(binder.getMetadataBuildingContext())
@@ -479,7 +480,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
def childBookProp = authorEntity.getPropertyByName("childBook") as HibernatePersistentProperty
- Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, childBookProp, collector)
+ Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, childBookProp)
rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, childBookProp))
// Process second passes to ensure Hibernate's internal mappings are finalized
collector.processSecondPasses(binder.getMetadataBuildingContext())
@@ -547,7 +548,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
when:
// Capture the return value of bindProperty
- def resultValue = propertyBinder.bindProperty(rootClass, table, EMPTY_PATH, null, currentGrailsProp, mappings)
+ def resultValue = propertyBinder.bindProperty(rootClass, table, EMPTY_PATH, null, currentGrailsProp)
then:
// Assert that bindProperty returns a Value object
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/IdentityBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/IdentityBinderSpec.groovy
index df50641f47..baff483d68 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/IdentityBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/IdentityBinderSpec.groovy
@@ -55,7 +55,7 @@ class IdentityBinderSpec extends HibernateGormDatastoreSpec {
binder.bindIdentity(domainClass, root, mappings, null)
then:
- 1 * compositeIdBinder.bindCompositeId(domainClass, root, null, mappings)
+ 1 * compositeIdBinder.bindCompositeId(domainClass, root, null)
}
def "should delegate to compositeIdBinder when mapping specifies composite identity"() {
@@ -71,7 +71,7 @@ class IdentityBinderSpec extends HibernateGormDatastoreSpec {
binder.bindIdentity(domainClass, root, mappings, gormMapping)
then:
- 1 * compositeIdBinder.bindCompositeId(domainClass, root, compositeIdentity, mappings)
+ 1 * compositeIdBinder.bindCompositeId(domainClass, root, compositeIdentity)
}
def "should delegate to simpleIdBinder when mapping specifies simple identity"() {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinderSpec.groovy
index 3597e0068c..5f5be503ab 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassPropertiesBinderSpec.groovy
@@ -43,13 +43,13 @@ class ClassPropertiesBinderSpec extends HibernateGormDatastoreSpec {
domainClass.getMappedForm() >> mapping
when:
- binder.bindClassProperties(domainClass, persistentClass, mappings)
+ binder.bindClassProperties(domainClass, persistentClass)
then:
- 1 * grailsPropertyBinder.bindProperty(persistentClass, persistentClass.table, GrailsDomainBinder.EMPTY_PATH, null, prop1, mappings) >> value1
+ 1 * grailsPropertyBinder.bindProperty(persistentClass, persistentClass.table, GrailsDomainBinder.EMPTY_PATH, null, prop1) >> value1
1 * propertyFromValueCreator.createProperty(value1, prop1) >> hibernateProp1
- 1 * grailsPropertyBinder.bindProperty(persistentClass, persistentClass.table, GrailsDomainBinder.EMPTY_PATH, null, prop2, mappings) >> value2
+ 1 * grailsPropertyBinder.bindProperty(persistentClass, persistentClass.table, GrailsDomainBinder.EMPTY_PATH, null, prop2) >> value2
1 * propertyFromValueCreator.createProperty(value2, prop2) >> hibernateProp2
persistentClass.getProperty("hibernateProp1") == hibernateProp1
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
index c9b66dbe4f..49878ddaee 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
@@ -56,7 +56,7 @@ class RootPersistentClassCommonValuesBinderSpec extends HibernateGormDatastoreSp
RootClass rootClass = binder.bindRootPersistentClassCommonValues(entity, [])
then:
- 1 * classPropertiesBinder.bindClassProperties(entity, _, _)
+ 1 * classPropertiesBinder.bindClassProperties(entity, _)
rootClass != null
rootClass.getEntityName() == TestEntity.name
rootClass.isAbstract() == false
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
index 3d26e162a3..55eb97790e 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
@@ -54,7 +54,7 @@ class SubclassMappingBinderSpec extends HibernateGormDatastoreSpec {
then:
subEntity != null
1 * singleTableSubclassBinder.bindSubClass(subEntity, _ as SingleTableSubclass, mappings)
- 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass, mappings)
+ 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass)
subClass instanceof SingleTableSubclass
subClass.getEntityName() == SMBSSingleSub.name
}
@@ -74,7 +74,7 @@ class SubclassMappingBinderSpec extends HibernateGormDatastoreSpec {
then:
subEntity != null
1 * joinedSubClassBinder.bindJoinedSubClass(subEntity, _ as JoinedSubclass)
- 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass, mappings)
+ 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass)
subClass instanceof JoinedSubclass
subClass.getEntityName() == SMBSJoinedSub.name
}
@@ -94,7 +94,7 @@ class SubclassMappingBinderSpec extends HibernateGormDatastoreSpec {
then:
subEntity != null
1 * unionSubclassBinder.bindUnionSubclass(subEntity, _ as UnionSubclass)
- 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass, mappings)
+ 1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass)
subClass instanceof UnionSubclass
subClass.getEntityName() == SMBSUnionSub.name
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
index f136a62ced..5266866570 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
@@ -90,7 +90,8 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
ComponentBinder componentBinder = new ComponentBinder(
metadataBuildingContext,
binder.getMappingCacheHolder(),
- componentUpdater
+ componentUpdater,
+ getCollector()
)
GrailsPropertyBinder propertyBinder = new GrailsPropertyBinder(
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
index cfbb66a509..9b1c4d1b53 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
@@ -90,7 +90,8 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
ComponentBinder componentBinder = new ComponentBinder(
metadataBuildingContext,
binder.getMappingCacheHolder(),
- componentUpdater
+ componentUpdater,
+ getCollector()
)
GrailsPropertyBinder propertyBinder = new GrailsPropertyBinder(
(grails-core) 04/24: refactor isValidHibernateOneToOne and isValidHibernateManyToOne
Posted by bo...@apache.org.
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 1b0e61cdf70104f7012c6974d3c0728c69983985
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 09:48:34 2026 -0600
refactor isValidHibernateOneToOne and isValidHibernateManyToOne
---
.../domainbinding/binder/GrailsPropertyBinder.java | 2 +-
.../cfg/domainbinding/binder/ManyToOneBinder.java | 2 +-
.../hibernate/HibernateManyToOneProperty.java | 8 ++++++++
.../hibernate/HibernateOneToOneProperty.java | 10 ++++++++++
.../hibernate/HibernatePersistentProperty.java | 4 ++--
.../hibernate/HibernateToOneProperty.java | 13 -------------
.../cfg/GrailsHibernatePersistentPropertySpec.groovy | 8 ++++----
.../cfg/domainbinding/ComponentBinderSpec.groovy | 20 ++++++++++----------
.../domainbinding/GrailsPropertyBinderSpec.groovy | 4 ++--
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 2 +-
10 files changed, 39 insertions(+), 34 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index 63e5c95542..4d949b2746 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -79,7 +79,7 @@ public class GrailsPropertyBinder {
value =
enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, path);
} else if (currentGrailsProp instanceof HibernateToOneProperty toOne) {
- if (toOne.isHibernateOneToOne()) {
+ if (toOne.isValidHibernateOneToOne()) {
value =
oneToOneBinder.bindOneToOne(
(org.grails.datastore.mapping.model.types.OneToOne) toOne,
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index f0cde199da..9dd8fe4f9f 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -136,7 +136,7 @@ public class ManyToOneBinder {
c.setUnique(config.isUnique());
} else if (property.isBidirectional()
&& property.getHibernateInverseSide() instanceof HibernateToOneProperty inverseSide
- && inverseSide.isHibernateOneToOne()) {
+ && inverseSide.isValidHibernateOneToOne()) {
c.setUnique(true);
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java
index dacd497b16..97cfb69171 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java
@@ -21,6 +21,7 @@ package org.grails.orm.hibernate.cfg.domainbinding.hibernate;
import java.beans.PropertyDescriptor;
import org.grails.datastore.mapping.model.MappingContext;
import org.grails.datastore.mapping.model.PersistentEntity;
+import org.grails.datastore.mapping.model.types.OneToOne;
import org.grails.datastore.mapping.model.types.mapping.ManyToOneWithMapping;
import org.grails.orm.hibernate.cfg.PropertyConfig;
@@ -36,4 +37,11 @@ public class HibernateManyToOneProperty extends ManyToOneWithMapping<PropertyCon
public GrailsHibernatePersistentEntity getHibernateAssociatedEntity() {
return (GrailsHibernatePersistentEntity) super.getAssociatedEntity();
}
+
+
+
+ public boolean isValidHibernateManyToOne() {
+ validateAssociation();
+ return true;
+ }
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
index d0a70f13cb..2328fe1707 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
@@ -48,4 +48,14 @@ public class HibernateOneToOneProperty extends OneToOneWithMapping<PropertyConfi
public GrailsHibernatePersistentEntity getHibernateAssociatedEntity() {
return (GrailsHibernatePersistentEntity) super.getAssociatedEntity();
}
+
+ public boolean isValidHibernateOneToOne() {
+ validateAssociation();
+ return canBindOneToOneWithSingleColumnAndForeignKey() || isHasOne() && isBidirectional() && getInverseSide() != null;
+ }
+
+ public boolean isValidHibernateManyToOne() {
+ validateAssociation();
+ return !isValidHibernateOneToOne();
+ }
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernatePersistentProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernatePersistentProperty.java
index aadca583b7..8c114539e0 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernatePersistentProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernatePersistentProperty.java
@@ -129,11 +129,11 @@ public interface HibernatePersistentProperty extends PersistentProperty<Property
return Optional.ofNullable(getType()).map(Class::isEnum).orElse(false);
}
- default boolean isHibernateOneToOne() {
+ default boolean isValidHibernateOneToOne() {
return false;
}
- default boolean isHibernateManyToOne() {
+ default boolean isValidHibernateManyToOne() {
return false;
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToOneProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToOneProperty.java
index 27029a3e8b..f301a3ee95 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToOneProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToOneProperty.java
@@ -26,18 +26,5 @@ import org.grails.datastore.mapping.model.types.OneToOne;
*/
public interface HibernateToOneProperty extends HibernateAssociation {
- default boolean isHibernateOneToOne() {
- validateAssociation();
- return this instanceof OneToOne association
- && (association.canBindOneToOneWithSingleColumnAndForeignKey()
- || (association.isHasOne()
- && association.isBidirectional()
- && association.getInverseSide() != null));
- }
- default boolean isHibernateManyToOne() {
- validateAssociation();
- return this instanceof org.grails.datastore.mapping.model.types.ManyToOne
- || (this instanceof OneToOne && !isHibernateOneToOne());
- }
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentPropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentPropertySpec.groovy
index ed651ee9bb..807269233a 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentPropertySpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentPropertySpec.groovy
@@ -107,10 +107,10 @@ class GrailsHibernatePersistentPropertySpec extends HibernateGormDatastoreSpec {
HibernatePersistentProperty manyToOneProp = (HibernatePersistentProperty) entity.getPropertyByName("manyToOne")
expect:
- oneToOneProp.isHibernateOneToOne()
- !oneToOneProp.isHibernateManyToOne()
- !manyToOneProp.isHibernateOneToOne()
- manyToOneProp.isHibernateManyToOne()
+ oneToOneProp.isValidHibernateOneToOne()
+ !oneToOneProp.isValidHibernateManyToOne()
+ !manyToOneProp.isValidHibernateOneToOne()
+ manyToOneProp.isValidHibernateManyToOne()
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
index bd65b66b03..52d939a864 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ComponentBinderSpec.groovy
@@ -111,15 +111,15 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
embeddedProp.getOwner() >> Mock(GrailsHibernatePersistentEntity) {
getJavaClass() >> MyEntity
}
- embeddedProp.isHibernateOneToOne() >> false
- embeddedProp.isHibernateManyToOne() >> false
+ embeddedProp.isValidHibernateOneToOne() >> false
+ embeddedProp.isValidHibernateManyToOne() >> false
associatedEntity.getName() >> "Address"
def prop1 = Mock(TestSimple)
prop1.getName() >> "street"
prop1.getType() >> String
- prop1.isHibernateOneToOne() >> false
- prop1.isHibernateManyToOne() >> false
+ prop1.isValidHibernateOneToOne() >> false
+ prop1.isValidHibernateManyToOne() >> false
associatedEntity.getHibernateParentProperty(MyEntity) >> Optional.empty()
associatedEntity.getHibernatePersistentProperties(MyEntity) >> [prop1]
@@ -152,8 +152,8 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
embeddedProp.getOwner() >> Mock(GrailsHibernatePersistentEntity) {
getJavaClass() >> MyEntity
}
- embeddedProp.isHibernateOneToOne() >> false
- embeddedProp.isHibernateManyToOne() >> false
+ embeddedProp.isValidHibernateOneToOne() >> false
+ embeddedProp.isValidHibernateManyToOne() >> false
associatedEntity.getName() >> "Address"
def idProp = Mock(org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateIdentityProperty)
@@ -163,8 +163,8 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
def normalProp = Mock(TestSimple)
normalProp.getName() >> "street"
normalProp.getType() >> String
- normalProp.isHibernateOneToOne() >> false
- normalProp.isHibernateManyToOne() >> false
+ normalProp.isValidHibernateOneToOne() >> false
+ normalProp.isValidHibernateManyToOne() >> false
associatedEntity.getIdentity() >> idProp
associatedEntity.getHibernateParentProperty(MyEntity) >> Optional.empty()
@@ -198,8 +198,8 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec {
embeddedProp.getOwner() >> Mock(GrailsHibernatePersistentEntity) {
getJavaClass() >> MyEntity
}
- embeddedProp.isHibernateOneToOne() >> false
- embeddedProp.isHibernateManyToOne() >> false
+ embeddedProp.isValidHibernateOneToOne() >> false
+ embeddedProp.isValidHibernateManyToOne() >> false
associatedEntity.getName() >> "Address"
def parentProp = Mock(TestSimple)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
index 2989687ecb..e41830b042 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
@@ -259,8 +259,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
def statusProp = Mock(TestSimpleEnum)
setupProperty(statusProp, "status", new Mapping(), persistentEntity)
statusProp.getType() >> java.util.concurrent.TimeUnit
- statusProp.isHibernateOneToOne() >> false
- statusProp.isHibernateManyToOne() >> false
+ statusProp.isValidHibernateOneToOne() >> false
+ statusProp.isValidHibernateManyToOne() >> false
when:
Value value = propertyBinder.bindProperty(rootClass, rootClass.table, EMPTY_PATH, null, statusProp, collector)
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 789054f2e7..6b368af0aa 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
@@ -128,7 +128,7 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
propertyConfig.isUniqueWithinGroup() >> isUniqueWithinGroup
property.isBidirectional() >> isBidirectional
property.getHibernateInverseSide() >> inverseSide
- inverseSide.isHibernateOneToOne() >> isInverseHasOne
+ inverseSide.isValidHibernateOneToOne() >> isInverseHasOne
when:
def result = binder.bindManyToOne(property, null, "/test")
(grails-core) 07/24: refactor methods into HibernateOneToOneProperty
Posted by bo...@apache.org.
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 12f7fb8e2f61743f53eb778bcd192bc34b4977f3
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 11:35:52 2026 -0600
refactor methods into HibernateOneToOneProperty
---
.../cfg/domainbinding/binder/ManyToOneBinder.java | 5 +-
.../cfg/domainbinding/binder/OneToOneBinder.java | 40 +-----
.../hibernate/HibernateOneToOneProperty.java | 52 +++++++
.../HibernateOneToOnePropertySpec.groovy | 149 +++++++++++++++++++++
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 8 +-
.../cfg/domainbinding/OneToOneBinderSpec.groovy | 48 +++----
6 files changed, 231 insertions(+), 71 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index bad0ebc63e..35e0af023a 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -31,7 +31,6 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
import org.hibernate.MappingException;
import org.hibernate.boot.spi.MetadataBuildingContext;
@@ -116,7 +115,6 @@ public class ManyToOneBinder {
return mapping != null && mapping.hasCompositeIdentifier();
}
- @SuppressWarnings("unchecked")
private ManyToOne doBind(
HibernateAssociation property,
GrailsHibernatePersistentEntity refDomainClass,
@@ -146,8 +144,7 @@ public class ManyToOneBinder {
if (!config.isUniqueWithinGroup()) {
c.setUnique(config.isUnique());
} else if (property.isBidirectional()
- && property.getHibernateInverseSide() instanceof HibernateToOneProperty inverseSide
- && inverseSide.isValidHibernateOneToOne()) {
+ && property.getHibernateInverseSide().isValidHibernateOneToOne()) {
c.setUnique(true);
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
index 236e2a6e07..583f6dd2e9 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
@@ -18,17 +18,10 @@
*/
package org.grails.orm.hibernate.cfg.domainbinding.binder;
-import org.grails.datastore.mapping.model.types.Association;
-import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
-import org.grails.orm.hibernate.cfg.PropertyConfig;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
-import org.hibernate.FetchMode;
import org.hibernate.boot.spi.MetadataBuildingContext;
-import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.PersistentClass;
-import org.hibernate.type.ForeignKeyDirection;
public class OneToOneBinder {
@@ -41,14 +34,6 @@ public class OneToOneBinder {
this.simpleValueBinder = simpleValueBinder;
}
- public OneToOneBinder(
- MetadataBuildingContext metadataBuildingContext,
- PersistentEntityNamingStrategy namingStrategy,
- JdbcEnvironment jdbcEnvironment) {
- this(
- metadataBuildingContext,
- new SimpleValueBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment));
- }
public OneToOne bindOneToOne(
final HibernateOneToOneProperty property,
@@ -56,32 +41,19 @@ public class OneToOneBinder {
org.hibernate.mapping.Table table,
String path) {
OneToOne oneToOne = new OneToOne(metadataBuildingContext, table, owner);
- PropertyConfig config = property.getMappedForm();
- final Association otherSide = property.getInverseSide();
- final boolean hasOne = otherSide != null && otherSide.isHasOne();
- oneToOne.setConstrained(hasOne);
- oneToOne.setForeignKeyType(
- oneToOne.isConstrained() ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT);
+ oneToOne.setConstrained(property.isHibernateConstrained());
+ oneToOne.setForeignKeyType(property.getHibernateForeignKeyDirection());
oneToOne.setAlternateUniqueKey(true);
-
- if (config != null && config.getFetchMode() != null) {
- oneToOne.setFetchMode(config.getFetchMode());
- } else {
- oneToOne.setFetchMode(FetchMode.DEFAULT);
- }
-
- oneToOne.setReferencedEntityName(
- otherSide != null
- ? otherSide.getOwner().getName()
- : property.getAssociatedEntity().getName());
+ oneToOne.setFetchMode(property.getHibernateFetchMode());
+ oneToOne.setReferencedEntityName(property.getHibernateReferencedEntityName());
oneToOne.setPropertyName(property.getName());
oneToOne.setReferenceToPrimaryKey(false);
- if (hasOne || otherSide == null) {
+ if (property.needsSimpleValueBinding()) {
simpleValueBinder.bindSimpleValue(property, null, oneToOne, path);
} else {
- oneToOne.setReferencedPropertyName(otherSide.getName());
+ oneToOne.setReferencedPropertyName(property.getHibernateReferencedPropertyName());
}
return oneToOne;
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
index 2328fe1707..ac85c3407f 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToOneProperty.java
@@ -23,7 +23,9 @@ import org.grails.datastore.mapping.model.MappingContext;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.grails.datastore.mapping.model.types.mapping.OneToOneWithMapping;
import org.grails.orm.hibernate.cfg.PropertyConfig;
+import org.hibernate.FetchMode;
import org.hibernate.MappingException;
+import org.hibernate.type.ForeignKeyDirection;
/** Hibernate implementation of {@link org.grails.datastore.mapping.model.types.OneToOne} */
public class HibernateOneToOneProperty extends OneToOneWithMapping<PropertyConfig>
@@ -49,6 +51,56 @@ public class HibernateOneToOneProperty extends OneToOneWithMapping<PropertyConfi
return (GrailsHibernatePersistentEntity) super.getAssociatedEntity();
}
+ @Override
+ public HibernateOneToOneProperty getHibernateInverseSide() {
+ return (HibernateOneToOneProperty) getInverseSide();
+ }
+
+ /** True when the FK is on this side (hasOne on the other side). Maps to Hibernate constrained. */
+ public boolean isHibernateConstrained() {
+ HibernateOneToOneProperty otherSide = getHibernateInverseSide();
+ return otherSide != null && otherSide.isHasOne();
+ }
+
+ /**
+ * The entity name that Hibernate should reference. When the other side exists, it is the other
+ * side's owner; otherwise the directly associated entity.
+ */
+ public String getHibernateReferencedEntityName() {
+ HibernateOneToOneProperty otherSide = getHibernateInverseSide();
+ return otherSide != null
+ ? otherSide.getOwner().getName()
+ : getAssociatedEntity().getName();
+ }
+
+ /**
+ * The property name on the referenced entity that back-references this association. Only
+ * meaningful when {@link #isHibernateConstrained()} is false and the other side exists.
+ */
+ public String getHibernateReferencedPropertyName() {
+ HibernateOneToOneProperty otherSide = getHibernateInverseSide();
+ return otherSide != null ? otherSide.getName() : null;
+ }
+
+ /** FK direction: FROM_PARENT when constrained (hasOne on other side), TO_PARENT otherwise. */
+ public ForeignKeyDirection getHibernateForeignKeyDirection() {
+ return isHibernateConstrained() ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT;
+ }
+
+ /** Resolved fetch mode: uses the configured value or falls back to {@link FetchMode#DEFAULT}. */
+ public FetchMode getHibernateFetchMode() {
+ PropertyConfig config = getMappedForm();
+ return (config != null && config.getFetchMode() != null) ? config.getFetchMode() : FetchMode.DEFAULT;
+ }
+
+ /**
+ * True when Hibernate should bind a simple column value rather than a referenced property name.
+ * This is the case when the FK is on this side (constrained) or no inverse side exists.
+ */
+ public boolean needsSimpleValueBinding() {
+ return isHibernateConstrained() || getHibernateReferencedPropertyName() == null;
+ }
+
public boolean isValidHibernateOneToOne() {
validateAssociation();
return canBindOneToOneWithSingleColumnAndForeignKey() || isHasOne() && isBidirectional() && getInverseSide() != null;
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/HibernateOneToOnePropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/HibernateOneToOnePropertySpec.groovy
new file mode 100644
index 0000000000..d178cfa3d8
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/HibernateOneToOnePropertySpec.groovy
@@ -0,0 +1,149 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding
+
+import grails.gorm.annotation.Entity
+import grails.gorm.hibernate.HibernateEntity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
+import org.hibernate.FetchMode
+import org.hibernate.type.ForeignKeyDirection
+
+class HibernateOneToOnePropertySpec extends HibernateGormDatastoreSpec {
+
+ def setupSpec() {
+ manager.addAllDomainClasses([OneToOneFace, OneToOneNose])
+ }
+
+ void "getHibernateInverseSide returns HibernateOneToOneProperty"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ noseProp.getHibernateInverseSide() instanceof HibernateOneToOneProperty
+ noseProp.getHibernateInverseSide().name == 'face'
+ }
+
+ void "isHibernateConstrained is false when other side does not have hasOne"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ !noseProp.isHibernateConstrained()
+ }
+
+ void "isHibernateConstrained is true when other side has hasOne"() {
+ when:
+ def noseEntity = mappingContext.getPersistentEntity(OneToOneNose.name)
+ def faceProp = noseEntity.persistentProperties.find { it.name == 'face' } as HibernateOneToOneProperty
+
+ then:
+ faceProp.isHibernateConstrained()
+ }
+
+ void "getHibernateReferencedEntityName returns other side owner name when inverse exists"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ noseProp.getHibernateReferencedEntityName() == OneToOneNose.name
+ }
+
+ void "getHibernateReferencedPropertyName returns inverse side name when inverse exists"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ noseProp.getHibernateReferencedPropertyName() == 'face'
+ }
+
+ void "getHibernateReferencedPropertyName returns null when no inverse"() {
+ when:
+ def noseEntity = mappingContext.getPersistentEntity(OneToOneNose.name)
+ // face belongs to OneToOneFace via hasOne — it has no inverse side from nose's perspective
+ def faceProp = noseEntity.persistentProperties.find { it.name == 'face' } as HibernateOneToOneProperty
+
+ then:
+ // face's inverse is the nose prop on the owning side, so referencedPropertyName is 'nose'
+ faceProp.getHibernateReferencedPropertyName() == 'nose'
+ }
+
+ void "getHibernateForeignKeyDirection returns TO_PARENT when not constrained"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ noseProp.getHibernateForeignKeyDirection() == ForeignKeyDirection.TO_PARENT
+ }
+
+ void "getHibernateForeignKeyDirection returns FROM_PARENT when constrained"() {
+ when:
+ def noseEntity = mappingContext.getPersistentEntity(OneToOneNose.name)
+ def faceProp = noseEntity.persistentProperties.find { it.name == 'face' } as HibernateOneToOneProperty
+
+ then:
+ faceProp.getHibernateForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT
+ }
+
+ void "getHibernateFetchMode returns DEFAULT when no fetch config"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ noseProp.getHibernateFetchMode() == FetchMode.DEFAULT
+ }
+
+ void "needsSimpleValueBinding is false when not constrained and inverse exists"() {
+ when:
+ def faceEntity = mappingContext.getPersistentEntity(OneToOneFace.name)
+ def noseProp = faceEntity.persistentProperties.find { it.name == 'nose' } as HibernateOneToOneProperty
+
+ then:
+ !noseProp.needsSimpleValueBinding()
+ }
+
+ void "needsSimpleValueBinding is true when constrained"() {
+ when:
+ def noseEntity = mappingContext.getPersistentEntity(OneToOneNose.name)
+ def faceProp = noseEntity.persistentProperties.find { it.name == 'face' } as HibernateOneToOneProperty
+
+ then:
+ faceProp.needsSimpleValueBinding()
+ }
+}
+
+@Entity
+class OneToOneFace implements HibernateEntity<OneToOneFace> {
+ String name
+ OneToOneNose nose
+ static hasOne = [nose: OneToOneNose]
+}
+
+@Entity
+class OneToOneNose implements HibernateEntity<OneToOneNose> {
+ Boolean hasFreckles
+ OneToOneFace face
+ static belongsTo = [face: OneToOneFace]
+}
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 6b368af0aa..e358200774 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
@@ -117,7 +117,7 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
}
def propertyConfig = Mock(PropertyConfig)
def column = new Column('test')
- def inverseSide = Mock(HibernateToOneProperty)
+ def inverseSide = Mock(TestOneToOneInverse)
property.getHibernateAssociatedEntity() >> refDomainClass
mapping.setIdentity(null)
@@ -179,3 +179,9 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
thrown(MappingException)
}
}
+
+abstract class TestOneToOneInverse extends HibernateOneToOneProperty {
+ TestOneToOneInverse(org.grails.datastore.mapping.model.PersistentEntity owner, org.grails.datastore.mapping.model.MappingContext context, java.beans.PropertyDescriptor descriptor) {
+ super(owner, context, descriptor)
+ }
+}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
index 4b5e9d2073..f2759532ea 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
@@ -1,12 +1,9 @@
package org.grails.orm.hibernate.cfg.domainbinding
import grails.gorm.specs.HibernateGormDatastoreSpec
-import org.grails.datastore.mapping.model.types.OneToOne as GormOneToOne
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
-import org.grails.orm.hibernate.cfg.PropertyConfig
import org.hibernate.FetchMode
import org.hibernate.mapping.OneToOne as HibernateOneToOne
import org.hibernate.mapping.RootClass
@@ -33,20 +30,16 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
def ownerRoot = new RootClass(metadataBuildingContext)
def gormOneToOne = Mock(TestOneToOne)
- def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
- def otherOwner = Mock(PersistentEntity)
- gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getName() >> "myOneToOne"
gormOneToOne.getOwner() >> owner
- gormOneToOne.getMappedForm() >> new PropertyConfig()
-
- otherSide.isHasOne() >> false
- otherSide.getOwner() >> otherOwner
- otherSide.getName() >> "otherSide"
-
- otherOwner.getName() >> "OtherEntity"
+ gormOneToOne.isHibernateConstrained() >> false
+ gormOneToOne.getHibernateForeignKeyDirection() >> ForeignKeyDirection.TO_PARENT
+ gormOneToOne.getHibernateFetchMode() >> FetchMode.DEFAULT
+ gormOneToOne.getHibernateReferencedEntityName() >> "OtherEntity"
+ gormOneToOne.getHibernateReferencedPropertyName() >> "otherSide"
+ gormOneToOne.needsSimpleValueBinding() >> false
when:
def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
@@ -68,19 +61,15 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
def ownerRoot = new RootClass(metadataBuildingContext)
def gormOneToOne = Mock(TestOneToOne)
- def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
- def otherOwner = Mock(PersistentEntity)
- gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getName() >> "myOneToOne"
gormOneToOne.getOwner() >> owner
- gormOneToOne.getMappedForm() >> new PropertyConfig()
-
- otherSide.isHasOne() >> true
- otherSide.getOwner() >> otherOwner
-
- otherOwner.getName() >> "OtherEntity"
+ gormOneToOne.isHibernateConstrained() >> true
+ gormOneToOne.getHibernateForeignKeyDirection() >> ForeignKeyDirection.FROM_PARENT
+ gormOneToOne.getHibernateFetchMode() >> FetchMode.DEFAULT
+ gormOneToOne.getHibernateReferencedEntityName() >> "OtherEntity"
+ gormOneToOne.needsSimpleValueBinding() >> true
when:
def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
@@ -96,20 +85,15 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
def metadataBuildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
def ownerRoot = new RootClass(metadataBuildingContext)
- def propertyConfig = new PropertyConfig()
- propertyConfig.setFetch("join")
-
def gormOneToOne = Mock(TestOneToOne)
- def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
- def otherOwner = Mock(PersistentEntity)
- gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getOwner() >> owner
- gormOneToOne.getMappedForm() >> propertyConfig
-
- otherSide.getOwner() >> otherOwner
- otherSide.isHasOne() >> false
+ gormOneToOne.isHibernateConstrained() >> false
+ gormOneToOne.getHibernateForeignKeyDirection() >> ForeignKeyDirection.TO_PARENT
+ gormOneToOne.getHibernateFetchMode() >> FetchMode.JOIN
+ gormOneToOne.getHibernateReferencedEntityName() >> "OtherEntity"
+ gormOneToOne.needsSimpleValueBinding() >> true
when:
def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
(grails-core) 16/24: refactor: extract bindCollectionKey from CollectionSecondPassBinder into CollectionKeyBinder
Posted by bo...@apache.org.
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 71504251357966821cf653a66d3f8952fe24fc6d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 10:55:17 2026 -0600
refactor: extract bindCollectionKey from CollectionSecondPassBinder into CollectionKeyBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 8 +-
.../secondpass/CollectionKeyBinder.java | 70 ++++++++
.../secondpass/CollectionSecondPassBinder.java | 44 +----
.../secondpass/CollectionKeyBinderSpec.groovy | 190 +++++++++++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 2 +-
5 files changed, 270 insertions(+), 44 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 174e31d7da..f8ec48d3fd 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -30,6 +30,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToManyLinker;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionMultiTenantFilterBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionOrderByBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionSecondPassBinder;
@@ -110,12 +111,13 @@ public class CollectionBinder {
manyToOneBinder,
new PrimaryKeyValueCreator(metadataBuildingContext),
new CollectionKeyColumnUpdater(),
- new BidirectionalOneToManyLinker(grailsPropertyResolver),
- new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
collectionWithJoinTableBinder,
collectionForPropertyConfigBinder,
- simpleValueColumnBinder,
+ new CollectionKeyBinder(
+ new BidirectionalOneToManyLinker(grailsPropertyResolver),
+ new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
+ simpleValueColumnBinder),
new CollectionOrderByBinder(),
new CollectionMultiTenantFilterBinder(new DefaultColumnNameFetcher(namingStrategy)));
this.listSecondPassBinder =
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java
new file mode 100644
index 0000000000..49d74fa661
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java
@@ -0,0 +1,70 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import java.util.Map;
+import org.grails.datastore.mapping.model.types.ToOne;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.DependantValue;
+import org.hibernate.mapping.PersistentClass;
+
+/** Binds the collection key value for a to-many association. */
+public class CollectionKeyBinder {
+
+ private final BidirectionalOneToManyLinker bidirectionalOneToManyLinker;
+ private final DependentKeyValueBinder dependentKeyValueBinder;
+ private final SimpleValueColumnBinder simpleValueColumnBinder;
+
+ /** Creates a new {@link CollectionKeyBinder} instance. */
+ public CollectionKeyBinder(
+ BidirectionalOneToManyLinker bidirectionalOneToManyLinker,
+ DependentKeyValueBinder dependentKeyValueBinder,
+ SimpleValueColumnBinder simpleValueColumnBinder) {
+ this.bidirectionalOneToManyLinker = bidirectionalOneToManyLinker;
+ this.dependentKeyValueBinder = dependentKeyValueBinder;
+ this.simpleValueColumnBinder = simpleValueColumnBinder;
+ }
+
+ /** Binds the collection key for the given property and collection. */
+ public void bind(
+ HibernateToManyProperty property,
+ DependantValue key,
+ PersistentClass associatedClass,
+ Collection collection) {
+ if (property.isBidirectional()) {
+ var inverseSide = property.getHibernateInverseSide();
+ if (inverseSide instanceof ToOne && property.shouldBindWithForeignKey()) {
+ bidirectionalOneToManyLinker.link(collection, associatedClass, key, inverseSide);
+ } else if (inverseSide instanceof HibernateManyToManyProperty
+ || Map.class.isAssignableFrom(property.getType())) {
+ dependentKeyValueBinder.bind(property, key);
+ }
+ } else {
+ if (property.getMappedForm().hasJoinKeyMapping()) {
+ simpleValueColumnBinder.bindSimpleValue(
+ key, "long", property.getMappedForm().getJoinTable().getKey().getName(), true);
+ } else {
+ dependentKeyValueBinder.bind(property, key);
+ }
+ }
+ }
+}
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 69356c87f2..c413e9eeb2 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
@@ -25,7 +25,6 @@ import java.util.*;
import java.util.Map;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
-import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
@@ -34,49 +33,38 @@ import org.hibernate.MappingException;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.*;
import org.hibernate.mapping.Collection;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/** Refactored from CollectionBinder to handle collection second pass binding. */
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class CollectionSecondPassBinder {
-
- private static final Logger LOG = LoggerFactory.getLogger(CollectionSecondPassBinder.class);
-
private final CollectionOrderByBinder collectionOrderByBinder;
private final CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder;
+ private final CollectionKeyBinder collectionKeyBinder;
private final ManyToOneBinder manyToOneBinder;
private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
- private final BidirectionalOneToManyLinker bidirectionalOneToManyLinker;
- private final DependentKeyValueBinder dependentKeyValueBinder;
private final UnidirectionalOneToManyBinder unidirectionalOneToManyBinder;
private final CollectionWithJoinTableBinder collectionWithJoinTableBinder;
private final CollectionForPropertyConfigBinder collectionForPropertyConfigBinder;
- private final SimpleValueColumnBinder simpleValueColumnBinder;
/** Creates a new {@link CollectionSecondPassBinder} instance. */
public CollectionSecondPassBinder(
ManyToOneBinder manyToOneBinder,
PrimaryKeyValueCreator primaryKeyValueCreator,
CollectionKeyColumnUpdater collectionKeyColumnUpdater,
- BidirectionalOneToManyLinker bidirectionalOneToManyLinker,
- DependentKeyValueBinder dependentKeyValueBinder,
UnidirectionalOneToManyBinder unidirectionalOneToManyBinder,
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
- SimpleValueColumnBinder simpleValueColumnBinder,
+ CollectionKeyBinder collectionKeyBinder,
CollectionOrderByBinder collectionOrderByBinder,
CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder) {
this.manyToOneBinder = manyToOneBinder;
this.primaryKeyValueCreator = primaryKeyValueCreator;
this.collectionKeyColumnUpdater = collectionKeyColumnUpdater;
- this.bidirectionalOneToManyLinker = bidirectionalOneToManyLinker;
- this.dependentKeyValueBinder = dependentKeyValueBinder;
this.unidirectionalOneToManyBinder = unidirectionalOneToManyBinder;
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
- this.simpleValueColumnBinder = simpleValueColumnBinder;
+ this.collectionKeyBinder = collectionKeyBinder;
this.collectionOrderByBinder = collectionOrderByBinder;
this.collectionMultiTenantFilterBinder = collectionMultiTenantFilterBinder;
}
@@ -99,7 +87,7 @@ public class CollectionSecondPassBinder {
DependantValue key = primaryKeyValueCreator.createPrimaryKeyValue(collection);
collection.setKey(key);
- bindCollectionKey(property, key, associatedClass, collection);
+ collectionKeyBinder.bind(property, key, associatedClass, collection);
Optional.ofNullable(property.getMappedForm().getCache())
.ifPresent(cacheConfig -> collection.setCacheConcurrencyStrategy(cacheConfig.getUsage()));
@@ -124,30 +112,6 @@ public class CollectionSecondPassBinder {
}
- private void bindCollectionKey(
- HibernateToManyProperty property,
- DependantValue key,
- PersistentClass associatedClass,
- Collection collection) {
- if (property.isBidirectional()) {
- var inverseSide = property.getHibernateInverseSide();
- if (inverseSide instanceof org.grails.datastore.mapping.model.types.ToOne
- && property.shouldBindWithForeignKey()) {
- bidirectionalOneToManyLinker.link(collection, associatedClass, key, inverseSide);
- } else if (inverseSide instanceof HibernateManyToManyProperty
- || java.util.Map.class.isAssignableFrom(property.getType())) {
- dependentKeyValueBinder.bind(property, key);
- }
- } else {
- if (property.getMappedForm().hasJoinKeyMapping()) {
- simpleValueColumnBinder.bindSimpleValue(
- key, "long", property.getMappedForm().getJoinTable().getKey().getName(), true);
- } else {
- dependentKeyValueBinder.bind(property, key);
- }
- }
- }
-
private void bindCollectionElement(
HibernateToManyProperty property,
InFlightMetadataCollector mappings,
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy
new file mode 100644
index 0000000000..12d2881e03
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy
@@ -0,0 +1,190 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver
+import org.hibernate.mapping.Bag
+import org.hibernate.mapping.BasicValue
+import org.hibernate.mapping.Column
+import org.hibernate.mapping.DependantValue
+import org.hibernate.mapping.Property
+import org.hibernate.mapping.RootClass
+import org.hibernate.mapping.Table
+import spock.lang.Subject
+
+class CollectionKeyBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ CollectionKeyBinder binder
+
+ void setupSpec() {
+ manager.addAllDomainClasses([
+ CKBBidOwner,
+ CKBBidItem,
+ CKBManyToManyOwner,
+ CKBManyToManyItem,
+ CKBUniOwner,
+ CKBUniItem,
+ CKBJoinKeyOwner,
+ CKBJoinKeyItem,
+ ])
+ }
+
+ void setup() {
+ def gdb = getGrailsDomainBinder()
+ def mbc = gdb.getMetadataBuildingContext()
+ def ns = gdb.getNamingStrategy()
+ def je = gdb.getJdbcEnvironment()
+ def svb = new SimpleValueBinder(mbc, ns, je)
+ def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
+ def botml = new BidirectionalOneToManyLinker(new GrailsPropertyResolver())
+ def dkvb = new DependentKeyValueBinder(svb, citmto)
+ def svcb = new SimpleValueColumnBinder()
+ binder = new CollectionKeyBinder(botml, dkvb, svcb)
+ }
+
+ private HibernateToManyProperty propertyFor(Class ownerClass, String name = "items") {
+ (getPersistentEntity(ownerClass) as GrailsHibernatePersistentEntity).getPropertyByName(name) as HibernateToManyProperty
+ }
+
+ private RootClass rootClassWith(String entityName, String propName, String columnName) {
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def rootClass = new RootClass(mbc)
+ rootClass.setEntityName(entityName)
+ def table = new Table("test", entityName.toLowerCase())
+ def simpleValue = new BasicValue(mbc, table)
+ simpleValue.setTypeName("long")
+ simpleValue.addColumn(new Column(columnName))
+ def prop = new Property()
+ prop.setName(propName)
+ prop.setValue(simpleValue)
+ rootClass.addProperty(prop)
+ return rootClass
+ }
+
+ private DependantValue keyWithTable(String tableName = "test_table") {
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def table = new Table("test", tableName)
+ def wrapped = new BasicValue(mbc, table)
+ return new DependantValue(mbc, table, wrapped)
+ }
+
+ def "bind sets collection inverse for bidirectional one-to-many with foreign key"() {
+ given:
+ def property = propertyFor(CKBBidOwner)
+ def associatedClass = rootClassWith(CKBBidItem.name, "owner", "OWNER_ID")
+ def key = keyWithTable("ckb_bid_item")
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, key, associatedClass, collection)
+
+ then:
+ collection.isInverse()
+ key.getColumnSpan() > 0
+ }
+
+ def "bind delegates to dependentKeyValueBinder for bidirectional many-to-many"() {
+ given:
+ def property = propertyFor(CKBManyToManyOwner)
+ def key = new DependantValue(getGrailsDomainBinder().getMetadataBuildingContext(), null, null)
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, key, associatedClass, collection)
+
+ then:
+ key.getColumnSpan() > 0
+ !collection.isInverse()
+ }
+
+ def "bind uses simpleValueColumnBinder for unidirectional with join key mapping"() {
+ given:
+ def property = propertyFor(CKBJoinKeyOwner)
+ def key = keyWithTable("ckb_join_key_owner_ckb_join_key_item")
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, key, associatedClass, collection)
+
+ then:
+ key.getTypeName() == "long"
+ key.getColumnSpan() > 0
+ !collection.isInverse()
+ }
+
+ def "bind delegates to dependentKeyValueBinder for unidirectional without join key mapping"() {
+ given:
+ def property = propertyFor(CKBUniOwner)
+ def key = new DependantValue(getGrailsDomainBinder().getMetadataBuildingContext(), null, null)
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, key, associatedClass, collection)
+
+ then:
+ key.getColumnSpan() > 0
+ !collection.isInverse()
+ }
+}
+
+@Entity
+class CKBBidOwner {
+ Long id
+ static hasMany = [items: CKBBidItem]
+}
+
+@Entity
+class CKBBidItem {
+ Long id
+ CKBBidOwner owner
+ static belongsTo = [owner: CKBBidOwner]
+}
+
+@Entity
+class CKBManyToManyOwner {
+ Long id
+ static hasMany = [items: CKBManyToManyItem]
+}
+
+@Entity
+class CKBManyToManyItem {
+ Long id
+ static hasMany = [owners: CKBManyToManyOwner]
+}
+
+@Entity
+class CKBUniOwner {
+ Long id
+ static hasMany = [items: CKBUniItem]
+}
+
+@Entity
+class CKBUniItem {
+ Long id
+ String description
+}
+
+@Entity
+class CKBJoinKeyOwner {
+ Long id
+ static hasMany = [items: CKBJoinKeyItem]
+ static mapping = {
+ items joinTable: [key: 'owner_fk']
+ }
+}
+
+@Entity
+class CKBJoinKeyItem {
+ Long id
+ String description
+}
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 bce105641c..2f01558daf 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
@@ -41,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(mtob, pkvc, cku, botml, dkvb, uotmb, cwjtb, cfpcb, svcb, new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
+ binder = new CollectionSecondPassBinder(mtob, pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
(grails-core) 02/24: Refactor CollectionSecondPassBinder and ManyToOneBinder for typed dispatch
Posted by bo...@apache.org.
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 d35d831b72a0c4d8f3c359e2e99b98b7655905c1
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 08:26:15 2026 -0600
Refactor CollectionSecondPassBinder and ManyToOneBinder for typed dispatch
Extract bindCollectionSecondPass into focused private methods:
- bindOneToManyAssociation: handles OneToMany collection setup
- applyMultiTenantFilter: applies tenant filter condition
- bindCollectionKey: handles bidirectional/unidirectional key binding
- bindCollectionElement: dispatches element binding by property type
- bindManyToManyElement: typed ManyToMany element binding
- bindBidirectionalMapElement: typed bidirectional map element binding
Replace ManyToOneBinder.bindManyToOne(HibernateAssociation) with
two typed overloads that eliminate instanceof guards:
- bindManyToOne(HibernateToOneProperty): handles direct to-one
associations; extracts bindOneToOneUniqueKey for the OneToOne case
- bindManyToOne(HibernateManyToManyProperty): handles inverse side
of ManyToMany; extracts prepareCircularManyToMany
Add HibernateManyToManyProperty.getHibernateInverseSide() covariant
override returning HibernateManyToManyProperty, enabling CollectionSecondPassBinder
to call the typed overload without a cast.
Update ManyToOneBinderSpec to call typed overloads directly
(removing 'as HibernateAssociation' casts).
Co-authored-by: Copilot <22...@users.noreply.github.com>
---
.../cfg/domainbinding/binder/ManyToOneBinder.java | 114 ++++++++++++---------
.../hibernate/HibernateManyToManyProperty.java | 5 +
.../secondpass/CollectionSecondPassBinder.java | 113 +++++++++++++-------
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 13 ++-
4 files changed, 152 insertions(+), 93 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index f24160a3a3..f0cde199da 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -77,66 +77,82 @@ public class ManyToOneBinder {
new SimpleValueColumnFetcher());
}
- /** Binds a many-to-one relationship to the */
- @SuppressWarnings("unchecked")
+ /** Binds a to-one (many-to-one or one-to-one) association. */
+ public ManyToOne bindManyToOne(
+ HibernateToOneProperty property, org.hibernate.mapping.Table table, String path) {
+ GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
+ boolean isComposite = isCompositeIdentifier(refDomainClass);
+ ManyToOne manyToOne = doBind(property, refDomainClass, isComposite, table, path);
+ if (property instanceof HibernateOneToOneProperty oneToOne && !isComposite) {
+ bindOneToOneUniqueKey(oneToOne, manyToOne);
+ }
+ return manyToOne;
+ }
+
+ /** Binds the inverse side of a many-to-many association as a collection element. */
public ManyToOne bindManyToOne(
- HibernateAssociation property, org.hibernate.mapping.Table table, String path) {
+ HibernateManyToManyProperty property, org.hibernate.mapping.Table table, String path) {
+ GrailsHibernatePersistentEntity refDomainClass = property.getHibernateOwner();
+ boolean isComposite = isCompositeIdentifier(refDomainClass);
+ if (!isComposite && property.isCircular()) {
+ prepareCircularManyToMany(property, refDomainClass.getMappedForm());
+ }
+ return doBind(property, refDomainClass, isComposite, table, path);
+ }
+
+ private static boolean isCompositeIdentifier(GrailsHibernatePersistentEntity entity) {
+ Mapping mapping = entity.getMappedForm();
+ return mapping != null && mapping.hasCompositeIdentifier();
+ }
+
+ @SuppressWarnings("unchecked")
+ private ManyToOne doBind(
+ HibernateAssociation property,
+ GrailsHibernatePersistentEntity refDomainClass,
+ boolean isComposite,
+ org.hibernate.mapping.Table table,
+ String path) {
ManyToOne manyToOne = new ManyToOne(metadataBuildingContext, table);
manyToOneValuesBinder.bindManyToOneValues(property, manyToOne);
- GrailsHibernatePersistentEntity refDomainClass =
- (property instanceof HibernateManyToManyProperty
- ? property.getHibernateOwner()
- : property.getHibernateAssociatedEntity());
- Mapping mapping = refDomainClass.getMappedForm();
-
- boolean isComposite = mapping != null && mapping.hasCompositeIdentifier();
if (isComposite) {
+ Mapping mapping = refDomainClass.getMappedForm();
CompositeIdentity ci = (CompositeIdentity) mapping.getIdentity();
compositeIdentifierToManyToOneBinder.bindCompositeIdentifierToManyToOne(
property, manyToOne, ci, refDomainClass, path);
} else {
- if (property.isCircular() && (property instanceof HibernateManyToManyProperty)) {
- PropertyConfig pc = property.getMappedForm();
-
- if (mapping != null && pc.getColumns().isEmpty()) {
- mapping.getColumns().put(property.getName(), pc);
- }
- if (!pc.hasJoinKeyMapping()) {
- JoinTable jt = new JoinTable();
- final ColumnConfig columnConfig = new ColumnConfig();
- columnConfig.setName(
- namingStrategy.resolveColumnName(property.getName()) + FOREIGN_KEY_SUFFIX);
- jt.setKey(columnConfig);
- pc.setJoinTable(jt);
- }
- // set type
- simpleValueBinder.bindSimpleValue(property, null, manyToOne, path);
- } else {
- // bind column
- // set type
- simpleValueBinder.bindSimpleValue(property, null, manyToOne, path);
- }
+ simpleValueBinder.bindSimpleValue(property, null, manyToOne, path);
}
+ return manyToOne;
+ }
+ private void bindOneToOneUniqueKey(HibernateOneToOneProperty property, ManyToOne manyToOne) {
PropertyConfig config = property.getMappedForm();
- boolean isOneToOne = property instanceof HibernateOneToOneProperty;
- boolean notComposite = !isComposite;
- if (isOneToOne && notComposite) {
- manyToOne.setAlternateUniqueKey(true);
- Column c = simpleValueColumnFetcher.getColumnForSimpleValue(manyToOne);
- if (c == null) {
- throw new MappingException("There is no column for property [" + property.getName() + "]");
- }
- if (!config.isUniqueWithinGroup()) {
- c.setUnique(config.isUnique());
- } else {
- if (property.isBidirectional()
- && property.getHibernateInverseSide() instanceof HibernateToOneProperty inverseSide
- && inverseSide.isHibernateOneToOne()) {
- c.setUnique(true);
- }
- }
+ manyToOne.setAlternateUniqueKey(true);
+ Column c = simpleValueColumnFetcher.getColumnForSimpleValue(manyToOne);
+ if (c == null) {
+ throw new MappingException("There is no column for property [" + property.getName() + "]");
+ }
+ if (!config.isUniqueWithinGroup()) {
+ c.setUnique(config.isUnique());
+ } else if (property.isBidirectional()
+ && property.getHibernateInverseSide() instanceof HibernateToOneProperty inverseSide
+ && inverseSide.isHibernateOneToOne()) {
+ c.setUnique(true);
+ }
+ }
+
+ private void prepareCircularManyToMany(HibernateManyToManyProperty property, Mapping mapping) {
+ PropertyConfig pc = property.getMappedForm();
+ if (mapping != null && pc.getColumns().isEmpty()) {
+ mapping.getColumns().put(property.getName(), pc);
+ }
+ if (!pc.hasJoinKeyMapping()) {
+ JoinTable jt = new JoinTable();
+ ColumnConfig columnConfig = new ColumnConfig();
+ columnConfig.setName(
+ namingStrategy.resolveColumnName(property.getName()) + FOREIGN_KEY_SUFFIX);
+ jt.setKey(columnConfig);
+ pc.setJoinTable(jt);
}
- return manyToOne;
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToManyProperty.java
index 947ce10fd7..d5d57d6e54 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToManyProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToManyProperty.java
@@ -36,4 +36,9 @@ public class HibernateManyToManyProperty extends ManyToManyWithMapping<PropertyC
public GrailsHibernatePersistentEntity getHibernateAssociatedEntity() {
return (GrailsHibernatePersistentEntity) super.getAssociatedEntity();
}
+
+ @Override
+ public HibernateManyToManyProperty getHibernateInverseSide() {
+ return (HibernateManyToManyProperty) getInverseSide();
+ }
}
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 2d0f524947..cb3ce556f7 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
@@ -33,6 +33,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder;
import org.hibernate.MappingException;
@@ -93,16 +94,41 @@ public class CollectionSecondPassBinder {
@Nonnull Collection collection) {
PersistentClass associatedClass = bindOrderBy(property, collection, persistentClasses);
+ bindOneToManyAssociation(property, associatedClass, collection);
- if (collection.isOneToMany()) {
- OneToMany oneToMany = (OneToMany) collection.getElement();
- oneToMany.setAssociatedClass(associatedClass);
- if (property.shouldBindWithForeignKey()) {
- collection.setCollectionTable(associatedClass.getTable());
- }
- collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
+ applyMultiTenantFilter(property, collection);
+
+ if (property.isSorted()) {
+ collection.setSorted(true);
+ }
+
+ DependantValue key = primaryKeyValueCreator.createPrimaryKeyValue(collection);
+ collection.setKey(key);
+ bindCollectionKey(property, key, associatedClass, collection);
+
+ Optional.ofNullable(property.getMappedForm().getCache())
+ .ifPresent(cacheConfig -> collection.setCacheConcurrencyStrategy(cacheConfig.getUsage()));
+
+ bindCollectionElement(property, mappings, collection);
+ collectionKeyColumnUpdater.forceNullableAndCheckUpdatable(key, property);
+ }
+
+ private void bindOneToManyAssociation(
+ HibernateToManyProperty property,
+ PersistentClass associatedClass,
+ Collection collection) {
+ if (!collection.isOneToMany()) {
+ return;
+ }
+ OneToMany oneToMany = (OneToMany) collection.getElement();
+ oneToMany.setAssociatedClass(associatedClass);
+ if (property.shouldBindWithForeignKey()) {
+ collection.setCollectionTable(associatedClass.getTable());
}
+ collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
+ }
+ private void applyMultiTenantFilter(HibernateToManyProperty property, Collection collection) {
Optional.ofNullable(property.getHibernateAssociatedEntity())
.filter(
referenced ->
@@ -126,21 +152,19 @@ public class CollectionSecondPassBinder {
Collections.emptyMap());
}
});
+ }
- if (property.isSorted()) {
- collection.setSorted(true);
- }
-
- DependantValue key = primaryKeyValueCreator.createPrimaryKeyValue(collection);
- collection.setKey(key);
-
+ private void bindCollectionKey(
+ HibernateToManyProperty property,
+ DependantValue key,
+ PersistentClass associatedClass,
+ Collection collection) {
if (property.isBidirectional()) {
- if (property.getHibernateInverseSide()
- instanceof org.grails.datastore.mapping.model.types.ToOne
+ var inverseSide = property.getHibernateInverseSide();
+ if (inverseSide instanceof org.grails.datastore.mapping.model.types.ToOne
&& property.shouldBindWithForeignKey()) {
- bidirectionalOneToManyLinker.link(
- collection, associatedClass, key, property.getHibernateInverseSide());
- } else if (property.getHibernateInverseSide() instanceof HibernateManyToManyProperty
+ bidirectionalOneToManyLinker.link(collection, associatedClass, key, inverseSide);
+ } else if (inverseSide instanceof HibernateManyToManyProperty
|| java.util.Map.class.isAssignableFrom(property.getType())) {
dependentKeyValueBinder.bind(property, key);
}
@@ -152,29 +176,44 @@ public class CollectionSecondPassBinder {
dependentKeyValueBinder.bind(property, key);
}
}
+ }
- Optional.ofNullable(property.getMappedForm().getCache())
- .ifPresent(cacheConfig -> collection.setCacheConcurrencyStrategy(cacheConfig.getUsage()));
-
- if (property instanceof HibernateManyToManyProperty || property.isBidirectionalOneToManyMap()) {
- if (property.isBidirectional()) {
- var otherSide = property.getHibernateInverseSide();
- ManyToOne element =
- manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
- element.setReferencedEntityName(otherSide.getOwner().getName());
- collection.setElement(element);
- collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
- if (property.isCircular()) {
- collection.setInverse(false);
- }
- }
+ private void bindCollectionElement(
+ HibernateToManyProperty property,
+ InFlightMetadataCollector mappings,
+ Collection collection) {
+ if (property instanceof HibernateManyToManyProperty manyToMany && manyToMany.isBidirectional()) {
+ bindManyToManyElement(manyToMany, collection);
+ } else if (property.isBidirectionalOneToManyMap() && property.isBidirectional()) {
+ bindBidirectionalMapElement(property, collection);
} else if (property.isUnidirectionalOneToMany()) {
- unidirectionalOneToManyBinder.bind(
- (HibernateOneToManyProperty) property, collection);
+ unidirectionalOneToManyBinder.bind((HibernateOneToManyProperty) property, collection);
} else if (property.supportsJoinColumnMapping()) {
collectionWithJoinTableBinder.bindCollectionWithJoinTable(property, mappings, collection);
}
- collectionKeyColumnUpdater.forceNullableAndCheckUpdatable(key, property);
+ }
+
+ private void bindManyToManyElement(
+ HibernateManyToManyProperty manyToMany, Collection collection) {
+ HibernateManyToManyProperty otherSide = manyToMany.getHibernateInverseSide();
+ ManyToOne element =
+ manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
+ element.setReferencedEntityName(otherSide.getOwner().getName());
+ collection.setElement(element);
+ collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, manyToMany);
+ if (manyToMany.isCircular()) {
+ collection.setInverse(false);
+ }
+ }
+
+ private void bindBidirectionalMapElement(
+ HibernateToManyProperty property, Collection collection) {
+ HibernateToOneProperty otherSide = (HibernateToOneProperty) property.getHibernateInverseSide();
+ ManyToOne element =
+ manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
+ element.setReferencedEntityName(otherSide.getOwner().getName());
+ collection.setElement(element);
+ collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
}
PersistentClass bindOrderBy(
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 7b54f81ad3..789054f2e7 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
@@ -1,7 +1,6 @@
package org.grails.orm.hibernate.cfg.domainbinding
import grails.gorm.specs.HibernateGormDatastoreSpec
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty
import org.grails.orm.hibernate.cfg.CompositeIdentity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
@@ -49,11 +48,11 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
mapping.setIdentity(hasCompositeId ? new CompositeIdentity() : null)
when:
- def result = binder.bindManyToOne(association as HibernateAssociation, null, path)
+ def result = binder.bindManyToOne(association, null, path)
then:
result instanceof ManyToOne
- 1 * manyToOneValuesBinder.bindManyToOneValues(association as HibernateAssociation, _ as ManyToOne)
+ 1 * manyToOneValuesBinder.bindManyToOneValues(association, _ as ManyToOne)
compositeBinderCalls * compositeBinder.bindCompositeIdentifierToManyToOne(association as HibernatePersistentProperty, _ as ManyToOne, _, refDomainClass, path)
simpleValueBinderCalls * simpleValueBinder.bindSimpleValue(association as HibernatePersistentProperty, null, _ as ManyToOne, path)
@@ -89,11 +88,11 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
namingStrategy.resolveColumnName("myCircularProp") >> "my_circular_prop"
when:
- def result = binder.bindManyToOne(property as HibernateAssociation, null, "/test")
+ def result = binder.bindManyToOne(property, null, "/test")
then:
result instanceof ManyToOne
- 1 * manyToOneValuesBinder.bindManyToOneValues(property as HibernateAssociation, _ as ManyToOne)
+ 1 * manyToOneValuesBinder.bindManyToOneValues(property, _ as ManyToOne)
1 * simpleValueBinder.bindSimpleValue(property as HibernatePersistentProperty, null, _ as ManyToOne, "/test")
def resultConfig = mapping.getColumns().get("myCircularProp")
resultConfig != null
@@ -132,7 +131,7 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
inverseSide.isHibernateOneToOne() >> isInverseHasOne
when:
- def result = binder.bindManyToOne(property as HibernateAssociation, null, "/test")
+ def result = binder.bindManyToOne(property, null, "/test")
then:
result.isAlternateUniqueKey()
@@ -174,7 +173,7 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
columnFetcher.getColumnForSimpleValue(_ as ManyToOne) >> null
when:
- binder.bindManyToOne(property as HibernateAssociation, null, "/test")
+ binder.bindManyToOne(property, null, "/test")
then:
thrown(MappingException)
(grails-core) 19/24: refactor: move DependantValue creation into CollectionKeyBinder
Posted by bo...@apache.org.
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 1b0d7a5b38327206ff0fbe0901bd1f1fceb5a733
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 12:22:12 2026 -0600
refactor: move DependantValue creation into CollectionKeyBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 4 +-
.../secondpass/CollectionKeyBinder.java | 13 ++++--
.../secondpass/CollectionKeyColumnUpdater.java | 2 +-
.../secondpass/CollectionSecondPassBinder.java | 11 +----
.../secondpass/CollectionKeyBinderSpec.groovy | 53 ++++++++++++----------
.../CollectionSecondPassBinderSpec.groovy | 2 +-
6 files changed, 45 insertions(+), 40 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 2a9fa23bcc..7749b30556 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -110,7 +110,6 @@ public class CollectionBinder {
new ColumnConfigToColumnBinder());
this.collectionSecondPassBinder =
new CollectionSecondPassBinder(
- new PrimaryKeyValueCreator(metadataBuildingContext),
new CollectionKeyColumnUpdater(),
new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
collectionWithJoinTableBinder,
@@ -118,7 +117,8 @@ public class CollectionBinder {
new CollectionKeyBinder(
new BidirectionalOneToManyLinker(grailsPropertyResolver),
new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
- simpleValueColumnBinder),
+ simpleValueColumnBinder,
+ new PrimaryKeyValueCreator(metadataBuildingContext)),
new BidirectionalMapElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new ManyToManyElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new CollectionOrderByBinder(),
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java
index 49d74fa661..36afedd005 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinder.java
@@ -33,23 +33,27 @@ public class CollectionKeyBinder {
private final BidirectionalOneToManyLinker bidirectionalOneToManyLinker;
private final DependentKeyValueBinder dependentKeyValueBinder;
private final SimpleValueColumnBinder simpleValueColumnBinder;
+ private final PrimaryKeyValueCreator primaryKeyValueCreator;
/** Creates a new {@link CollectionKeyBinder} instance. */
public CollectionKeyBinder(
BidirectionalOneToManyLinker bidirectionalOneToManyLinker,
DependentKeyValueBinder dependentKeyValueBinder,
- SimpleValueColumnBinder simpleValueColumnBinder) {
+ SimpleValueColumnBinder simpleValueColumnBinder,
+ PrimaryKeyValueCreator primaryKeyValueCreator) {
this.bidirectionalOneToManyLinker = bidirectionalOneToManyLinker;
this.dependentKeyValueBinder = dependentKeyValueBinder;
this.simpleValueColumnBinder = simpleValueColumnBinder;
+ this.primaryKeyValueCreator = primaryKeyValueCreator;
}
- /** Binds the collection key for the given property and collection. */
- public void bind(
+ /** Creates the {@link DependantValue} key, sets it on the collection, and binds it. */
+ public DependantValue bind(
HibernateToManyProperty property,
- DependantValue key,
PersistentClass associatedClass,
Collection collection) {
+ DependantValue key = primaryKeyValueCreator.createPrimaryKeyValue(collection);
+ collection.setKey(key);
if (property.isBidirectional()) {
var inverseSide = property.getHibernateInverseSide();
if (inverseSide instanceof ToOne && property.shouldBindWithForeignKey()) {
@@ -66,5 +70,6 @@ public class CollectionKeyBinder {
dependentKeyValueBinder.bind(property, key);
}
}
+ return key;
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
index 3f8464438d..0d9f2f55e3 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
@@ -28,7 +28,7 @@ public class CollectionKeyColumnUpdater {
/** Force nullable and check updatable. */
public void forceNullableAndCheckUpdatable(DependantValue key, HibernateToManyProperty property) {
- StreamSupport.stream(key.getColumns().spliterator(), false)
+ key.getColumns().stream()
.filter(Objects::nonNull)
.forEach(column -> column.setNullable(true));
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 501f49d55b..6ee0f04118 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
@@ -38,7 +38,6 @@ public class CollectionSecondPassBinder {
private final CollectionKeyBinder collectionKeyBinder;
private final BidirectionalMapElementBinder bidirectionalMapElementBinder;
private final ManyToManyElementBinder manyToManyElementBinder;
- private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
private final UnidirectionalOneToManyBinder unidirectionalOneToManyBinder;
private final CollectionWithJoinTableBinder collectionWithJoinTableBinder;
@@ -46,7 +45,6 @@ public class CollectionSecondPassBinder {
/** Creates a new {@link CollectionSecondPassBinder} instance. */
public CollectionSecondPassBinder(
- PrimaryKeyValueCreator primaryKeyValueCreator,
CollectionKeyColumnUpdater collectionKeyColumnUpdater,
UnidirectionalOneToManyBinder unidirectionalOneToManyBinder,
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
@@ -56,7 +54,6 @@ public class CollectionSecondPassBinder {
ManyToManyElementBinder manyToManyElementBinder,
CollectionOrderByBinder collectionOrderByBinder,
CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder) {
- this.primaryKeyValueCreator = primaryKeyValueCreator;
this.collectionKeyColumnUpdater = collectionKeyColumnUpdater;
this.unidirectionalOneToManyBinder = unidirectionalOneToManyBinder;
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
@@ -80,13 +77,9 @@ public class CollectionSecondPassBinder {
bindOneToManyAssociation(property, associatedClass, collection);
collectionMultiTenantFilterBinder.bind(property, collection);
- if (property.isSorted()) {
- collection.setSorted(true);
- }
+ collection.setSorted(property.isSorted());
- DependantValue key = primaryKeyValueCreator.createPrimaryKeyValue(collection);
- collection.setKey(key);
- collectionKeyBinder.bind(property, key, associatedClass, collection);
+ DependantValue key = collectionKeyBinder.bind(property, associatedClass, collection);
Optional.ofNullable(property.getMappedForm().getCache())
.ifPresent(cacheConfig -> collection.setCacheConcurrencyStrategy(cacheConfig.getUsage()));
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy
index 12d2881e03..9b4332cb2f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyBinderSpec.groovy
@@ -3,7 +3,6 @@ package org.grails.orm.hibernate.cfg.domainbinding.secondpass
import grails.gorm.annotation.Entity
import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
-import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
@@ -12,7 +11,6 @@ import org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver
import org.hibernate.mapping.Bag
import org.hibernate.mapping.BasicValue
import org.hibernate.mapping.Column
-import org.hibernate.mapping.DependantValue
import org.hibernate.mapping.Property
import org.hibernate.mapping.RootClass
import org.hibernate.mapping.Table
@@ -46,7 +44,8 @@ class CollectionKeyBinderSpec extends HibernateGormDatastoreSpec {
def botml = new BidirectionalOneToManyLinker(new GrailsPropertyResolver())
def dkvb = new DependentKeyValueBinder(svb, citmto)
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionKeyBinder(botml, dkvb, svcb)
+ def pkvc = new PrimaryKeyValueCreator(mbc)
+ binder = new CollectionKeyBinder(botml, dkvb, svcb, pkvc)
}
private HibernateToManyProperty propertyFor(Class ownerClass, String name = "items") {
@@ -68,71 +67,79 @@ class CollectionKeyBinderSpec extends HibernateGormDatastoreSpec {
return rootClass
}
- private DependantValue keyWithTable(String tableName = "test_table") {
+ private RootClass ownerRootClass(String tableName) {
def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def rootClass = new RootClass(mbc)
def table = new Table("test", tableName)
- def wrapped = new BasicValue(mbc, table)
- return new DependantValue(mbc, table, wrapped)
+ rootClass.setTable(table)
+ def idValue = new BasicValue(mbc, table)
+ idValue.setTypeName("long")
+ idValue.addColumn(new Column("id"))
+ rootClass.setIdentifier(idValue)
+ return rootClass
+ }
+
+ private Bag bagWithOwner(RootClass owner, String collectionTableName) {
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def bag = new Bag(mbc, owner)
+ bag.setCollectionTable(new Table("test", collectionTableName))
+ return bag
}
def "bind sets collection inverse for bidirectional one-to-many with foreign key"() {
given:
def property = propertyFor(CKBBidOwner)
def associatedClass = rootClassWith(CKBBidItem.name, "owner", "OWNER_ID")
- def key = keyWithTable("ckb_bid_item")
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def collection = bagWithOwner(ownerRootClass("ckb_bid_owner"), "ckb_bid_item")
when:
- binder.bind(property, key, associatedClass, collection)
+ binder.bind(property, associatedClass, collection)
then:
collection.isInverse()
- key.getColumnSpan() > 0
+ collection.getKey().getColumnSpan() > 0
}
def "bind delegates to dependentKeyValueBinder for bidirectional many-to-many"() {
given:
def property = propertyFor(CKBManyToManyOwner)
- def key = new DependantValue(getGrailsDomainBinder().getMetadataBuildingContext(), null, null)
def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def collection = bagWithOwner(ownerRootClass("ckb_mtm_owner"), "ckb_mtm_join")
when:
- binder.bind(property, key, associatedClass, collection)
+ binder.bind(property, associatedClass, collection)
then:
- key.getColumnSpan() > 0
+ collection.getKey().getColumnSpan() > 0
!collection.isInverse()
}
def "bind uses simpleValueColumnBinder for unidirectional with join key mapping"() {
given:
def property = propertyFor(CKBJoinKeyOwner)
- def key = keyWithTable("ckb_join_key_owner_ckb_join_key_item")
def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def collection = bagWithOwner(ownerRootClass("ckb_join_key_owner"), "ckb_join_key_owner_ckb_join_key_item")
when:
- binder.bind(property, key, associatedClass, collection)
+ binder.bind(property, associatedClass, collection)
then:
- key.getTypeName() == "long"
- key.getColumnSpan() > 0
+ collection.getKey().getTypeName() == "long"
+ collection.getKey().getColumnSpan() > 0
!collection.isInverse()
}
def "bind delegates to dependentKeyValueBinder for unidirectional without join key mapping"() {
given:
def property = propertyFor(CKBUniOwner)
- def key = new DependantValue(getGrailsDomainBinder().getMetadataBuildingContext(), null, null)
def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def collection = bagWithOwner(ownerRootClass("ckb_uni_owner"), "ckb_uni_owner_ckb_uni_item")
when:
- binder.bind(property, key, associatedClass, collection)
+ binder.bind(property, associatedClass, collection)
then:
- key.getColumnSpan() > 0
+ collection.getKey().getColumnSpan() > 0
!collection.isInverse()
}
}
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 4aa8190304..46a18eba1c 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
@@ -41,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new BidirectionalMapElementBinder(mtob, cfpcb), new ManyToManyElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
+ binder = new CollectionSecondPassBinder(cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb, pkvc), new BidirectionalMapElementBinder(mtob, cfpcb), new ManyToManyElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
(grails-core) 11/24: test OrderByClauseBuilder
Posted by bo...@apache.org.
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 4b7a1796415dfecec49c2f326e7694d7b3904f26
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 08:33:01 2026 -0600
test OrderByClauseBuilder
---
.../secondpass/CollectionSecondPassBinder.java | 4 +-
.../domainbinding/OrderByClauseBuilderSpec.groovy | 230 ++++++++++-----------
2 files changed, 113 insertions(+), 121 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 9b54553ca5..6b705fa4d8 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
@@ -186,8 +186,8 @@ public class CollectionSecondPassBinder {
bindManyToManyElement(manyToMany, collection);
} else if (property.isBidirectionalOneToManyMap() && property.isBidirectional()) {
bindBidirectionalMapElement(property, collection);
- } else if (property.isUnidirectionalOneToMany()) {
- unidirectionalOneToManyBinder.bind((HibernateOneToManyProperty) property, collection);
+ } else if (property instanceof HibernateOneToManyProperty oneToManyProperty && oneToManyProperty.isUnidirectionalOneToMany()) {
+ unidirectionalOneToManyBinder.bind(oneToManyProperty, collection);
} else if (property.supportsJoinColumnMapping()) {
collectionWithJoinTableBinder.bindCollectionWithJoinTable(property, mappings, collection);
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OrderByClauseBuilderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OrderByClauseBuilderSpec.groovy
index 603681fe1c..6b27f7746b 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OrderByClauseBuilderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OrderByClauseBuilderSpec.groovy
@@ -1,10 +1,17 @@
package org.grails.orm.hibernate.cfg.domainbinding
-import grails.gorm.annotation.Entity
import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.datastore.mapping.model.DatastoreConfigurationException
-import org.hibernate.mapping.PersistentClass
+import org.hibernate.mapping.BasicValue
+import org.hibernate.mapping.Column
+import org.hibernate.mapping.Component
+import org.hibernate.mapping.JoinedSubclass
+import org.hibernate.mapping.Property
+import org.hibernate.mapping.RootClass
+import org.hibernate.mapping.SingleTableSubclass
+import org.hibernate.mapping.Table
import spock.lang.Subject
+import spock.lang.Unroll
import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder
@@ -13,159 +20,144 @@ class OrderByClauseBuilderSpec extends HibernateGormDatastoreSpec {
@Subject
OrderByClauseBuilder builder = new OrderByClauseBuilder()
- void setupSpec() {
- manager.addAllDomainClasses([OrderTest, SubOrderTest, OrderWithComponent])
+ private RootClass entityClass
+ private RootClass componentEntityClass
+
+ def setup() {
+ def ctx = getGrailsDomainBinder().getMetadataBuildingContext()
+ def table = new Table("test", "order_entity")
+
+ entityClass = new RootClass(ctx)
+ entityClass.setEntityName("OrderEntity")
+ entityClass.setTable(table)
+ entityClass.setIdentifier(basicValue(ctx, table, "id"))
+ entityClass.addProperty(simpleProperty(ctx, table, "name", "name"))
+ entityClass.addProperty(simpleProperty(ctx, table, "age", "age"))
+ entityClass.addProperty(simpleProperty(ctx, table, "other", "other_column"))
+
+ def compTable = new Table("test", "comp_entity")
+ componentEntityClass = new RootClass(ctx)
+ componentEntityClass.setEntityName("CompEntity")
+ componentEntityClass.setTable(compTable)
+ componentEntityClass.setIdentifier(basicValue(ctx, compTable, "id"))
+
+ def comp = new Component(ctx, compTable, componentEntityClass)
+ comp.addProperty(simpleProperty(ctx, compTable, "c1", "comp_c1"))
+ comp.addProperty(simpleProperty(ctx, compTable, "c2", "comp_c2"))
+ def compProp = new Property()
+ compProp.setName("comp")
+ compProp.setValue(comp)
+ componentEntityClass.addProperty(compProp)
}
- void "test buildOrderByClause with empty string (default to id)"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("", pc, "role", "asc")
-
- then:
- result == "id asc"
+ void "null hqlOrderBy returns null"() {
+ expect:
+ builder.buildOrderByClause(null, entityClass, "role", "asc") == null
}
- void "test buildOrderByClause with single property"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("name", pc, "role", "asc")
-
- then:
- result == "name asc"
+ void "empty hqlOrderBy returns identifier column with asc"() {
+ expect:
+ builder.buildOrderByClause("", entityClass, "role", "asc") == "id asc"
}
- void "test buildOrderByClause with property and explicit order"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("name desc", pc, "role", "asc")
-
- then:
- result == "name desc"
+ @Unroll
+ void "single property '#hql' with defaultOrder '#defaultOrder' returns '#expected'"() {
+ expect:
+ builder.buildOrderByClause(hql, entityClass, "role", defaultOrder) == expected
+
+ where:
+ hql | defaultOrder | expected
+ "name" | "asc" | "name asc"
+ "name" | "desc" | "name desc"
+ "name asc" | "desc" | "name asc"
+ "name desc" | "asc" | "name desc"
+ "name ASC" | "desc" | "name asc"
+ "name DESC" | "asc" | "name desc"
}
- void "test buildOrderByClause with multiple properties"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("name, age desc", pc, "role", "asc")
-
- then:
- result == "name asc, age desc"
+ void "custom column name is used in order clause"() {
+ expect:
+ builder.buildOrderByClause("other", entityClass, "role", "asc") == "other_column asc"
}
- void "test buildOrderByClause with mapped column name"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("other", pc, "role", "asc")
-
- then:
- result == "other_column asc"
+ void "multiple properties with mixed directions"() {
+ expect:
+ builder.buildOrderByClause("name, age desc", entityClass, "role", "asc") == "name asc, age desc"
}
- void "test buildOrderByClause with null string"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause(null, pc, "role", "asc")
-
- then:
- result == null
+ void "component property expands to all its columns"() {
+ expect:
+ builder.buildOrderByClause("comp", componentEntityClass, "role", "asc") == "comp_c1 asc, comp_c2 asc"
}
- void "test buildOrderByClause with non-existent property"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
+ void "non-existent property throws DatastoreConfigurationException"() {
when:
- builder.buildOrderByClause("foo", pc, "role", "asc")
+ builder.buildOrderByClause("nonExistent", entityClass, "role", "asc")
then:
- thrown(DatastoreConfigurationException)
+ def ex = thrown(DatastoreConfigurationException)
+ ex.message.contains("OrderEntity.nonExistent")
}
- void "test buildOrderByClause with invalid sort clause"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
+ void "double direction token throws DatastoreConfigurationException"() {
when:
- // Double ordering keyword should fail
- builder.buildOrderByClause("name asc desc", pc, "role", "asc")
+ builder.buildOrderByClause("name asc desc", entityClass, "role", "asc")
then:
thrown(DatastoreConfigurationException)
}
- void "test buildOrderByClause with different defaultOrder"() {
+ void "inherited property from parent in joined subclass receives table prefix"() {
given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderTest.name)
-
- when:
- String result = builder.buildOrderByClause("name", pc, "role", "desc")
-
- then:
- result == "name desc"
- }
+ def ctx = getGrailsDomainBinder().getMetadataBuildingContext()
+ def subTable = new Table("test", "sub_entity")
+ def sub = new JoinedSubclass(entityClass, ctx)
+ sub.setEntityName("SubEntity")
+ sub.setTable(subTable)
+ sub.addProperty(simpleProperty(ctx, subTable, "extra", "extra_col"))
- void "test buildOrderByClause with multiple columns"() {
- given:
- PersistentClass pc = datastore.metadata.getEntityBinding(OrderWithComponent.name)
+ expect: "property from root table gets no prefix when sorting on root class"
+ builder.buildOrderByClause("name", entityClass, "role", "asc") == "name asc"
- when:
- String result = builder.buildOrderByClause("comp", pc, "role", "asc")
+ and: "property from root table gets its table prefix when sorting on the subclass"
+ builder.buildOrderByClause("name", sub, "role", "asc") == "order_entity.name asc"
- then:
- result == "comp_c1 asc, comp_c2 asc"
+ and: "property from subclass table gets no prefix when sorting on the subclass"
+ builder.buildOrderByClause("extra", sub, "role", "asc") == "extra_col asc"
}
- void "test buildOrderByClause with table prefix for inherited property"() {
+ void "single-table subclass property is sorted without table prefix"() {
given:
- PersistentClass pc = datastore.metadata.getEntityBinding(SubOrderTest.name)
-
- when:
- // 'name' is in the base table 'order_test', 'subProperty' is in 'sub_order_test'
- String result = builder.buildOrderByClause("name, subProperty", pc, "role", "asc")
-
- then:
- // In GORM TPH is default, so no table prefix
- result == "name asc, sub_property asc"
+ def ctx = getGrailsDomainBinder().getMetadataBuildingContext()
+ entityClass.setClassName("org.grails.orm.hibernate.cfg.domainbinding.ParentEntity")
+ def sub = new SingleTableSubclass(entityClass, ctx)
+ sub.setEntityName("ChildEntity")
+ sub.setClassName("org.grails.orm.hibernate.cfg.domainbinding.ChildEntity")
+ sub.addProperty(simpleProperty(ctx, entityClass.getTable(), "childProp", "child_prop"))
+
+ expect: "parent property has no prefix on the subclass"
+ builder.buildOrderByClause("name", sub, "role", "asc") == "name asc"
+
+ and: "subclass-own property has no prefix"
+ builder.buildOrderByClause("childProp", sub, "role", "asc") == "child_prop asc"
}
-}
-@Entity
-class OrderTest {
- String name
- Integer age
- String other
+ // ---- helpers --------------------------------------------------------
- static mapping = {
- other column: 'other_column'
+ private static BasicValue basicValue(ctx, Table table, String columnName) {
+ def v = new BasicValue(ctx, table)
+ v.addColumn(new Column(columnName))
+ v
}
-}
-
-@Entity
-class SubOrderTest extends OrderTest {
- String subProperty
-}
-@Entity
-class OrderWithComponent {
- Long id
- TestComponent comp
- static embedded = ['comp']
+ private static Property simpleProperty(ctx, Table table, String name, String columnName) {
+ def prop = new Property()
+ prop.setName(name)
+ prop.setValue(basicValue(ctx, table, columnName))
+ prop
+ }
}
-class TestComponent {
- String c1
- String c2
-}
+// Minimal classes needed for mapped-class assignments in the STI test
+class ParentEntity {}
+class ChildEntity extends ParentEntity {}
(grails-core) 08/24: refactor: split ManyToOneBinder into ManyToOneBinder and ForeignKeyOneToOneBinder
Posted by bo...@apache.org.
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 1b205085814495ea6715dfcb55b53ad37352cd69
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 13:09:33 2026 -0600
refactor: split ManyToOneBinder into ManyToOneBinder and ForeignKeyOneToOneBinder
---
.../binder/ForeignKeyOneToOneBinder.java | 77 ++++++++++++
.../domainbinding/binder/GrailsDomainBinder.java | 8 +-
.../domainbinding/binder/GrailsPropertyBinder.java | 7 +-
.../cfg/domainbinding/binder/ManyToOneBinder.java | 46 +------
.../cfg/domainbinding/CollectionBinderSpec.groovy | 7 +-
.../ForeignKeyOneToOneBinderSpec.groovy | 134 +++++++++++++++++++++
.../domainbinding/GrailsPropertyBinderSpec.groovy | 11 +-
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 96 +--------------
.../CollectionSecondPassBinderSpec.groovy | 2 +-
.../secondpass/ListSecondPassBinderSpec.groovy | 7 +-
.../secondpass/MapSecondPassBinderSpec.groovy | 7 +-
11 files changed, 251 insertions(+), 151 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java
new file mode 100644
index 0000000000..ef45844a18
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java
@@ -0,0 +1,77 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.binder;
+
+import org.grails.orm.hibernate.cfg.PropertyConfig;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
+import org.hibernate.MappingException;
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.ManyToOne;
+
+/**
+ * Binds a {@link HibernateOneToOneProperty} whose foreign key resides on this side as a Hibernate
+ * {@link ManyToOne} value, and applies unique-key constraints as needed.
+ *
+ * <p>This handles the case where {@code isValidHibernateOneToOne()} is {@code false} — i.e. the
+ * association cannot be mapped as a Hibernate {@code OneToOne}, so it falls back to a ManyToOne
+ * column with an alternate unique key.
+ */
+public class ForeignKeyOneToOneBinder {
+
+ private final ManyToOneBinder manyToOneBinder;
+ private final SimpleValueColumnFetcher simpleValueColumnFetcher;
+
+ public ForeignKeyOneToOneBinder(
+ ManyToOneBinder manyToOneBinder, SimpleValueColumnFetcher simpleValueColumnFetcher) {
+ this.manyToOneBinder = manyToOneBinder;
+ this.simpleValueColumnFetcher = simpleValueColumnFetcher;
+ }
+
+ /**
+ * Binds the one-to-one property as a {@link ManyToOne} value and applies unique-key constraints.
+ */
+ public ManyToOne bind(
+ HibernateOneToOneProperty property, org.hibernate.mapping.Table table, String path) {
+ GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
+ boolean isComposite = ManyToOneBinder.isCompositeIdentifier(refDomainClass);
+ ManyToOne manyToOne =
+ manyToOneBinder.doBind(property, refDomainClass, isComposite, table, path);
+ if (!isComposite) {
+ bindUniqueKey(property, manyToOne);
+ }
+ return manyToOne;
+ }
+
+ private void bindUniqueKey(HibernateOneToOneProperty property, ManyToOne manyToOne) {
+ PropertyConfig config = property.getMappedForm();
+ manyToOne.setAlternateUniqueKey(true);
+ Column c = simpleValueColumnFetcher.getColumnForSimpleValue(manyToOne);
+ if (c == null) {
+ throw new MappingException("There is no column for property [" + property.getName() + "]");
+ }
+ if (!config.isUniqueWithinGroup()) {
+ c.setUnique(config.isUnique());
+ } else if (property.isBidirectional()
+ && property.getHibernateInverseSide().isValidHibernateOneToOne()) {
+ c.setUnique(true);
+ }
+ }
+}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
index c7ae65c34d..048022b202 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
@@ -142,8 +142,9 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
namingStrategy,
simpleValueBinder,
new ManyToOneValuesBinder(),
- compositeIdentifierToManyToOneBinder,
- simpleValueColumnFetcher);
+ compositeIdentifierToManyToOneBinder);
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder =
+ new ForeignKeyOneToOneBinder(manyToOneBinder, simpleValueColumnFetcher);
CollectionBinder collectionBinder =
new CollectionBinder(
@@ -167,7 +168,8 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
collectionBinder,
simpleValueBinder,
oneToOneBinder,
- manyToOneBinder);
+ manyToOneBinder,
+ foreignKeyOneToOneBinder);
componentBinder.setGrailsPropertyBinder(grailsPropertyBinder);
CompositeIdBinder compositeIdBinder =
new CompositeIdBinder(metadataBuildingContext, componentUpdater, grailsPropertyBinder);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index c970dd5a32..75ed9937ce 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -42,6 +42,7 @@ public class GrailsPropertyBinder {
private final SimpleValueBinder simpleValueBinder;
private final OneToOneBinder oneToOneBinder;
private final ManyToOneBinder manyToOneBinder;
+ private final ForeignKeyOneToOneBinder foreignKeyOneToOneBinder;
public GrailsPropertyBinder(
EnumTypeBinder enumTypeBinder,
@@ -49,13 +50,15 @@ public class GrailsPropertyBinder {
CollectionBinder collectionBinder,
SimpleValueBinder simpleValueBinder,
OneToOneBinder oneToOneBinder,
- ManyToOneBinder manyToOneBinder) {
+ ManyToOneBinder manyToOneBinder,
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder) {
this.enumTypeBinder = enumTypeBinder;
this.componentBinder = componentBinder;
this.collectionBinder = collectionBinder;
this.simpleValueBinder = simpleValueBinder;
this.oneToOneBinder = oneToOneBinder;
this.manyToOneBinder = manyToOneBinder;
+ this.foreignKeyOneToOneBinder = foreignKeyOneToOneBinder;
}
public Value bindProperty(
@@ -81,7 +84,7 @@ public class GrailsPropertyBinder {
&& oneToOne.isValidHibernateOneToOne()) {
value = oneToOneBinder.bindOneToOne(oneToOne, persistentClass, table, path);
} else if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne) {
- value = manyToOneBinder.bindManyToOne(oneToOne, table, path);
+ value = foreignKeyOneToOneBinder.bind(oneToOne, table, path);
} else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) {
value = manyToOneBinder.bindManyToOne(manyToOne, table, path);
} else if (currentGrailsProp instanceof HibernateToManyProperty toMany
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index 35e0af023a..7e256ff33f 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -30,12 +30,8 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersi
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
-import org.hibernate.MappingException;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
-import org.hibernate.mapping.Column;
import org.hibernate.mapping.ManyToOne;
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
@@ -46,21 +42,18 @@ public class ManyToOneBinder {
private final SimpleValueBinder simpleValueBinder;
private final ManyToOneValuesBinder manyToOneValuesBinder;
private final CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder;
- private final SimpleValueColumnFetcher simpleValueColumnFetcher;
public ManyToOneBinder(
MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
SimpleValueBinder simpleValueBinder,
ManyToOneValuesBinder manyToOneValuesBinder,
- CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder,
- SimpleValueColumnFetcher simpleValueColumnFetcher) {
+ CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.simpleValueBinder = simpleValueBinder;
this.manyToOneValuesBinder = manyToOneValuesBinder;
this.compositeIdentifierToManyToOneBinder = compositeIdentifierToManyToOneBinder;
- this.simpleValueColumnFetcher = simpleValueColumnFetcher;
}
public ManyToOneBinder(
@@ -73,8 +66,7 @@ public class ManyToOneBinder {
new SimpleValueBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment),
new ManyToOneValuesBinder(),
new CompositeIdentifierToManyToOneBinder(
- metadataBuildingContext, namingStrategy, jdbcEnvironment),
- new SimpleValueColumnFetcher());
+ metadataBuildingContext, namingStrategy, jdbcEnvironment));
}
/** Binds a many-to-one association. */
@@ -84,21 +76,6 @@ public class ManyToOneBinder {
return doBind(property, refDomainClass, isCompositeIdentifier(refDomainClass), table, path);
}
- /**
- * Binds a one-to-one association where the foreign key resides on the other side (not a true
- * Hibernate one-to-one), i.e. {@code isHibernateOneToOne()} is false.
- */
- public ManyToOne bindManyToOne(
- HibernateOneToOneProperty property, org.hibernate.mapping.Table table, String path) {
- GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
- boolean isComposite = isCompositeIdentifier(refDomainClass);
- ManyToOne manyToOne = doBind(property, refDomainClass, isComposite, table, path);
- if (!isComposite) {
- bindOneToOneUniqueKey(property, manyToOne);
- }
- return manyToOne;
- }
-
/** Binds the inverse side of a many-to-many association as a collection element. */
public ManyToOne bindManyToOne(
HibernateManyToManyProperty property, org.hibernate.mapping.Table table, String path) {
@@ -110,12 +87,12 @@ public class ManyToOneBinder {
return doBind(property, refDomainClass, isComposite, table, path);
}
- private static boolean isCompositeIdentifier(GrailsHibernatePersistentEntity entity) {
+ static boolean isCompositeIdentifier(GrailsHibernatePersistentEntity entity) {
Mapping mapping = entity.getMappedForm();
return mapping != null && mapping.hasCompositeIdentifier();
}
- private ManyToOne doBind(
+ ManyToOne doBind(
HibernateAssociation property,
GrailsHibernatePersistentEntity refDomainClass,
boolean isComposite,
@@ -134,21 +111,6 @@ public class ManyToOneBinder {
return manyToOne;
}
- private void bindOneToOneUniqueKey(HibernateOneToOneProperty property, ManyToOne manyToOne) {
- PropertyConfig config = property.getMappedForm();
- manyToOne.setAlternateUniqueKey(true);
- Column c = simpleValueColumnFetcher.getColumnForSimpleValue(manyToOne);
- if (c == null) {
- throw new MappingException("There is no column for property [" + property.getName() + "]");
- }
- if (!config.isUniqueWithinGroup()) {
- c.setUnique(config.isUnique());
- } else if (property.isBidirectional()
- && property.getHibernateInverseSide().isValidHibernateOneToOne()) {
- c.setUnique(true);
- }
- }
-
private void prepareCircularManyToMany(HibernateManyToManyProperty property, Mapping mapping) {
PropertyConfig pc = property.getMappedForm();
if (mapping != null && pc.getColumns().isEmpty()) {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
index 629c3e9f6f..945bf05dd5 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
@@ -10,6 +10,7 @@ import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ForeignKeyOneToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
@@ -71,7 +72,8 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
)
OneToOneBinder oneToOneBinder = new OneToOneBinder(metadataBuildingContext, simpleValueBinder)
- ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder, simpleValueColumnFetcher)
+ ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder)
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder = new ForeignKeyOneToOneBinder(manyToOneBinder, simpleValueColumnFetcher)
CollectionBinder collectionBinder = new CollectionBinder(
metadataBuildingContext,
@@ -104,7 +106,8 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
,
oneToOneBinder,
- manyToOneBinder
+ manyToOneBinder,
+ foreignKeyOneToOneBinder
)
CompositeIdBinder compositeIdBinder = new CompositeIdBinder(metadataBuildingContext, componentUpdater, propertyBinder)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy
new file mode 100644
index 0000000000..bfcadc3367
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy
@@ -0,0 +1,134 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding
+
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.datastore.mapping.model.MappingContext
+import org.grails.datastore.mapping.model.PersistentEntity
+import org.grails.orm.hibernate.cfg.Mapping
+import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
+import org.grails.orm.hibernate.cfg.PropertyConfig
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
+import org.hibernate.MappingException
+import org.hibernate.mapping.Column
+import org.hibernate.mapping.ManyToOne
+import spock.lang.Unroll
+
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ForeignKeyOneToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
+import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher
+
+class ForeignKeyOneToOneBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Unroll
+ def "bind sets alternate unique key and column uniqueness for #scenario"() {
+ given:
+ def namingStrategy = Mock(PersistentEntityNamingStrategy)
+ def simpleValueBinder = Mock(SimpleValueBinder)
+ def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
+ def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
+ def columnFetcher = Mock(SimpleValueColumnFetcher)
+
+ def manyToOneBinder = new ManyToOneBinder(
+ getGrailsDomainBinder().getMetadataBuildingContext(),
+ namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder)
+ def binder = new ForeignKeyOneToOneBinder(manyToOneBinder, columnFetcher)
+
+ def property = Mock(TestFKOneToOne)
+ def mapping = new Mapping()
+ def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
+ getMappedForm() >> mapping
+ }
+ def propertyConfig = Mock(PropertyConfig)
+ def column = new Column('test')
+ def inverseSide = Mock(TestFKOneToOne)
+
+ property.getHibernateAssociatedEntity() >> refDomainClass
+ mapping.setIdentity(null)
+ property.getMappedForm() >> propertyConfig
+ columnFetcher.getColumnForSimpleValue(_ as ManyToOne) >> column
+
+ propertyConfig.isUnique() >> isUnique
+ propertyConfig.isUniqueWithinGroup() >> isUniqueWithinGroup
+ property.isBidirectional() >> isBidirectional
+ property.getHibernateInverseSide() >> inverseSide
+ inverseSide.isValidHibernateOneToOne() >> isInverseHasOne
+
+ when:
+ def result = binder.bind(property, null, "/test")
+
+ then:
+ result.isAlternateUniqueKey()
+ if (expectedUniqueValue != null) {
+ assert column.isUnique() == expectedUniqueValue
+ } else {
+ assert !column.isUnique()
+ }
+
+ where:
+ scenario | isUnique | isUniqueWithinGroup | isBidirectional | isInverseHasOne | expectedUniqueValue
+ "simple unique=true" | true | false | false | false | true
+ "simple unique=false" | false | false | false | false | false
+ "uniqueWithinGroup and bidirectional" | false | true | true | true | true
+ "uniqueWithinGroup and unidirectional" | false | true | false | false | null
+ "uniqueWithinGroup and not hasOne" | false | true | true | false | null
+ }
+
+ def "bind throws MappingException when column is not found"() {
+ given:
+ def namingStrategy = Mock(PersistentEntityNamingStrategy)
+ def simpleValueBinder = Mock(SimpleValueBinder)
+ def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
+ def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
+ def columnFetcher = Mock(SimpleValueColumnFetcher)
+
+ def manyToOneBinder = new ManyToOneBinder(
+ getGrailsDomainBinder().getMetadataBuildingContext(),
+ namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder)
+ def binder = new ForeignKeyOneToOneBinder(manyToOneBinder, columnFetcher)
+
+ def property = Mock(TestFKOneToOne)
+ def mapping = new Mapping()
+ def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
+ getMappedForm() >> mapping
+ }
+ def propertyConfig = new PropertyConfig()
+
+ property.getHibernateAssociatedEntity() >> refDomainClass
+ mapping.setIdentity(null)
+ property.getMappedForm() >> propertyConfig
+ columnFetcher.getColumnForSimpleValue(_ as ManyToOne) >> null
+
+ when:
+ binder.bind(property, null, "/test")
+
+ then:
+ thrown(MappingException)
+ }
+}
+
+abstract class TestFKOneToOne extends HibernateOneToOneProperty {
+ TestFKOneToOne(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) {
+ super(owner, context, descriptor)
+ }
+}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
index 4c01fa8d17..563d7d768b 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
@@ -26,6 +26,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.ComponentBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ComponentUpdater
import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsPropertyBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ForeignKeyOneToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.OneToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
@@ -136,7 +137,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
)
OneToOneBinder oneToOneBinder = new OneToOneBinder(metadataBuildingContext, simpleValueBinder)
- ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder, simpleValueColumnFetcher)
+ ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder)
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder = new ForeignKeyOneToOneBinder(manyToOneBinder, simpleValueColumnFetcher)
CollectionBinder collectionBinder = new CollectionBinder(
metadataBuildingContext,
@@ -168,7 +170,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
,
oneToOneBinder,
- manyToOneBinder
+ manyToOneBinder,
+ foreignKeyOneToOneBinder
)
componentBinder.setGrailsPropertyBinder(propertyBinder)
@@ -507,6 +510,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
def columnNameForPropertyAndPathFetcher = Mock(ColumnNameForPropertyAndPathFetcher)
def oneToOneBinder = Mock(OneToOneBinder)
def manyToOneBinder = Mock(ManyToOneBinder)
+ def foreignKeyOneToOneBinder = Mock(ForeignKeyOneToOneBinder)
// Instantiate GrailsPropertyBinder using the protected constructor with necessary mocks
def propertyBinder = new GrailsPropertyBinder(
@@ -518,7 +522,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
,
oneToOneBinder,
- manyToOneBinder
+ manyToOneBinder,
+ foreignKeyOneToOneBinder
)
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 e358200774..15a7bbae11 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
@@ -1,7 +1,6 @@
package org.grails.orm.hibernate.cfg.domainbinding
import grails.gorm.specs.HibernateGormDatastoreSpec
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty
import org.grails.orm.hibernate.cfg.CompositeIdentity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
@@ -10,9 +9,6 @@ import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.grails.orm.hibernate.cfg.PropertyConfig
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
-import org.hibernate.MappingException
-import org.hibernate.mapping.Column
import org.hibernate.mapping.ManyToOne
import spock.lang.Unroll
@@ -31,9 +27,8 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
def simpleValueBinder = Mock(SimpleValueBinder)
def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
- def columnFetcher = Mock(SimpleValueColumnFetcher)
- def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder, columnFetcher)
+ def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder)
def association = Mock(HibernateManyToOneProperty)
def path = "/test"
@@ -68,9 +63,8 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
def simpleValueBinder = Mock(SimpleValueBinder)
def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
- def columnFetcher = Mock(SimpleValueColumnFetcher)
- def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder, columnFetcher)
+ def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder)
def property = Mock(HibernateManyToManyProperty)
def mapping = new Mapping()
@@ -98,90 +92,4 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
resultConfig != null
resultConfig.getJoinTable().getKey().getName() == "my_circular_prop_id"
}
-
- @Unroll
- def "Test one-to-one binding with uniqueWithinGroup constraint for #scenario"() {
- given:
- def namingStrategy = Mock(PersistentEntityNamingStrategy)
- def simpleValueBinder = Mock(SimpleValueBinder)
- def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
- def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
- def columnFetcher = Mock(SimpleValueColumnFetcher)
-
- def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder, columnFetcher)
-
- def property = Mock(HibernateOneToOneProperty)
- def mapping = new Mapping()
- def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
- getMappedForm() >> mapping
- }
- def propertyConfig = Mock(PropertyConfig)
- def column = new Column('test')
- def inverseSide = Mock(TestOneToOneInverse)
-
- property.getHibernateAssociatedEntity() >> refDomainClass
- mapping.setIdentity(null)
- property.getMappedForm() >> propertyConfig
- columnFetcher.getColumnForSimpleValue(_ as ManyToOne) >> column
-
- propertyConfig.isUnique() >> isUnique
- propertyConfig.isUniqueWithinGroup() >> isUniqueWithinGroup
- property.isBidirectional() >> isBidirectional
- property.getHibernateInverseSide() >> inverseSide
- inverseSide.isValidHibernateOneToOne() >> isInverseHasOne
-
- when:
- def result = binder.bindManyToOne(property, null, "/test")
-
- then:
- result.isAlternateUniqueKey()
- if (expectedUniqueValue != null) {
- assert column.isUnique() == expectedUniqueValue
- } else {
- assert !column.isUnique()
- }
-
- where:
- scenario | isUnique | isUniqueWithinGroup | isBidirectional | isInverseHasOne | expectedUniqueValue
- "simple unique=true" | true | false | false | false | true
- "simple unique=false" | false | false | false | false | false
- "uniqueWithinGroup and bidirectional" | false | true | true | true | true
- "uniqueWithinGroup and unidirectional" | false | true | false | false | null
- "uniqueWithinGroup and not hasOne" | false | true | true | false | null
- }
-
- def "Test one-to-one binding throws exception when column is not found"() {
- given:
- def namingStrategy = Mock(PersistentEntityNamingStrategy)
- def simpleValueBinder = Mock(SimpleValueBinder)
- def manyToOneValuesBinder = Mock(ManyToOneValuesBinder)
- def compositeBinder = Mock(CompositeIdentifierToManyToOneBinder)
- def columnFetcher = Mock(SimpleValueColumnFetcher)
-
- def binder = new ManyToOneBinder(getGrailsDomainBinder().getMetadataBuildingContext(), namingStrategy, simpleValueBinder, manyToOneValuesBinder, compositeBinder, columnFetcher)
-
- def property = Mock(HibernateOneToOneProperty)
- def mapping = new Mapping()
- def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
- getMappedForm() >> mapping
- }
- def propertyConfig = new PropertyConfig()
-
- property.getHibernateAssociatedEntity() >> refDomainClass
- mapping.setIdentity(null)
- property.getMappedForm() >> propertyConfig
- columnFetcher.getColumnForSimpleValue(_ as ManyToOne) >> null
-
- when:
- binder.bindManyToOne(property, null, "/test")
-
- then:
- thrown(MappingException)
- }
-}
-
-abstract class TestOneToOneInverse extends HibernateOneToOneProperty {
- TestOneToOneInverse(org.grails.datastore.mapping.model.PersistentEntity owner, org.grails.datastore.mapping.model.MappingContext context, java.beans.PropertyDescriptor descriptor) {
- super(owner, context, descriptor)
- }
}
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 38eb6c4761..76c011d992 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
@@ -32,7 +32,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def svb = new SimpleValueBinder(mbc, ns, je)
def svcf = new SimpleValueColumnFetcher()
def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
- def mtob = new ManyToOneBinder(mbc, ns, svb, new ManyToOneValuesBinder(), citmto, svcf)
+ def mtob = new ManyToOneBinder(mbc, ns, svb, new ManyToOneValuesBinder(), citmto)
def pkvc = new PrimaryKeyValueCreator(mbc)
def cku = new CollectionKeyColumnUpdater()
def botml = new BidirectionalOneToManyLinker(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver())
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
index 5266866570..19a63afb81 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
@@ -10,6 +10,7 @@ import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ForeignKeyOneToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
@@ -70,7 +71,8 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
)
OneToOneBinder oneToOneBinder = new OneToOneBinder(metadataBuildingContext, simpleValueBinder)
- ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder, simpleValueColumnFetcher)
+ ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder)
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder = new ForeignKeyOneToOneBinder(manyToOneBinder, simpleValueColumnFetcher)
CollectionBinder collectionBinder = new CollectionBinder(
metadataBuildingContext,
@@ -103,7 +105,8 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
,
oneToOneBinder,
- manyToOneBinder
+ manyToOneBinder,
+ foreignKeyOneToOneBinder
)
CompositeIdBinder compositeIdBinder = new CompositeIdBinder(metadataBuildingContext, componentUpdater, propertyBinder)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
index 9b1c4d1b53..cdff3a07c2 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
@@ -10,6 +10,7 @@ import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ForeignKeyOneToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
@@ -70,7 +71,8 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
)
OneToOneBinder oneToOneBinder = new OneToOneBinder(metadataBuildingContext, simpleValueBinder)
- ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder, simpleValueColumnFetcher)
+ ManyToOneBinder manyToOneBinder = new ManyToOneBinder(metadataBuildingContext, namingStrategy, simpleValueBinder, new ManyToOneValuesBinder(), compositeIdentifierToManyToOneBinder)
+ ForeignKeyOneToOneBinder foreignKeyOneToOneBinder = new ForeignKeyOneToOneBinder(manyToOneBinder, simpleValueColumnFetcher)
CollectionBinder collectionBinder = new CollectionBinder(
metadataBuildingContext,
@@ -103,7 +105,8 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
simpleValueBinder
,
oneToOneBinder,
- manyToOneBinder
+ manyToOneBinder,
+ foreignKeyOneToOneBinder
)
CompositeIdBinder compositeIdBinder = new CompositeIdBinder(metadataBuildingContext, componentUpdater, propertyBinder)
(grails-core) 14/24: Extract bindOrderBy into CollectionOrderByBinder
Posted by bo...@apache.org.
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 f2427ba3fae0e0b0b1771337e1eef35800899607
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 09:40:33 2026 -0600
Extract bindOrderBy into CollectionOrderByBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 4 +-
.../secondpass/CollectionOrderByBinder.java | 73 ++++++++++
.../secondpass/CollectionSecondPassBinder.java | 48 +-----
.../secondpass/CollectionOrderByBinderSpec.groovy | 161 +++++++++++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 97 +------------
5 files changed, 246 insertions(+), 137 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 319d9b12c0..93648444f9 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -30,6 +30,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToManyLinker;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionOrderByBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionSecondPassBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionWithJoinTableBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.DependentKeyValueBinder;
@@ -114,7 +115,8 @@ public class CollectionBinder {
collectionWithJoinTableBinder,
collectionForPropertyConfigBinder,
new DefaultColumnNameFetcher(namingStrategy),
- simpleValueColumnBinder);
+ simpleValueColumnBinder,
+ new CollectionOrderByBinder());
this.listSecondPassBinder =
new ListSecondPassBinder(
metadataBuildingContext,
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinder.java
new file mode 100644
index 0000000000..2adc916c07
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinder.java
@@ -0,0 +1,73 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import java.util.Optional;
+import java.util.Set;
+import org.grails.datastore.mapping.model.DatastoreConfigurationException;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.PersistentClass;
+
+/** Binds the order-by clause and discriminator where condition to a collection. */
+public class CollectionOrderByBinder {
+
+ private final OrderByClauseBuilder orderByClauseBuilder;
+
+ /** Creates a new {@link CollectionOrderByBinder} instance. */
+ public CollectionOrderByBinder() {
+ this.orderByClauseBuilder = new OrderByClauseBuilder();
+ }
+
+ /** Binds the order-by clause and discriminator where condition to the given collection. */
+ public void bind(
+ HibernateToManyProperty property,
+ Collection collection,
+ PersistentClass associatedClass) {
+ GrailsHibernatePersistentEntity referenced = property.getHibernateAssociatedEntity();
+
+ if (referenced.isTablePerHierarchySubclass()) {
+ String discriminatorColumnName = referenced.getDiscriminatorColumnName();
+ Set<String> discSet = referenced.buildDiscriminatorSet();
+ String clause = String.join(",", discSet);
+ collection.setWhere(discriminatorColumnName + " in (" + clause + ")");
+ }
+
+ if (property.hasSort()) {
+ if (!property.isBidirectional() && property instanceof HibernateOneToManyProperty) {
+ throw new DatastoreConfigurationException(
+ "Default sort for associations ["
+ + property.getHibernateOwner().getName()
+ + "->"
+ + property.getName()
+ + "] are not supported with unidirectional one to many relationships.");
+ }
+ HibernatePersistentProperty sortBy =
+ (HibernatePersistentProperty) referenced.getPropertyByName(property.getSort());
+ String order = Optional.ofNullable(property.getOrder()).orElse("asc");
+ collection.setOrderBy(
+ orderByClauseBuilder.buildOrderByClause(
+ sortBy.getName(), associatedClass, collection.getRole(), order));
+ }
+ }
+}
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 9a34c98add..83410c5573 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
@@ -23,20 +23,15 @@ import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBind
import jakarta.annotation.Nonnull;
import java.util.*;
import java.util.Map;
-import java.util.Set;
-import org.grails.datastore.mapping.model.DatastoreConfigurationException;
import org.grails.datastore.mapping.model.config.GormProperties;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
-import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder;
import org.hibernate.MappingException;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.*;
@@ -51,7 +46,7 @@ public class CollectionSecondPassBinder {
private static final Logger LOG = LoggerFactory.getLogger(CollectionSecondPassBinder.class);
private final DefaultColumnNameFetcher defaultColumnNameFetcher;
- private final OrderByClauseBuilder orderByClauseBuilder;
+ private final CollectionOrderByBinder collectionOrderByBinder;
private final ManyToOneBinder manyToOneBinder;
private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
@@ -73,7 +68,8 @@ public class CollectionSecondPassBinder {
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
DefaultColumnNameFetcher defaultColumnNameFetcher,
- SimpleValueColumnBinder simpleValueColumnBinder) {
+ SimpleValueColumnBinder simpleValueColumnBinder,
+ CollectionOrderByBinder collectionOrderByBinder) {
this.manyToOneBinder = manyToOneBinder;
this.primaryKeyValueCreator = primaryKeyValueCreator;
this.collectionKeyColumnUpdater = collectionKeyColumnUpdater;
@@ -84,7 +80,7 @@ public class CollectionSecondPassBinder {
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
this.defaultColumnNameFetcher = defaultColumnNameFetcher;
this.simpleValueColumnBinder = simpleValueColumnBinder;
- this.orderByClauseBuilder = new OrderByClauseBuilder();
+ this.collectionOrderByBinder = collectionOrderByBinder;
}
/** Bind collection second pass. */
@@ -95,7 +91,7 @@ public class CollectionSecondPassBinder {
@Nonnull Collection collection) {
PersistentClass associatedClass = resolveAssociatedClass(property, persistentClasses);
- bindOrderBy(property, collection, associatedClass);
+ collectionOrderByBinder.bind(property, collection, associatedClass);
bindOneToManyAssociation(property, associatedClass, collection);
applyMultiTenantFilter(property, collection);
@@ -227,36 +223,4 @@ public class CollectionSecondPassBinder {
.orElseThrow(() -> new MappingException("Association [" + property.getName() + "] has no associated class"));
}
- private void bindOrderBy(
- HibernateToManyProperty property, Collection collection, PersistentClass associatedClass) {
- if (associatedClass == null) {
- return;
- }
- GrailsHibernatePersistentEntity referenced = property.getHibernateAssociatedEntity();
-
- if (referenced.isTablePerHierarchySubclass()) {
- String discriminatorColumnName = referenced.getDiscriminatorColumnName();
- Set<String> discSet = referenced.buildDiscriminatorSet();
- String inclause = String.join(",", discSet);
-
- collection.setWhere(discriminatorColumnName + " in (" + inclause + ")");
- }
-
- if (property.hasSort()) {
- if (!property.isBidirectional() && property instanceof HibernateOneToManyProperty) {
- throw new DatastoreConfigurationException(
- "Default sort for associations ["
- + property.getHibernateOwner().getName()
- + "->"
- + property.getName()
- + "] are not supported with unidirectional one to many relationships.");
- }
- HibernatePersistentProperty sortBy =
- (HibernatePersistentProperty) referenced.getPropertyByName(property.getSort());
- String order = Optional.ofNullable(property.getOrder()).orElse("asc");
- collection.setOrderBy(
- orderByClauseBuilder.buildOrderByClause(
- sortBy.getName(), associatedClass, collection.getRole(), order));
- }
- }
-}
+}
\ No newline at end of file
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinderSpec.groovy
new file mode 100644
index 0000000000..87a8819c0c
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionOrderByBinderSpec.groovy
@@ -0,0 +1,161 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.datastore.mapping.model.DatastoreConfigurationException
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.hibernate.mapping.Bag
+import org.hibernate.mapping.BasicValue
+import org.hibernate.mapping.Column
+import org.hibernate.mapping.Property
+import org.hibernate.mapping.RootClass
+import org.hibernate.mapping.Table
+import spock.lang.Subject
+
+class CollectionOrderByBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ CollectionOrderByBinder binder = new CollectionOrderByBinder()
+
+ void setupSpec() {
+ manager.addAllDomainClasses([
+ COBOwnerEntity,
+ COBAssociatedItem,
+ COBUnidirectionalOwner,
+ COBBaseItem,
+ COBSubItem,
+ COBHierarchyOwner,
+ ])
+ }
+
+ private HibernateToManyProperty propertyFor(Class ownerClass, String name = "items") {
+ (getPersistentEntity(ownerClass) as GrailsHibernatePersistentEntity).getPropertyByName(name) as HibernateToManyProperty
+ }
+
+ private RootClass rootClassWith(String entityName, String propertyName, String columnName) {
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def rootClass = new RootClass(mbc)
+ rootClass.setEntityName(entityName)
+ def table = new Table("test", entityName.toLowerCase())
+ def simpleValue = new BasicValue(mbc, table)
+ simpleValue.setTypeName("string")
+ simpleValue.addColumn(new Column(columnName))
+ def prop = new Property()
+ prop.setName(propertyName)
+ prop.setValue(simpleValue)
+ rootClass.addProperty(prop)
+ return rootClass
+ }
+
+ def "bind sets orderBy when sort is configured on a bidirectional association"() {
+ given:
+ def property = propertyFor(COBOwnerEntity)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ collection.setRole("${COBOwnerEntity.name}.items")
+ def associatedClass = rootClassWith(COBAssociatedItem.name, "value", "VALUE")
+ property.getMappedForm().setSort("value")
+ property.getMappedForm().setOrder("desc")
+
+ when:
+ binder.bind(property, collection, associatedClass)
+
+ then:
+ collection.getOrderBy() != null
+ collection.getOrderBy().contains("desc")
+ }
+
+ def "bind defaults to asc when order is not specified"() {
+ given:
+ def property = propertyFor(COBOwnerEntity)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ collection.setRole("${COBOwnerEntity.name}.items")
+ def associatedClass = rootClassWith(COBAssociatedItem.name, "value", "VALUE")
+ property.getMappedForm().setSort("value")
+
+ when:
+ binder.bind(property, collection, associatedClass)
+
+ then:
+ collection.getOrderBy() != null
+ collection.getOrderBy().contains("asc")
+ }
+
+ def "bind throws DatastoreConfigurationException for unidirectional one-to-many with sort"() {
+ given:
+ def property = propertyFor(COBUnidirectionalOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+ property.getMappedForm().setSort("value")
+
+ when:
+ binder.bind(property, collection, associatedClass)
+
+ then:
+ thrown(DatastoreConfigurationException)
+ }
+
+ def "bind does not set orderBy when no sort is configured"() {
+ given:
+ def property = propertyFor(COBOwnerEntity)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+
+ when:
+ binder.bind(property, collection, associatedClass)
+
+ then:
+ collection.getOrderBy() == null
+ }
+
+ def "bind sets where clause for table-per-hierarchy subclass"() {
+ given:
+ def property = propertyFor(COBHierarchyOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def associatedClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+
+ when:
+ binder.bind(property, collection, associatedClass)
+
+ then:
+ collection.getWhere() != null
+ collection.getWhere().contains("DTYPE in (")
+ collection.getWhere().contains("COBSubItem")
+ }
+}
+
+@Entity
+class COBOwnerEntity {
+ Long id
+ static hasMany = [items: COBAssociatedItem]
+}
+
+@Entity
+class COBAssociatedItem {
+ Long id
+ String value
+ COBOwnerEntity owner
+ static belongsTo = [owner: COBOwnerEntity]
+}
+
+@Entity
+class COBUnidirectionalOwner {
+ Long id
+ static hasMany = [items: COBAssociatedItem]
+}
+
+@Entity
+class COBBaseItem {
+ Long id
+ String value
+}
+
+@Entity
+class COBSubItem extends COBBaseItem {
+}
+
+@Entity
+class COBHierarchyOwner {
+ Long id
+ static hasMany = [items: COBSubItem]
+}
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 9a4ee671f9..09f56705c9 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
@@ -5,10 +5,8 @@ import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
-import org.hibernate.mapping.Bag
import org.hibernate.mapping.RootClass
-import org.grails.datastore.mapping.model.DatastoreConfigurationException
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
@@ -43,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(mtob, pkvc, cku, botml, dkvb, uotmb, cwjtb, cfpcb, dcnf, svcb)
+ binder = new CollectionSecondPassBinder(mtob, pkvc, cku, botml, dkvb, uotmb, cwjtb, cfpcb, dcnf, svcb, new CollectionOrderByBinder())
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
@@ -52,63 +50,6 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
return property
}
- def "test bindOrderBy with sort configured"() {
- given:
- def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- collection.setRole("CSPBTestEntityWithMany.items")
-
- def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
- def valueProperty = new org.hibernate.mapping.Property()
- valueProperty.setName("value")
- def simpleValue = new org.hibernate.mapping.BasicValue(getGrailsDomainBinder().getMetadataBuildingContext(), new org.hibernate.mapping.Table("test", "ASSOCIATED_ITEM"))
- simpleValue.setTypeName("string")
- def column = new org.hibernate.mapping.Column("VALUE")
- simpleValue.addColumn(column)
- valueProperty.setValue(simpleValue)
- associatedPersistentClass.addProperty(valueProperty)
-
- property.getMappedForm().setSort("value")
- property.getMappedForm().setOrder("desc")
-
- when:
- binder.bindOrderBy(property, collection, associatedPersistentClass)
-
- then:
- collection.getOrderBy() != null
- }
-
- def "test bindOrderBy with unidirectional one-to-many throws exception"() {
- given:
- def property = createTestHibernateToManyProperty(CSPBUnidirectionalEntity, "items") as HibernateToManyProperty
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
-
- property.getMappedForm().setSort("value")
-
- when:
- binder.bindOrderBy(property, collection, associatedPersistentClass)
-
- then:
- thrown(DatastoreConfigurationException)
- }
-
- def "test bindOrderBy does nothing without sort"() {
- given:
- def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
-
- when:
- binder.bindOrderBy(property, collection, associatedPersistentClass)
-
- then:
- collection.getOrderBy() == null
- }
-
def "resolveAssociatedClass throws MappingException when property has no associated entity"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
@@ -135,7 +76,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
ex.message.contains("items")
}
- def "test resolveAssociatedClass returns associatedClass"() {
+ def "resolveAssociatedClass returns the matching PersistentClass"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
@@ -148,31 +89,6 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
then:
result == associatedPersistentClass
}
-
- def "test bindOrderBy with table per hierarchy subclass"() {
- given:
- def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
- associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
-
- // Mock GrailsHibernatePersistentEntity behavior for table per hierarchy
- def referencedEntity = property.getHibernateAssociatedEntity()
- def spiedReferencedEntity = Spy(referencedEntity)
- spiedReferencedEntity.isTablePerHierarchySubclass() >> true
- spiedReferencedEntity.getDiscriminatorColumnName() >> "item_type"
- spiedReferencedEntity.buildDiscriminatorSet() >> (["'A'", "'B'"] as Set)
-
- // Inject the spy if possible, or mock the getter on property
- def spiedProperty = Spy(property)
- spiedProperty.getHibernateAssociatedEntity() >> spiedReferencedEntity
-
- when:
- binder.bindOrderBy(spiedProperty, collection, associatedPersistentClass)
-
- then:
- collection.getWhere() == "item_type in ('A','B')"
- }
}
@Entity
@@ -186,13 +102,6 @@ class CSPBTestEntityWithMany {
class CSPBAssociatedItem {
Long id
String value
- CSPBTestEntityWithMany parent // Bidirectional for association property testing
+ CSPBTestEntityWithMany parent
static belongsTo = [parent: CSPBTestEntityWithMany]
}
-
-@Entity
-class CSPBUnidirectionalEntity {
- Long id
- Set<CSPBAssociatedItem> items
- static hasMany = [items: CSPBAssociatedItem]
-}
(grails-core) 22/24: efactor: extract BasicCollectionElementBinder from CollectionWithJoinTableBinder
Posted by bo...@apache.org.
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 b965e8aefd56f45fcb679ab0586b4fcbf80905b7
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 15:48:08 2026 -0600
efactor: extract BasicCollectionElementBinder from CollectionWithJoinTableBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 11 ++-
.../hibernate/HibernateToManyProperty.java | 4 +
.../secondpass/BasicCollectionElementBinder.java | 99 ++++++++++++++++++++++
.../secondpass/CollectionWithJoinTableBinder.java | 78 +++--------------
.../BasicCollectionElementBinderSpec.groovy | 82 ++++++++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 2 +-
.../CollectionWithJoinTableBinderSpec.groovy | 23 ++---
.../UnidirectionalOneToManyBinderSpec.groovy | 10 ++-
8 files changed, 224 insertions(+), 85 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index b397d360ad..71e1bf5fb3 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -28,6 +28,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionType;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BasicCollectionElementBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToManyLinker;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalMapElementBinder;
@@ -102,12 +103,16 @@ public class CollectionBinder {
metadataBuildingContext,
namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
- enumTypeBinder,
compositeIdentifierToManyToOneBinder,
- simpleValueColumnFetcher,
collectionForPropertyConfigBinder,
simpleValueColumnBinder,
- new ColumnConfigToColumnBinder());
+ new BasicCollectionElementBinder(
+ metadataBuildingContext,
+ namingStrategy,
+ enumTypeBinder,
+ simpleValueColumnBinder,
+ simpleValueColumnFetcher,
+ new ColumnConfigToColumnBinder()));
this.collectionSecondPassBinder =
new CollectionSecondPassBinder(
new CollectionKeyColumnUpdater(
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
index ea6d8d38ac..5dad0c18d9 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
@@ -68,6 +68,10 @@ public interface HibernateToManyProperty
.orElse(null);
}
+ default boolean isBasic() {
+ return this instanceof Basic;
+ }
+
/**
* @return Whether the collection should be bound with a foreign key
*/
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinder.java
new file mode 100644
index 0000000000..98918589d3
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinder.java
@@ -0,0 +1,99 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.UNDERSCORE;
+
+import java.util.Optional;
+import org.grails.datastore.mapping.model.types.Basic;
+import org.grails.orm.hibernate.cfg.ColumnConfig;
+import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
+import org.grails.orm.hibernate.cfg.PropertyConfig;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ColumnConfigToColumnBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover;
+import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
+import org.hibernate.boot.spi.MetadataBuildingContext;
+import org.hibernate.mapping.BasicValue;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.Column;
+
+/** Binds the element value for a basic (scalar or enum) collection. */
+public class BasicCollectionElementBinder {
+
+ private final MetadataBuildingContext metadataBuildingContext;
+ private final PersistentEntityNamingStrategy namingStrategy;
+ private final EnumTypeBinder enumTypeBinder;
+ private final SimpleValueColumnBinder simpleValueColumnBinder;
+ private final SimpleValueColumnFetcher simpleValueColumnFetcher;
+ private final ColumnConfigToColumnBinder columnConfigToColumnBinder;
+
+ /** Creates a new {@link BasicCollectionElementBinder} instance. */
+ public BasicCollectionElementBinder(
+ MetadataBuildingContext metadataBuildingContext,
+ PersistentEntityNamingStrategy namingStrategy,
+ EnumTypeBinder enumTypeBinder,
+ SimpleValueColumnBinder simpleValueColumnBinder,
+ SimpleValueColumnFetcher simpleValueColumnFetcher,
+ ColumnConfigToColumnBinder columnConfigToColumnBinder) {
+ this.metadataBuildingContext = metadataBuildingContext;
+ this.namingStrategy = namingStrategy;
+ this.enumTypeBinder = enumTypeBinder;
+ this.simpleValueColumnBinder = simpleValueColumnBinder;
+ this.simpleValueColumnFetcher = simpleValueColumnFetcher;
+ this.columnConfigToColumnBinder = columnConfigToColumnBinder;
+ }
+
+ /** Creates and binds a {@link BasicValue} element for the given basic collection property. */
+ public BasicValue bind(HibernateToManyProperty property, Collection collection) {
+ BasicValue element = new BasicValue(metadataBuildingContext, collection.getCollectionTable());
+ final Class<?> referencedType = ((Basic) property).getComponentType();
+ final boolean isEnum = referencedType.isEnum();
+ var joinColumnMappingOptional =
+ Optional.ofNullable(property.getMappedForm()).map(PropertyConfig::getJoinTableColumnConfig);
+ String columnName;
+ if (joinColumnMappingOptional.isPresent()) {
+ columnName = joinColumnMappingOptional.get().getName();
+ } else {
+ var clazz = namingStrategy.resolveColumnName(referencedType.getName());
+ var prop = namingStrategy.resolveTableName(property.getName());
+ columnName =
+ isEnum
+ ? clazz
+ : new BackticksRemover().apply(prop)
+ + UNDERSCORE
+ + new BackticksRemover().apply(clazz);
+ }
+ if (isEnum) {
+ enumTypeBinder.bindEnumType(property, referencedType, element, columnName);
+ } else {
+ String typeName = property.getTypeName(referencedType);
+ simpleValueColumnBinder.bindSimpleValue(element, typeName, columnName, true);
+ if (joinColumnMappingOptional.isPresent()) {
+ Column column = simpleValueColumnFetcher.getColumnForSimpleValue(element);
+ ColumnConfig columnConfig = joinColumnMappingOptional.get();
+ final PropertyConfig mappedForm = property.getMappedForm();
+ columnConfigToColumnBinder.bindColumnConfigToColumn(column, columnConfig, mappedForm);
+ }
+ }
+ return element;
+ }
+}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
index 29b694b44c..e880b21c56 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
@@ -22,16 +22,11 @@ import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBind
import jakarta.annotation.Nonnull;
import java.util.Optional;
-import org.grails.datastore.mapping.model.types.Basic;
import org.grails.orm.hibernate.cfg.*;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
-import org.grails.orm.hibernate.cfg.domainbinding.binder.ColumnConfigToColumnBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder;
-import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover;
-import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.*;
@@ -45,33 +40,27 @@ public class CollectionWithJoinTableBinder {
private final PersistentEntityNamingStrategy namingStrategy;
private final UnidirectionalOneToManyInverseValuesBinder
unidirectionalOneToManyInverseValuesBinder;
- private final EnumTypeBinder enumTypeBinder;
private final CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder;
- private final SimpleValueColumnFetcher simpleValueColumnFetcher;
private final CollectionForPropertyConfigBinder collectionForPropertyConfigBinder;
private final SimpleValueColumnBinder simpleValueColumnBinder;
- private final ColumnConfigToColumnBinder columnConfigToColumnBinder;
+ private final BasicCollectionElementBinder basicCollectionElementBinder;
/** Creates a new {@link CollectionWithJoinTableBinder} instance. */
public CollectionWithJoinTableBinder(
MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
UnidirectionalOneToManyInverseValuesBinder unidirectionalOneToManyInverseValuesBinder,
- EnumTypeBinder enumTypeBinder,
CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder,
- SimpleValueColumnFetcher simpleValueColumnFetcher,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
SimpleValueColumnBinder simpleValueColumnBinder,
- ColumnConfigToColumnBinder columnConfigToColumnBinder) {
+ BasicCollectionElementBinder basicCollectionElementBinder) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.unidirectionalOneToManyInverseValuesBinder = unidirectionalOneToManyInverseValuesBinder;
- this.enumTypeBinder = enumTypeBinder;
this.compositeIdentifierToManyToOneBinder = compositeIdentifierToManyToOneBinder;
- this.simpleValueColumnFetcher = simpleValueColumnFetcher;
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
this.simpleValueColumnBinder = simpleValueColumnBinder;
- this.columnConfigToColumnBinder = columnConfigToColumnBinder;
+ this.basicCollectionElementBinder = basicCollectionElementBinder;
}
/** Bind collection with join table. */
@@ -82,73 +71,34 @@ public class CollectionWithJoinTableBinder {
collection.setInverse(false);
SimpleValue element;
- final boolean isBasicCollectionType = property instanceof Basic;
- if (isBasicCollectionType) {
- element = new BasicValue(metadataBuildingContext, collection.getCollectionTable());
+ if (property.isBasic()) {
+ element = basicCollectionElementBinder.bind(property, collection);
} else {
- // for a normal unidirectional one-to-many we use a join column
element = new ManyToOne(metadataBuildingContext, collection.getCollectionTable());
unidirectionalOneToManyInverseValuesBinder.bindUnidirectionalOneToManyInverseValues(
property, (ManyToOne) element);
- }
-
- String columnName;
-
- var joinColumnMappingOptional =
- Optional.ofNullable(property.getMappedForm()).map(PropertyConfig::getJoinTableColumnConfig);
- if (isBasicCollectionType) {
- final Class<?> referencedType = ((Basic) property).getComponentType();
- final boolean isEnum = referencedType.isEnum();
- if (joinColumnMappingOptional.isPresent()) {
- columnName = joinColumnMappingOptional.get().getName();
- } else {
- var clazz = namingStrategy.resolveColumnName(referencedType.getName());
- var prop = namingStrategy.resolveTableName(property.getName());
- columnName =
- isEnum
- ? clazz
- : new BackticksRemover().apply(prop)
- + UNDERSCORE
- + new BackticksRemover().apply(clazz);
- }
-
- if (isEnum) {
- enumTypeBinder.bindEnumType(property, referencedType, (BasicValue) element, columnName);
- } else {
-
- String typeName = property.getTypeName(referencedType);
-
- simpleValueColumnBinder.bindSimpleValue(element, typeName, columnName, true);
- if (joinColumnMappingOptional.isPresent()) {
- Column column = simpleValueColumnFetcher.getColumnForSimpleValue(element);
- ColumnConfig columnConfig = joinColumnMappingOptional.get();
- final PropertyConfig mappedForm = property.getMappedForm();
- columnConfigToColumnBinder.bindColumnConfigToColumn(column, columnConfig, mappedForm);
- }
- }
- } else {
final var domainClass = property.getHibernateAssociatedEntity();
-
if (domainClass != null) {
+ var joinColumnMappingOptional =
+ Optional.ofNullable(property.getMappedForm())
+ .map(PropertyConfig::getJoinTableColumnConfig);
if (domainClass.getHibernateCompositeIdentity().isPresent()) {
CompositeIdentity ci = domainClass.getHibernateCompositeIdentity().get();
compositeIdentifierToManyToOneBinder.bindCompositeIdentifierToManyToOne(
property, element, ci, domainClass, EMPTY_PATH);
} else {
- if (joinColumnMappingOptional.isPresent()) {
- columnName = joinColumnMappingOptional.get().getName();
- } else {
- var decapitalize = domainClass.getHibernateRootEntity().getJavaClass().getSimpleName();
- columnName = namingStrategy.resolveColumnName(decapitalize) + FOREIGN_KEY_SUFFIX;
- }
-
+ String columnName =
+ joinColumnMappingOptional.isPresent()
+ ? joinColumnMappingOptional.get().getName()
+ : namingStrategy.resolveColumnName(
+ domainClass.getHibernateRootEntity().getJavaClass().getSimpleName())
+ + FOREIGN_KEY_SUFFIX;
simpleValueColumnBinder.bindSimpleValue(element, "long", columnName, true);
}
}
}
collection.setElement(element);
-
collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinderSpec.groovy
new file mode 100644
index 0000000000..d623ed7c61
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BasicCollectionElementBinderSpec.groovy
@@ -0,0 +1,82 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ColumnConfigToColumnBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher
+import org.hibernate.mapping.BasicValue
+import org.hibernate.mapping.Collection
+import org.hibernate.mapping.RootClass
+import org.hibernate.mapping.Set
+import org.hibernate.mapping.Table
+import org.hibernate.type.spi.TypeConfiguration
+import spock.lang.Subject
+
+class BasicCollectionElementBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ BasicCollectionElementBinder binder
+
+ EnumTypeBinder enumTypeBinder = Mock(EnumTypeBinder)
+
+ void setup() {
+ def domainBinder = getGrailsDomainBinder()
+ binder = new BasicCollectionElementBinder(
+ domainBinder.metadataBuildingContext,
+ domainBinder.namingStrategy,
+ enumTypeBinder,
+ new SimpleValueColumnBinder(),
+ new SimpleValueColumnFetcher(),
+ new ColumnConfigToColumnBinder()
+ )
+ }
+
+ private Collection collectionWithTable(String tableName) {
+ def mbc = getGrailsDomainBinder().metadataBuildingContext
+ def collection = new Set(mbc, new RootClass(mbc))
+ collection.setCollectionTable(new Table(tableName))
+ return collection
+ }
+
+ void "bind creates BasicValue with column for scalar collection"() {
+ given:
+ def entity = createPersistentEntity(BCEBAuthor)
+ HibernateToManyProperty property = (HibernateToManyProperty) entity.getPropertyByName("tags")
+ Collection collection = collectionWithTable("bceb_author_tags")
+
+ when:
+ BasicValue element = binder.bind(property, collection)
+
+ then:
+ element != null
+ element.getColumnSpan() > 0
+ 0 * enumTypeBinder.bindEnumType(*_)
+ }
+
+ void "bind delegates to enumTypeBinder for enum collection"() {
+ given:
+ def entity = createPersistentEntity(BCEBAuthor)
+ HibernateToManyProperty property = (HibernateToManyProperty) entity.getPropertyByName("statuses")
+ Collection collection = collectionWithTable("bceb_author_statuses")
+
+ when:
+ BasicValue element = binder.bind(property, collection)
+
+ then:
+ element != null
+ 1 * enumTypeBinder.bindEnumType(property, BCEBStatus, _ as BasicValue, _)
+ }
+}
+
+enum BCEBStatus { ACTIVE, INACTIVE }
+
+@Entity
+class BCEBAuthor {
+ Long id
+ java.util.Set<String> tags
+ java.util.Set<BCEBStatus> statuses
+ static hasMany = [tags: String, statuses: BCEBStatus]
+}
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 d89e94b226..2cd4ee88dc 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
@@ -34,7 +34,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def pkvc = new PrimaryKeyValueCreator(mbc)
def botml = new BidirectionalOneToManyLinker(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver())
def dkvb = new DependentKeyValueBinder(svb, citmto)
- def cwjtb = new CollectionWithJoinTableBinder(mbc, ns, new UnidirectionalOneToManyInverseValuesBinder(), null, citmto, svcf, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), null)
+ def cwjtb = new CollectionWithJoinTableBinder(mbc, ns, new UnidirectionalOneToManyInverseValuesBinder(), citmto, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), new BasicCollectionElementBinder(mbc, ns, null, new SimpleValueColumnBinder(), svcf, null))
def uotmb = new UnidirectionalOneToManyBinder(cwjtb, mbc.getMetadataCollector())
def cfpcb = new CollectionForPropertyConfigBinder()
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
index c6b32a6fd2..abc62e0ebd 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
@@ -4,11 +4,8 @@ import grails.gorm.annotation.Entity
import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder
-import org.grails.orm.hibernate.cfg.domainbinding.binder.ColumnConfigToColumnBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
-import org.grails.orm.hibernate.cfg.domainbinding.binder.EnumTypeBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder
-import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
import org.hibernate.boot.spi.InFlightMetadataCollector
import org.hibernate.mapping.Collection
@@ -16,7 +13,6 @@ import org.hibernate.mapping.ManyToOne
import org.hibernate.mapping.RootClass
import org.hibernate.mapping.Set
import org.hibernate.mapping.Table
-import org.hibernate.type.spi.TypeConfiguration
import spock.lang.Subject
class CollectionWithJoinTableBinderSpec extends HibernateGormDatastoreSpec {
@@ -25,12 +21,10 @@ class CollectionWithJoinTableBinderSpec extends HibernateGormDatastoreSpec {
CollectionWithJoinTableBinder binder
UnidirectionalOneToManyInverseValuesBinder unidirectionalOneToManyInverseValuesBinder = Mock(UnidirectionalOneToManyInverseValuesBinder)
- EnumTypeBinder enumTypeBinder = Mock(EnumTypeBinder)
CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder = Mock(CompositeIdentifierToManyToOneBinder)
- SimpleValueColumnFetcher simpleValueColumnFetcher = Mock(SimpleValueColumnFetcher)
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder = Mock(CollectionForPropertyConfigBinder)
+ BasicCollectionElementBinder basicCollectionElementBinder = Mock(BasicCollectionElementBinder)
SimpleValueColumnBinder simpleValueColumnBinder = new SimpleValueColumnBinder()
- ColumnConfigToColumnBinder columnConfigToColumnBinder = new ColumnConfigToColumnBinder()
void setup() {
def domainBinder = getGrailsDomainBinder()
@@ -38,37 +32,38 @@ class CollectionWithJoinTableBinderSpec extends HibernateGormDatastoreSpec {
domainBinder.metadataBuildingContext,
domainBinder.namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
- enumTypeBinder,
compositeIdentifierToManyToOneBinder,
- simpleValueColumnFetcher,
collectionForPropertyConfigBinder,
simpleValueColumnBinder,
- columnConfigToColumnBinder
+ basicCollectionElementBinder
)
}
- void "test bindCollectionWithJoinTable for basic type"() {
+ void "test bindCollectionWithJoinTable delegates to BasicCollectionElementBinder for basic type"() {
given:
PersistentEntity authorEntity = createPersistentEntity(CWJTBAuthor)
HibernateToManyProperty property = (HibernateToManyProperty) authorEntity.getPropertyByName("tags")
def domainBinder = getGrailsDomainBinder()
InFlightMetadataCollector mappings = Mock(InFlightMetadataCollector)
- mappings.getTypeConfiguration() >> new TypeConfiguration()
def owner = new RootClass(domainBinder.metadataBuildingContext)
Collection collection = new Set(domainBinder.metadataBuildingContext, owner)
collection.setCollectionTable(new Table("CWJTB_TAGS"))
+ def basicValue = new org.hibernate.mapping.BasicValue(domainBinder.metadataBuildingContext, collection.getCollectionTable())
+ basicCollectionElementBinder.bind(property, collection) >> basicValue
+
when:
binder.bindCollectionWithJoinTable(property, mappings, collection)
then:
- collection.getElement() != null
+ 1 * basicCollectionElementBinder.bind(property, collection) >> basicValue
+ collection.getElement() == basicValue
1 * collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property)
}
- void "test bindCollectionWithJoinTable for entity association"() {
+ void "test bindCollectionWithJoinTable creates ManyToOne element for entity association"() {
given:
createPersistentEntity(CWJTBBook)
PersistentEntity authorEntity = createPersistentEntity(CWJTBAuthor)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
index 56682ccb04..c26c011306 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
@@ -49,12 +49,16 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
metadataBuildingContext,
namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
- enumTypeBinder,
compositeIdentifierToManyToOneBinder,
- simpleValueColumnFetcher,
collectionForPropertyConfigBinder,
new SimpleValueColumnBinder(),
- new ColumnConfigToColumnBinder()
+ new BasicCollectionElementBinder(
+ metadataBuildingContext,
+ namingStrategy,
+ enumTypeBinder,
+ new SimpleValueColumnBinder(),
+ simpleValueColumnFetcher,
+ new ColumnConfigToColumnBinder())
)
binder = new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, grailsDomainBinder.metadataBuildingContext.metadataCollector)
}
(grails-core) 17/24: refactor: extract bindBidirectionalMapElement into BidirectionalMapElementBinder
Posted by bo...@apache.org.
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 380769aaf63cc776225cfee7d8f878f473101715
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 11:06:36 2026 -0600
refactor: extract bindBidirectionalMapElement into BidirectionalMapElementBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 2 +
.../secondpass/BidirectionalMapElementBinder.java | 54 ++++++++++++++
.../secondpass/CollectionSecondPassBinder.java | 17 ++---
.../BidirectionalMapElementBinderSpec.groovy | 82 ++++++++++++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 2 +-
5 files changed, 143 insertions(+), 14 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index f8ec48d3fd..0ddbbfb8f8 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -30,6 +30,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToManyLinker;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalMapElementBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionMultiTenantFilterBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionOrderByBinder;
@@ -118,6 +119,7 @@ public class CollectionBinder {
new BidirectionalOneToManyLinker(grailsPropertyResolver),
new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
simpleValueColumnBinder),
+ new BidirectionalMapElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new CollectionOrderByBinder(),
new CollectionMultiTenantFilterBinder(new DefaultColumnNameFetcher(namingStrategy)));
this.listSecondPassBinder =
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinder.java
new file mode 100644
index 0000000000..a16946ccb0
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinder.java
@@ -0,0 +1,54 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.EMPTY_PATH;
+
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.ManyToOne;
+
+/** Binds the element of a bidirectional one-to-many Map association. */
+public class BidirectionalMapElementBinder {
+
+ private final ManyToOneBinder manyToOneBinder;
+ private final CollectionForPropertyConfigBinder collectionForPropertyConfigBinder;
+
+ /** Creates a new {@link BidirectionalMapElementBinder} instance. */
+ public BidirectionalMapElementBinder(
+ ManyToOneBinder manyToOneBinder,
+ CollectionForPropertyConfigBinder collectionForPropertyConfigBinder) {
+ this.manyToOneBinder = manyToOneBinder;
+ this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
+ }
+
+ /** Binds the ManyToOne element for a bidirectional Map collection. */
+ public void bind(HibernateToManyProperty property, Collection collection) {
+ HibernateManyToOneProperty otherSide =
+ (HibernateManyToOneProperty) property.getHibernateInverseSide();
+ ManyToOne element =
+ manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
+ element.setReferencedEntityName(otherSide.getOwner().getName());
+ collection.setElement(element);
+ collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
+ }
+}
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 c413e9eeb2..ee69807823 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
@@ -28,7 +28,6 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
import org.hibernate.MappingException;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.*;
@@ -40,6 +39,7 @@ public class CollectionSecondPassBinder {
private final CollectionOrderByBinder collectionOrderByBinder;
private final CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder;
private final CollectionKeyBinder collectionKeyBinder;
+ private final BidirectionalMapElementBinder bidirectionalMapElementBinder;
private final ManyToOneBinder manyToOneBinder;
private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
@@ -56,6 +56,7 @@ public class CollectionSecondPassBinder {
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
CollectionKeyBinder collectionKeyBinder,
+ BidirectionalMapElementBinder bidirectionalMapElementBinder,
CollectionOrderByBinder collectionOrderByBinder,
CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder) {
this.manyToOneBinder = manyToOneBinder;
@@ -65,6 +66,7 @@ public class CollectionSecondPassBinder {
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
this.collectionKeyBinder = collectionKeyBinder;
+ this.bidirectionalMapElementBinder = bidirectionalMapElementBinder;
this.collectionOrderByBinder = collectionOrderByBinder;
this.collectionMultiTenantFilterBinder = collectionMultiTenantFilterBinder;
}
@@ -119,7 +121,7 @@ public class CollectionSecondPassBinder {
if (property instanceof HibernateManyToManyProperty manyToMany && manyToMany.isBidirectional()) {
bindManyToManyElement(manyToMany, collection);
} else if (property.isBidirectionalOneToManyMap() && property.isBidirectional()) {
- bindBidirectionalMapElement(property, collection);
+ bidirectionalMapElementBinder.bind(property, collection);
} else if (property instanceof HibernateOneToManyProperty oneToManyProperty && oneToManyProperty.isUnidirectionalOneToMany()) {
unidirectionalOneToManyBinder.bind(oneToManyProperty, collection);
} else if (property.supportsJoinColumnMapping()) {
@@ -140,17 +142,6 @@ public class CollectionSecondPassBinder {
}
}
- private void bindBidirectionalMapElement(
- HibernateToManyProperty property, Collection collection) {
- HibernateManyToOneProperty otherSide =
- (HibernateManyToOneProperty) property.getHibernateInverseSide();
- ManyToOne element =
- manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
- element.setReferencedEntityName(otherSide.getOwner().getName());
- collection.setElement(element);
- collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
- }
-
private @Nonnull PersistentClass resolveAssociatedClass(
HibernateToManyProperty property, Map<?, ?> persistentClasses) {
return Optional.ofNullable(property.getHibernateAssociatedEntity())
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinderSpec.groovy
new file mode 100644
index 0000000000..409701a4b3
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/BidirectionalMapElementBinderSpec.groovy
@@ -0,0 +1,82 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.hibernate.mapping.Bag
+import org.hibernate.mapping.ManyToOne
+import org.hibernate.mapping.Table
+import spock.lang.Subject
+
+class BidirectionalMapElementBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ BidirectionalMapElementBinder binder
+
+ void setupSpec() {
+ manager.addAllDomainClasses([
+ BBMEOwner,
+ BBMEItem,
+ ])
+ }
+
+ void setup() {
+ def gdb = getGrailsDomainBinder()
+ def mbc = gdb.getMetadataBuildingContext()
+ def ns = gdb.getNamingStrategy()
+ def je = gdb.getJdbcEnvironment()
+ def svb = new SimpleValueBinder(mbc, ns, je)
+ def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
+ def mtob = new ManyToOneBinder(mbc, ns, svb, new ManyToOneValuesBinder(), citmto)
+ binder = new BidirectionalMapElementBinder(mtob, new CollectionForPropertyConfigBinder())
+ }
+
+ private HibernateToManyProperty propertyFor(Class ownerClass, String name = "items") {
+ (getPersistentEntity(ownerClass) as GrailsHibernatePersistentEntity).getPropertyByName(name) as HibernateToManyProperty
+ }
+
+ def "bind sets ManyToOne element referencing the inverse side's owner"() {
+ given:
+ def property = propertyFor(BBMEOwner)
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def collection = new Bag(mbc, null)
+ collection.setCollectionTable(new Table("test", "bbme_owner_items"))
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getElement() instanceof ManyToOne
+ (collection.getElement() as ManyToOne).getReferencedEntityName() == BBMEItem.name
+ }
+
+ def "bind honours isBidirectionalOneToManyMap on the property"() {
+ given:
+ def property = propertyFor(BBMEOwner)
+
+ expect:
+ property.isBidirectionalOneToManyMap()
+ property.isBidirectional()
+ }
+}
+
+@Entity
+class BBMEOwner {
+ Long id
+ Map<String, BBMEItem> items
+ static hasMany = [items: BBMEItem]
+}
+
+@Entity
+class BBMEItem {
+ Long id
+ String description
+ BBMEOwner owner
+ static belongsTo = [owner: BBMEOwner]
+}
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 2f01558daf..49f127415d 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
@@ -41,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(mtob, pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
+ binder = new CollectionSecondPassBinder(mtob, pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new BidirectionalMapElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
(grails-core) 18/24: refactor: extract bindManyToManyElement into ManyToManyElementBinder
Posted by bo...@apache.org.
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 6cf5923907b2dc2da036679aab3b791a42c7aaa4
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 11:57:26 2026 -0600
refactor: extract bindManyToManyElement into ManyToManyElementBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 3 +-
.../secondpass/CollectionSecondPassBinder.java | 24 +-----
.../secondpass/ManyToManyElementBinder.java | 55 ++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 2 +-
.../secondpass/ManyToManyElementBinderSpec.groovy | 99 ++++++++++++++++++++++
5 files changed, 161 insertions(+), 22 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 0ddbbfb8f8..2a9fa23bcc 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -32,6 +32,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToM
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalMapElementBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.ManyToManyElementBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionMultiTenantFilterBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionOrderByBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionSecondPassBinder;
@@ -109,7 +110,6 @@ public class CollectionBinder {
new ColumnConfigToColumnBinder());
this.collectionSecondPassBinder =
new CollectionSecondPassBinder(
- manyToOneBinder,
new PrimaryKeyValueCreator(metadataBuildingContext),
new CollectionKeyColumnUpdater(),
new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
@@ -120,6 +120,7 @@ public class CollectionBinder {
new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
simpleValueColumnBinder),
new BidirectionalMapElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
+ new ManyToManyElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new CollectionOrderByBinder(),
new CollectionMultiTenantFilterBinder(new DefaultColumnNameFetcher(namingStrategy)));
this.listSecondPassBinder =
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 ee69807823..501f49d55b 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
@@ -18,13 +18,10 @@
*/
package org.grails.orm.hibernate.cfg.domainbinding.secondpass;
-import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.*;
-
import jakarta.annotation.Nonnull;
import java.util.*;
import java.util.Map;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
-import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
@@ -40,7 +37,7 @@ public class CollectionSecondPassBinder {
private final CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder;
private final CollectionKeyBinder collectionKeyBinder;
private final BidirectionalMapElementBinder bidirectionalMapElementBinder;
- private final ManyToOneBinder manyToOneBinder;
+ private final ManyToManyElementBinder manyToManyElementBinder;
private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
private final UnidirectionalOneToManyBinder unidirectionalOneToManyBinder;
@@ -49,7 +46,6 @@ public class CollectionSecondPassBinder {
/** Creates a new {@link CollectionSecondPassBinder} instance. */
public CollectionSecondPassBinder(
- ManyToOneBinder manyToOneBinder,
PrimaryKeyValueCreator primaryKeyValueCreator,
CollectionKeyColumnUpdater collectionKeyColumnUpdater,
UnidirectionalOneToManyBinder unidirectionalOneToManyBinder,
@@ -57,9 +53,9 @@ public class CollectionSecondPassBinder {
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
CollectionKeyBinder collectionKeyBinder,
BidirectionalMapElementBinder bidirectionalMapElementBinder,
+ ManyToManyElementBinder manyToManyElementBinder,
CollectionOrderByBinder collectionOrderByBinder,
CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder) {
- this.manyToOneBinder = manyToOneBinder;
this.primaryKeyValueCreator = primaryKeyValueCreator;
this.collectionKeyColumnUpdater = collectionKeyColumnUpdater;
this.unidirectionalOneToManyBinder = unidirectionalOneToManyBinder;
@@ -67,6 +63,7 @@ public class CollectionSecondPassBinder {
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
this.collectionKeyBinder = collectionKeyBinder;
this.bidirectionalMapElementBinder = bidirectionalMapElementBinder;
+ this.manyToManyElementBinder = manyToManyElementBinder;
this.collectionOrderByBinder = collectionOrderByBinder;
this.collectionMultiTenantFilterBinder = collectionMultiTenantFilterBinder;
}
@@ -119,7 +116,7 @@ public class CollectionSecondPassBinder {
InFlightMetadataCollector mappings,
Collection collection) {
if (property instanceof HibernateManyToManyProperty manyToMany && manyToMany.isBidirectional()) {
- bindManyToManyElement(manyToMany, collection);
+ manyToManyElementBinder.bind(manyToMany, collection);
} else if (property.isBidirectionalOneToManyMap() && property.isBidirectional()) {
bidirectionalMapElementBinder.bind(property, collection);
} else if (property instanceof HibernateOneToManyProperty oneToManyProperty && oneToManyProperty.isUnidirectionalOneToMany()) {
@@ -129,19 +126,6 @@ public class CollectionSecondPassBinder {
}
}
- private void bindManyToManyElement(
- HibernateManyToManyProperty manyToMany, Collection collection) {
- HibernateManyToManyProperty otherSide = manyToMany.getHibernateInverseSide();
- ManyToOne element =
- manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
- element.setReferencedEntityName(otherSide.getOwner().getName());
- collection.setElement(element);
- collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, manyToMany);
- if (manyToMany.isCircular()) {
- collection.setInverse(false);
- }
- }
-
private @Nonnull PersistentClass resolveAssociatedClass(
HibernateToManyProperty property, Map<?, ?> persistentClasses) {
return Optional.ofNullable(property.getHibernateAssociatedEntity())
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinder.java
new file mode 100644
index 0000000000..9d4aa3654b
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinder.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.EMPTY_PATH;
+
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
+import org.hibernate.mapping.Collection;
+import org.hibernate.mapping.ManyToOne;
+
+/** Binds the element of a bidirectional many-to-many association. */
+public class ManyToManyElementBinder {
+
+ private final ManyToOneBinder manyToOneBinder;
+ private final CollectionForPropertyConfigBinder collectionForPropertyConfigBinder;
+
+ /** Creates a new {@link ManyToManyElementBinder} instance. */
+ public ManyToManyElementBinder(
+ ManyToOneBinder manyToOneBinder,
+ CollectionForPropertyConfigBinder collectionForPropertyConfigBinder) {
+ this.manyToOneBinder = manyToOneBinder;
+ this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
+ }
+
+ /** Binds the ManyToOne element for a bidirectional many-to-many collection. */
+ public void bind(HibernateManyToManyProperty manyToMany, Collection collection) {
+ HibernateManyToManyProperty otherSide = manyToMany.getHibernateInverseSide();
+ ManyToOne element =
+ manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
+ element.setReferencedEntityName(otherSide.getOwner().getName());
+ collection.setElement(element);
+ collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, manyToMany);
+ if (manyToMany.isCircular()) {
+ collection.setInverse(false);
+ }
+ }
+}
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 49f127415d..4aa8190304 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
@@ -41,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(mtob, pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new BidirectionalMapElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
+ binder = new CollectionSecondPassBinder(pkvc, cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb), new BidirectionalMapElementBinder(mtob, cfpcb), new ManyToManyElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinderSpec.groovy
new file mode 100644
index 0000000000..af397f4a2a
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ManyToManyElementBinderSpec.groovy
@@ -0,0 +1,99 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneValuesBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty
+import org.hibernate.mapping.Bag
+import org.hibernate.mapping.ManyToOne
+import org.hibernate.mapping.Table
+import spock.lang.Subject
+
+class ManyToManyElementBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ ManyToManyElementBinder binder
+
+ void setupSpec() {
+ manager.addAllDomainClasses([
+ MTMEOwner,
+ MTMEItem,
+ MTMEBase,
+ MTMESubtype,
+ ])
+ }
+
+ void setup() {
+ def gdb = getGrailsDomainBinder()
+ def mbc = gdb.getMetadataBuildingContext()
+ def ns = gdb.getNamingStrategy()
+ def je = gdb.getJdbcEnvironment()
+ def svb = new SimpleValueBinder(mbc, ns, je)
+ def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
+ def mtob = new ManyToOneBinder(mbc, ns, svb, new ManyToOneValuesBinder(), citmto)
+ binder = new ManyToManyElementBinder(mtob, new CollectionForPropertyConfigBinder())
+ }
+
+ private HibernateManyToManyProperty propertyFor(Class ownerClass, String name = "items") {
+ (getPersistentEntity(ownerClass) as GrailsHibernatePersistentEntity).getPropertyByName(name) as HibernateManyToManyProperty
+ }
+
+ def "bind sets ManyToOne element referencing the inverse owner for a standard bidirectional many-to-many"() {
+ given:
+ def property = propertyFor(MTMEOwner)
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def collection = new Bag(mbc, null)
+ collection.setCollectionTable(new Table("test", "mtme_owner_mtme_item"))
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getElement() instanceof ManyToOne
+ (collection.getElement() as ManyToOne).getReferencedEntityName() == MTMEItem.name
+ }
+
+ def "bind sets collection inverse false for a circular many-to-many"() {
+ given:
+ def property = propertyFor(MTMESubtype, "related")
+ def mbc = getGrailsDomainBinder().getMetadataBuildingContext()
+ def collection = new Bag(mbc, null)
+ collection.setCollectionTable(new Table("test", "mtme_subtype_mtme_base"))
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ property.isCircular()
+ !collection.isInverse()
+ }
+}
+
+@Entity
+class MTMEOwner {
+ Long id
+ static hasMany = [items: MTMEItem]
+}
+
+@Entity
+class MTMEItem {
+ Long id
+ String description
+ static hasMany = [owners: MTMEOwner]
+}
+
+@Entity
+class MTMEBase {
+ Long id
+ static hasMany = [subtypes: MTMESubtype]
+}
+
+@Entity
+class MTMESubtype extends MTMEBase {
+ static hasMany = [related: MTMEBase]
+}
(grails-core) 12/24: refactor CollectionSecondPassBinder
Posted by bo...@apache.org.
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 dfc567747ece06fc08bc59e64f1f9e822786f58b
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 09:02:27 2026 -0600
refactor CollectionSecondPassBinder
---
.../secondpass/CollectionSecondPassBinder.java | 66 +++++++++++++---------
.../CollectionSecondPassBinderSpec.groovy | 41 +++++++-------
2 files changed, 60 insertions(+), 47 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 6b705fa4d8..27077eb16f 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
@@ -34,6 +34,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyPr
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder;
import org.hibernate.MappingException;
@@ -93,7 +94,8 @@ public class CollectionSecondPassBinder {
Map<?, ?> persistentClasses,
@Nonnull Collection collection) {
- PersistentClass associatedClass = bindOrderBy(property, collection, persistentClasses);
+ PersistentClass associatedClass = resolveAssociatedClass(property, persistentClasses);
+ bindOrderBy(property, collection, associatedClass);
bindOneToManyAssociation(property, associatedClass, collection);
applyMultiTenantFilter(property, collection);
@@ -217,44 +219,52 @@ public class CollectionSecondPassBinder {
collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
}
- PersistentClass bindOrderBy(
- HibernateToManyProperty property, Collection collection, Map<?, ?> persistentClasses) {
+ private PersistentClass resolveAssociatedClass(
+ HibernateToManyProperty property, Map<?, ?> persistentClasses) {
return Optional.ofNullable(property.getHibernateAssociatedEntity())
.map(
referenced -> {
- if (referenced.isTablePerHierarchySubclass()) {
- String discriminatorColumnName = referenced.getDiscriminatorColumnName();
- Set<String> discSet = referenced.buildDiscriminatorSet();
- String inclause = String.join(",", discSet);
-
- collection.setWhere(discriminatorColumnName + " in (" + inclause + ")");
- }
-
PersistentClass associatedClass =
(PersistentClass) persistentClasses.get(referenced.getName());
if (associatedClass == null) {
throw new MappingException(
"Association references unmapped class: " + referenced.getName());
}
-
- if (property.hasSort()) {
- if (!property.isBidirectional() && property instanceof HibernateOneToManyProperty) {
- throw new DatastoreConfigurationException(
- "Default sort for associations ["
- + property.getHibernateOwner().getName()
- + "->"
- + property.getName()
- + "] are not supported with unidirectional one to many relationships.");
- }
- HibernatePersistentProperty sortBy =
- (HibernatePersistentProperty) referenced.getPropertyByName(property.getSort());
- String order = Optional.ofNullable(property.getOrder()).orElse("asc");
- collection.setOrderBy(
- orderByClauseBuilder.buildOrderByClause(
- sortBy.getName(), associatedClass, collection.getRole(), order));
- }
return associatedClass;
})
.orElse(null);
}
+
+ private void bindOrderBy(
+ HibernateToManyProperty property, Collection collection, PersistentClass associatedClass) {
+ if (associatedClass == null) {
+ return;
+ }
+ GrailsHibernatePersistentEntity referenced = property.getHibernateAssociatedEntity();
+
+ if (referenced.isTablePerHierarchySubclass()) {
+ String discriminatorColumnName = referenced.getDiscriminatorColumnName();
+ Set<String> discSet = referenced.buildDiscriminatorSet();
+ String inclause = String.join(",", discSet);
+
+ collection.setWhere(discriminatorColumnName + " in (" + inclause + ")");
+ }
+
+ if (property.hasSort()) {
+ if (!property.isBidirectional() && property instanceof HibernateOneToManyProperty) {
+ throw new DatastoreConfigurationException(
+ "Default sort for associations ["
+ + property.getHibernateOwner().getName()
+ + "->"
+ + property.getName()
+ + "] are not supported with unidirectional one to many relationships.");
+ }
+ HibernatePersistentProperty sortBy =
+ (HibernatePersistentProperty) referenced.getPropertyByName(property.getSort());
+ String order = Optional.ofNullable(property.getOrder()).orElse("asc");
+ collection.setOrderBy(
+ orderByClauseBuilder.buildOrderByClause(
+ sortBy.getName(), associatedClass, collection.getRole(), order));
+ }
+ }
}
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 76c011d992..d4501b62a3 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
@@ -58,86 +58,89 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
collection.setRole("CSPBTestEntityWithMany.items")
- def persistentClasses = [:]
def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
def valueProperty = new org.hibernate.mapping.Property()
valueProperty.setName("value")
- def simpleValue = new org.hibernate.mapping.BasicValue(getGrailsDomainBinder().getMetadataBuildingContext(), new org.hibernate.mapping.Table("ASSOCIATED_ITEM"))
+ def simpleValue = new org.hibernate.mapping.BasicValue(getGrailsDomainBinder().getMetadataBuildingContext(), new org.hibernate.mapping.Table("test", "ASSOCIATED_ITEM"))
simpleValue.setTypeName("string")
def column = new org.hibernate.mapping.Column("VALUE")
simpleValue.addColumn(column)
valueProperty.setValue(simpleValue)
associatedPersistentClass.addProperty(valueProperty)
- persistentClasses[CSPBAssociatedItem.name] = associatedPersistentClass
property.getMappedForm().setSort("value")
property.getMappedForm().setOrder("desc")
when:
- def result = binder.bindOrderBy(property, collection, persistentClasses)
+ binder.bindOrderBy(property, collection, associatedPersistentClass)
then:
collection.getOrderBy() != null
- result == associatedPersistentClass
}
def "test bindOrderBy with unidirectional one-to-many throws exception"() {
given:
def property = createTestHibernateToManyProperty(CSPBUnidirectionalEntity, "items") as HibernateToManyProperty
def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def persistentClasses = [:]
def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
- persistentClasses[CSPBAssociatedItem.name] = associatedPersistentClass
property.getMappedForm().setSort("value")
when:
- binder.bindOrderBy(property, collection, persistentClasses)
+ binder.bindOrderBy(property, collection, associatedPersistentClass)
then:
thrown(DatastoreConfigurationException)
}
- def "test bindOrderBy returns associatedClass even without sort"() {
+ def "test bindOrderBy does nothing without sort"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def persistentClasses = [:]
def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
- persistentClasses[CSPBAssociatedItem.name] = associatedPersistentClass
when:
- def result = binder.bindOrderBy(property, collection, persistentClasses)
+ binder.bindOrderBy(property, collection, associatedPersistentClass)
then:
collection.getOrderBy() == null
- result == associatedPersistentClass
}
- def "test bindOrderBy throws MappingException when class is unmapped"() {
+ def "test resolveAssociatedClass throws MappingException when class is unmapped"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
- def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
def persistentClasses = [:] // Empty map, so CSPBAssociatedItem will be missing
when:
- binder.bindOrderBy(property, collection, persistentClasses)
+ binder.resolveAssociatedClass(property, persistentClasses)
then:
thrown(org.hibernate.MappingException)
}
+ def "test resolveAssociatedClass returns associatedClass"() {
+ given:
+ def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
+ def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
+ associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
+ def persistentClasses = [(CSPBAssociatedItem.name): associatedPersistentClass]
+
+ when:
+ def result = binder.resolveAssociatedClass(property, persistentClasses)
+
+ then:
+ result == associatedPersistentClass
+ }
+
def "test bindOrderBy with table per hierarchy subclass"() {
given:
def property = createTestHibernateToManyProperty(CSPBTestEntityWithMany, "items") as HibernateToManyProperty
def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
- def persistentClasses = [:]
def associatedPersistentClass = new RootClass(getGrailsDomainBinder().getMetadataBuildingContext())
associatedPersistentClass.setEntityName(CSPBAssociatedItem.name)
- persistentClasses[CSPBAssociatedItem.name] = associatedPersistentClass
// Mock GrailsHibernatePersistentEntity behavior for table per hierarchy
def referencedEntity = property.getHibernateAssociatedEntity()
@@ -151,7 +154,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
spiedProperty.getHibernateAssociatedEntity() >> spiedReferencedEntity
when:
- binder.bindOrderBy(spiedProperty, collection, persistentClasses)
+ binder.bindOrderBy(spiedProperty, collection, associatedPersistentClass)
then:
collection.getWhere() == "item_type in ('A','B')"
(grails-core) 09/24: refactor: replace isCompositeIdentifier with Optional in ManyToOneBinder
Posted by bo...@apache.org.
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 9153fed8ad837c89ff611eaf52f5676070bb7572
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 13:42:34 2026 -0600
refactor: replace isCompositeIdentifier with Optional<CompositeIdentity> in ManyToOneBinder
---
.../binder/ForeignKeyOneToOneBinder.java | 6 ++--
.../cfg/domainbinding/binder/ManyToOneBinder.java | 38 +++++++++-------------
.../ForeignKeyOneToOneBinderSpec.groovy | 3 ++
.../cfg/domainbinding/ManyToOneBinderSpec.groovy | 5 ++-
4 files changed, 24 insertions(+), 28 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java
index ef45844a18..5e1163f6ba 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ForeignKeyOneToOneBinder.java
@@ -51,10 +51,8 @@ public class ForeignKeyOneToOneBinder {
public ManyToOne bind(
HibernateOneToOneProperty property, org.hibernate.mapping.Table table, String path) {
GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
- boolean isComposite = ManyToOneBinder.isCompositeIdentifier(refDomainClass);
- ManyToOne manyToOne =
- manyToOneBinder.doBind(property, refDomainClass, isComposite, table, path);
- if (!isComposite) {
+ ManyToOne manyToOne = manyToOneBinder.doBind(property, refDomainClass, table, path);
+ if (refDomainClass.getHibernateCompositeIdentity().isEmpty()) {
bindUniqueKey(property, manyToOne);
}
return manyToOne;
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index 7e256ff33f..ab3dbba1b5 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -20,12 +20,12 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.FOREIGN_KEY_SUFFIX;
+import java.util.Optional;
import org.grails.orm.hibernate.cfg.ColumnConfig;
import org.grails.orm.hibernate.cfg.CompositeIdentity;
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;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
@@ -72,57 +72,49 @@ public class ManyToOneBinder {
/** Binds a many-to-one association. */
public ManyToOne bindManyToOne(
HibernateManyToOneProperty property, org.hibernate.mapping.Table table, String path) {
- GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
- return doBind(property, refDomainClass, isCompositeIdentifier(refDomainClass), table, path);
+ return doBind(property, property.getHibernateAssociatedEntity(), table, path);
}
/** Binds the inverse side of a many-to-many association as a collection element. */
public ManyToOne bindManyToOne(
HibernateManyToManyProperty property, org.hibernate.mapping.Table table, String path) {
GrailsHibernatePersistentEntity refDomainClass = property.getHibernateOwner();
- boolean isComposite = isCompositeIdentifier(refDomainClass);
- if (!isComposite && property.isCircular()) {
- prepareCircularManyToMany(property, refDomainClass.getMappedForm());
+ Optional<CompositeIdentity> compositeId = refDomainClass.getHibernateCompositeIdentity();
+ if (compositeId.isEmpty() && property.isCircular()) {
+ prepareCircularManyToMany(property);
}
- return doBind(property, refDomainClass, isComposite, table, path);
- }
-
- static boolean isCompositeIdentifier(GrailsHibernatePersistentEntity entity) {
- Mapping mapping = entity.getMappedForm();
- return mapping != null && mapping.hasCompositeIdentifier();
+ return doBind(property, refDomainClass, table, path);
}
ManyToOne doBind(
HibernateAssociation property,
GrailsHibernatePersistentEntity refDomainClass,
- boolean isComposite,
org.hibernate.mapping.Table table,
String path) {
ManyToOne manyToOne = new ManyToOne(metadataBuildingContext, table);
manyToOneValuesBinder.bindManyToOneValues(property, manyToOne);
- if (isComposite) {
- Mapping mapping = refDomainClass.getMappedForm();
- CompositeIdentity ci = (CompositeIdentity) mapping.getIdentity();
+ Optional<CompositeIdentity> compositeId = refDomainClass.getHibernateCompositeIdentity();
+ if (compositeId.isPresent()) {
compositeIdentifierToManyToOneBinder.bindCompositeIdentifierToManyToOne(
- property, manyToOne, ci, refDomainClass, path);
+ property, manyToOne, compositeId.get(), refDomainClass, path);
} else {
simpleValueBinder.bindSimpleValue(property, null, manyToOne, path);
}
return manyToOne;
}
- private void prepareCircularManyToMany(HibernateManyToManyProperty property, Mapping mapping) {
- PropertyConfig pc = property.getMappedForm();
- if (mapping != null && pc.getColumns().isEmpty()) {
- mapping.getColumns().put(property.getName(), pc);
+ private void prepareCircularManyToMany(HibernateManyToManyProperty property) {
+ Mapping ownerMapping = property.getHibernateOwner().getMappedForm();
+ if (ownerMapping != null && !ownerMapping.getColumns().containsKey(property.getName())) {
+ ownerMapping.getColumns().put(property.getName(), property.getMappedForm());
}
- if (!pc.hasJoinKeyMapping()) {
+ if (!property.getMappedForm().hasJoinKeyMapping()) {
JoinTable jt = new JoinTable();
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setName(
namingStrategy.resolveColumnName(property.getName()) + FOREIGN_KEY_SUFFIX);
jt.setKey(columnConfig);
- pc.setJoinTable(jt);
+ property.getMappedForm().setJoinTable(jt);
}
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy
index bfcadc3367..c016a3e22c 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ForeignKeyOneToOneBinderSpec.groovy
@@ -22,6 +22,7 @@ import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.orm.hibernate.cfg.Mapping
+import java.util.Optional
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.grails.orm.hibernate.cfg.PropertyConfig
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
@@ -58,6 +59,7 @@ class ForeignKeyOneToOneBinderSpec extends HibernateGormDatastoreSpec {
def mapping = new Mapping()
def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.empty()
}
def propertyConfig = Mock(PropertyConfig)
def column = new Column('test')
@@ -111,6 +113,7 @@ class ForeignKeyOneToOneBinderSpec extends HibernateGormDatastoreSpec {
def mapping = new Mapping()
def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.empty()
}
def propertyConfig = new PropertyConfig()
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 15a7bbae11..2d98f6d595 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
@@ -5,6 +5,7 @@ import org.grails.orm.hibernate.cfg.CompositeIdentity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
import org.grails.orm.hibernate.cfg.Mapping
+import java.util.Optional
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
import org.grails.orm.hibernate.cfg.PropertyConfig
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty
@@ -33,14 +34,15 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
def association = Mock(HibernateManyToOneProperty)
def path = "/test"
def mapping = new Mapping()
+ mapping.setIdentity(hasCompositeId ? new CompositeIdentity() : null)
def refDomainClass = Mock(GrailsHibernatePersistentEntity) {
getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.ofNullable(mapping.hasCompositeIdentifier() ? (CompositeIdentity) mapping.getIdentity() : null)
}
def propertyConfig = new PropertyConfig()
association.getHibernateAssociatedEntity() >> refDomainClass
association.getMappedForm() >> propertyConfig
- mapping.setIdentity(hasCompositeId ? new CompositeIdentity() : null)
when:
def result = binder.bindManyToOne(association, null, path)
@@ -71,6 +73,7 @@ class ManyToOneBinderSpec extends HibernateGormDatastoreSpec {
mapping.setColumns(new HashMap<String, PropertyConfig>())
def ownerEntity = Mock(GrailsHibernatePersistentEntity) {
getMappedForm() >> mapping
+ getHibernateCompositeIdentity() >> Optional.empty()
}
def propertyConfig = new PropertyConfig()
(grails-core) 20/24: refactor: HibernateToManyProperty.getCacheUSage
Posted by bo...@apache.org.
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 b893b4348ab78b768c6c6aa6e3d172f8a3bdd1d1
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 13:30:24 2026 -0600
refactor: HibernateToManyProperty.getCacheUSage
---
.../cfg/domainbinding/hibernate/HibernateToManyProperty.java | 10 ++++++++++
.../domainbinding/secondpass/CollectionSecondPassBinder.java | 4 +---
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
index 315728b16d..ea6d8d38ac 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
@@ -19,8 +19,11 @@
package org.grails.orm.hibernate.cfg.domainbinding.hibernate;
import java.util.Map;
+import java.util.Optional;
+
import org.grails.datastore.mapping.model.types.Basic;
import org.grails.datastore.mapping.model.types.mapping.PropertyWithMapping;
+import org.grails.orm.hibernate.cfg.CacheConfig;
import org.grails.orm.hibernate.cfg.ColumnConfig;
import org.grails.orm.hibernate.cfg.JoinTable;
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
@@ -58,6 +61,13 @@ public interface HibernateToManyProperty
return getMappedForm().getLazy();
}
+ default String getCacheUSage() {
+ return Optional.ofNullable(getMappedForm())
+ .map(PropertyConfig::getCache)
+ .map(CacheConfig::getUsage)
+ .orElse(null);
+ }
+
/**
* @return Whether the collection should be bound with a foreign key
*/
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 6ee0f04118..4507e52732 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
@@ -80,9 +80,7 @@ public class CollectionSecondPassBinder {
collection.setSorted(property.isSorted());
DependantValue key = collectionKeyBinder.bind(property, associatedClass, collection);
-
- Optional.ofNullable(property.getMappedForm().getCache())
- .ifPresent(cacheConfig -> collection.setCacheConcurrencyStrategy(cacheConfig.getUsage()));
+ collection.setCacheConcurrencyStrategy(property.getCacheUSage());
bindCollectionElement(property, mappings, collection);
collectionKeyColumnUpdater.forceNullableAndCheckUpdatable(key, property);
(grails-core) 10/24: refactor: CompositeIdentifierToManyToOneBinder
Posted by bo...@apache.org.
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 f71eac0999a3d8bae37c830042979d776229077d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 14:36:25 2026 -0600
refactor: CompositeIdentifierToManyToOneBinder
---
.../CompositeIdentifierToManyToOneBinder.java | 196 ++++++++++-----------
.../hibernate/GrailsHibernatePersistentEntity.java | 9 +
...CompositeIdentifierToManyToOneBinderSpec.groovy | 18 +-
3 files changed, 111 insertions(+), 112 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdentifierToManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdentifierToManyToOneBinder.java
index aa485a9740..785403f21d 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdentifierToManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CompositeIdentifierToManyToOneBinder.java
@@ -20,130 +20,124 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.UNDERSCORE;
+import java.util.Arrays;
import java.util.List;
-import org.grails.datastore.mapping.model.PersistentEntity;
-import org.grails.datastore.mapping.model.PersistentProperty;
-import org.grails.datastore.mapping.model.types.ToOne;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
import org.grails.orm.hibernate.cfg.ColumnConfig;
import org.grails.orm.hibernate.cfg.CompositeIdentity;
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover;
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.grails.orm.hibernate.cfg.domainbinding.util.ForeignKeyColumnCountCalculator;
+
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.mapping.SimpleValue;
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class CompositeIdentifierToManyToOneBinder {
- private final ForeignKeyColumnCountCalculator foreignKeyColumnCountCalculator;
- private final PersistentEntityNamingStrategy namingStrategy;
- private final DefaultColumnNameFetcher defaultColumnNameFetcher;
- private final BackticksRemover backticksRemover;
- private final SimpleValueBinder simpleValueBinder;
- public CompositeIdentifierToManyToOneBinder(
- ForeignKeyColumnCountCalculator foreignKeyColumnCountCalculator,
- PersistentEntityNamingStrategy namingStrategy,
- DefaultColumnNameFetcher defaultColumnNameFetcher,
- BackticksRemover backticksRemover,
- SimpleValueBinder simpleValueBinder) {
- this.foreignKeyColumnCountCalculator = foreignKeyColumnCountCalculator;
- this.namingStrategy = namingStrategy;
- this.defaultColumnNameFetcher = defaultColumnNameFetcher;
- this.backticksRemover = backticksRemover;
- this.simpleValueBinder = simpleValueBinder;
- }
+ private final ForeignKeyColumnCountCalculator foreignKeyColumnCountCalculator;
+ private final PersistentEntityNamingStrategy namingStrategy;
+ private final DefaultColumnNameFetcher defaultColumnNameFetcher;
+ private final BackticksRemover backticksRemover;
+ private final SimpleValueBinder simpleValueBinder;
- public CompositeIdentifierToManyToOneBinder(
- MetadataBuildingContext metadataBuildingContext,
- PersistentEntityNamingStrategy namingStrategy,
- JdbcEnvironment jdbcEnvironment) {
- this(
- new ForeignKeyColumnCountCalculator(),
- namingStrategy,
- new DefaultColumnNameFetcher(namingStrategy),
- new BackticksRemover(),
- new SimpleValueBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment));
- }
+ public CompositeIdentifierToManyToOneBinder(
+ ForeignKeyColumnCountCalculator foreignKeyColumnCountCalculator,
+ PersistentEntityNamingStrategy namingStrategy,
+ DefaultColumnNameFetcher defaultColumnNameFetcher,
+ BackticksRemover backticksRemover,
+ SimpleValueBinder simpleValueBinder) {
+ this.foreignKeyColumnCountCalculator = foreignKeyColumnCountCalculator;
+ this.namingStrategy = namingStrategy;
+ this.defaultColumnNameFetcher = defaultColumnNameFetcher;
+ this.backticksRemover = backticksRemover;
+ this.simpleValueBinder = simpleValueBinder;
+ }
- public void bindCompositeIdentifierToManyToOne(
- HibernatePersistentProperty property,
- SimpleValue value,
- CompositeIdentity compositeId,
- PersistentEntity refDomainClass,
- String path) {
- String[] propertyNames = compositeId.getPropertyNames();
+ public CompositeIdentifierToManyToOneBinder(
+ MetadataBuildingContext metadataBuildingContext,
+ PersistentEntityNamingStrategy namingStrategy,
+ JdbcEnvironment jdbcEnvironment) {
+ this(
+ new ForeignKeyColumnCountCalculator(),
+ namingStrategy,
+ new DefaultColumnNameFetcher(namingStrategy),
+ new BackticksRemover(),
+ new SimpleValueBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment));
+ }
- List<ColumnConfig> columns = property.getMappedForm().getColumns();
- int i = columns.size();
- int expectedForeignKeyColumnLength =
- foreignKeyColumnCountCalculator.calculateForeignKeyColumnCount(
- refDomainClass, propertyNames);
- if (i != expectedForeignKeyColumnLength) {
- int j = 0;
- for (String propertyName : propertyNames) {
- final ColumnConfig cc;
- // if a column configuration exists in the mapping use it
- if (j < i) {
- cc = columns.get(j++);
+ public void bindCompositeIdentifierToManyToOne(
+ HibernatePersistentProperty property,
+ SimpleValue value,
+ CompositeIdentity compositeId,
+ GrailsHibernatePersistentEntity refDomainClass,
+ String path) {
+ String[] propertyNames = compositeId.getPropertyNames();
+ List<ColumnConfig> columns = property.getMappedForm().getColumns();
+ int existingCount = columns.size();
+ if (existingCount != foreignKeyColumnCountCalculator.calculateForeignKeyColumnCount(refDomainClass, propertyNames)) {
+ String prefix = refDomainClass.getTableName(namingStrategy);
+ IntStream.range(0, propertyNames.length)
+ .boxed()
+ .flatMap(idx -> {
+ ColumnConfig cc = idx < existingCount ? columns.get(idx) : new ColumnConfig();
+ if (cc.getName() != null) {
+ return Stream.empty();
+ }
+ String propertyName = propertyNames[idx];
+ HibernatePersistentProperty ref = refDomainClass.getHibernatePropertyByName(propertyName);
+ return tryExpandNestedComposite(prefix, propertyName, ref)
+ .orElseGet(() -> singleColumn(prefix, propertyName, ref, cc));
+ })
+ .forEach(columns::add);
}
- // otherwise create a new one to represent the composite column
- else {
- cc = new ColumnConfig();
+ simpleValueBinder.bindSimpleValue(property, null, value, path);
+ }
+
+ /**
+ * If {@code ref} is a to-one whose associated entity has a composite identity, returns a stream
+ * of one named {@link ColumnConfig} per composite-identity property. Returns empty otherwise.
+ */
+ private Optional<Stream<ColumnConfig>> tryExpandNestedComposite(
+ String prefix, String propertyName, HibernatePersistentProperty ref) {
+ if (!(ref instanceof HibernateToOneProperty toOne)) {
+ return Optional.empty();
}
- // if the name is null then configure the name by convention
- if (cc.getName() == null) {
- // use the referenced table name as a prefix
- String prefix =
- refDomainClass instanceof GrailsHibernatePersistentEntity ghpe
- ? ghpe.getTableName(namingStrategy)
- : refDomainClass.getName();
- PersistentProperty referencedProperty = refDomainClass.getPropertyByName(propertyName);
+ HibernatePersistentProperty[] nestedComposite =
+ toOne.getHibernateAssociatedEntity().getCompositeIdentity();
+ if (nestedComposite == null) {
+ return Optional.empty();
+ }
+ return Optional.of(Arrays.stream(nestedComposite)
+ .map(cip -> namedColumn(join(prefix, namingStrategy.resolveColumnName(propertyName),
+ defaultColumnNameFetcher.getDefaultColumnName(cip)))));
+ }
- // if the referenced property is a ToOne and it has a composite id
- // then a column is needed for each property that forms the composite id
- if (referencedProperty instanceof ToOne toOne) {
- PersistentProperty[] compositeIdentity =
- toOne.getAssociatedEntity().getCompositeIdentity();
- if (compositeIdentity != null) {
- for (PersistentProperty cip : compositeIdentity) {
- // for each property of a composite id by default we use the table name and the
- // property name as a prefix
- String string = namingStrategy.resolveColumnName(referencedProperty.getName());
- String compositeIdPrefix =
- backticksRemover.apply(prefix) + UNDERSCORE + backticksRemover.apply(string);
+ private Stream<ColumnConfig> singleColumn(
+ String prefix, String propertyName, HibernatePersistentProperty ref, ColumnConfig cc) {
+ String suffix = ref != null
+ ? defaultColumnNameFetcher.getDefaultColumnName(ref)
+ : propertyName;
+ cc.setName(join(prefix, suffix));
+ return Stream.of(cc);
+ }
- String suffix =
- cip instanceof HibernatePersistentProperty ghpp
- ? defaultColumnNameFetcher.getDefaultColumnName(ghpp)
- : cip.getName();
- String finalColumnName =
- backticksRemover.apply(compositeIdPrefix)
- + UNDERSCORE
- + backticksRemover.apply(suffix);
- ColumnConfig newCc = new ColumnConfig();
- newCc.setName(finalColumnName);
- columns.add(newCc);
- }
- continue;
- }
- }
+ private ColumnConfig namedColumn(String name) {
+ ColumnConfig cc = new ColumnConfig();
+ cc.setName(name);
+ return cc;
+ }
- String suffix =
- referencedProperty instanceof HibernatePersistentProperty ghpp
- ? defaultColumnNameFetcher.getDefaultColumnName(ghpp)
- : referencedProperty.getName();
- String finalColumnName =
- backticksRemover.apply(prefix) + UNDERSCORE + backticksRemover.apply(suffix);
- cc.setName(finalColumnName);
- columns.add(cc);
- }
- }
+ private String join(String... parts) {
+ return Arrays.stream(parts).map(backticksRemover::apply).collect(Collectors.joining(String.valueOf(UNDERSCORE)));
}
- // set type
- simpleValueBinder.bindSimpleValue(property, null, value, path);
- }
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/GrailsHibernatePersistentEntity.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/GrailsHibernatePersistentEntity.java
index 20ac8e2c7b..0337ab1acb 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/GrailsHibernatePersistentEntity.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/GrailsHibernatePersistentEntity.java
@@ -156,6 +156,15 @@ public interface GrailsHibernatePersistentEntity extends PersistentEntity {
@Override
HibernatePersistentProperty getVersion();
+ /**
+ * Returns the persistent property with the given name cast to {@link HibernatePersistentProperty},
+ * or {@code null} if no such property exists.
+ */
+ default HibernatePersistentProperty getHibernatePropertyByName(String name) {
+ var property = getPropertyByName(name);
+ return property instanceof HibernatePersistentProperty hpp ? hpp : null;
+ }
+
/**
* @param parentType The type of the parent entity
* @return The parent property if it exists
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdentifierToManyToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdentifierToManyToOneBinderSpec.groovy
index ca337b414b..71cc4ed150 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdentifierToManyToOneBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CompositeIdentifierToManyToOneBinderSpec.groovy
@@ -1,8 +1,7 @@
package org.grails.orm.hibernate.cfg.domainbinding
-import org.grails.datastore.mapping.model.PersistentProperty
-import org.grails.datastore.mapping.model.types.ToOne
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty
import org.grails.orm.hibernate.cfg.ColumnConfig
import org.grails.orm.hibernate.cfg.CompositeIdentity
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
@@ -34,8 +33,7 @@ class CompositeIdentifierToManyToOneBinderSpec extends Specification {
def binder = new CompositeIdentifierToManyToOneBinder(calculator, namingStrategy, columnNameFetcher, backticksRemover, simpleValueBinder)
// 2. Set up stubs for the method arguments
- def association = Mock(ToOne)
- association.asType(HibernatePersistentProperty) >> association
+ def association = Mock(HibernatePersistentProperty)
def value = Mock(SimpleValue)
def refDomainClass = Mock(GrailsHibernatePersistentEntity)
def path = "/test"
@@ -51,17 +49,16 @@ class CompositeIdentifierToManyToOneBinderSpec extends Specification {
calculator.calculateForeignKeyColumnCount(refDomainClass, propertyNames) >> 2
- def nestedEntityProp = Mock(ToOne)
- nestedEntityProp.asType(HibernatePersistentProperty) >> nestedEntityProp
- refDomainClass.getPropertyByName("nestedEntity") >> nestedEntityProp
+ def nestedEntityProp = Mock(HibernateToOneProperty)
+ refDomainClass.getHibernatePropertyByName("nestedEntity") >> nestedEntityProp
nestedEntityProp.name >> "nestedEntity"
def nestedAssociatedEntity = Mock(GrailsHibernatePersistentEntity)
- nestedEntityProp.getAssociatedEntity() >> nestedAssociatedEntity
+ nestedEntityProp.getHibernateAssociatedEntity() >> nestedAssociatedEntity
def nestedPartA = Mock(HibernatePersistentProperty)
def nestedPartB = Mock(HibernatePersistentProperty)
- def perArray = [nestedPartA, nestedPartB] as PersistentProperty[]
+ def perArray = [nestedPartA, nestedPartB] as HibernatePersistentProperty[]
nestedAssociatedEntity.getCompositeIdentity() >> perArray
// 4. Mock the behavior of the dependency methods
@@ -100,8 +97,7 @@ class CompositeIdentifierToManyToOneBinderSpec extends Specification {
def binder = new CompositeIdentifierToManyToOneBinder(calculator, namingStrategy, columnNameFetcher, backticksRemover, simpleValueBinder)
// 2. Set up arguments
- def association = Mock(ToOne)
- association.asType(HibernatePersistentProperty) >> association
+ def association = Mock(HibernatePersistentProperty)
def value = Mock(SimpleValue)
def compositeId = new CompositeIdentity()
compositeId.setPropertyNames(["prop1", "prop2"] as String[])
(grails-core) 23/24: refactor: move ManyToOne element creation into UnidirectionalOneToManyInverseValuesBinder
Posted by bo...@apache.org.
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 244a595487a10b003f6760e3235e2f19641c079d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 16:04:02 2026 -0600
refactor: move ManyToOne element creation into UnidirectionalOneToManyInverseValuesBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 3 +--
.../secondpass/CollectionWithJoinTableBinder.java | 9 +--------
.../UnidirectionalOneToManyInverseValuesBinder.java | 15 ++++++++++++---
.../secondpass/CollectionSecondPassBinderSpec.groovy | 2 +-
.../CollectionWithJoinTableBinderSpec.groovy | 4 +++-
.../UnidirectionalOneToManyBinderSpec.groovy | 3 +--
...irectionalOneToManyInverseValuesBinderSpec.groovy | 20 ++++++++++++++------
7 files changed, 33 insertions(+), 23 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 71e1bf5fb3..adea9b6d82 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -96,11 +96,10 @@ public class CollectionBinder {
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder =
new CollectionForPropertyConfigBinder();
UnidirectionalOneToManyInverseValuesBinder unidirectionalOneToManyInverseValuesBinder =
- new UnidirectionalOneToManyInverseValuesBinder();
+ new UnidirectionalOneToManyInverseValuesBinder(metadataBuildingContext);
SimpleValueColumnBinder simpleValueColumnBinder = new SimpleValueColumnBinder();
CollectionWithJoinTableBinder collectionWithJoinTableBinder =
new CollectionWithJoinTableBinder(
- metadataBuildingContext,
namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
compositeIdentifierToManyToOneBinder,
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
index e880b21c56..e83dea0456 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
@@ -28,15 +28,12 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToMa
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.hibernate.boot.spi.InFlightMetadataCollector;
-import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.*;
-import org.hibernate.mapping.Collection;
/** Binds a collection with a join table. */
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public class CollectionWithJoinTableBinder {
- private final MetadataBuildingContext metadataBuildingContext;
private final PersistentEntityNamingStrategy namingStrategy;
private final UnidirectionalOneToManyInverseValuesBinder
unidirectionalOneToManyInverseValuesBinder;
@@ -47,14 +44,12 @@ public class CollectionWithJoinTableBinder {
/** Creates a new {@link CollectionWithJoinTableBinder} instance. */
public CollectionWithJoinTableBinder(
- MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
UnidirectionalOneToManyInverseValuesBinder unidirectionalOneToManyInverseValuesBinder,
CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
SimpleValueColumnBinder simpleValueColumnBinder,
BasicCollectionElementBinder basicCollectionElementBinder) {
- this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.unidirectionalOneToManyInverseValuesBinder = unidirectionalOneToManyInverseValuesBinder;
this.compositeIdentifierToManyToOneBinder = compositeIdentifierToManyToOneBinder;
@@ -74,9 +69,7 @@ public class CollectionWithJoinTableBinder {
if (property.isBasic()) {
element = basicCollectionElementBinder.bind(property, collection);
} else {
- element = new ManyToOne(metadataBuildingContext, collection.getCollectionTable());
- unidirectionalOneToManyInverseValuesBinder.bindUnidirectionalOneToManyInverseValues(
- property, (ManyToOne) element);
+ element = unidirectionalOneToManyInverseValuesBinder.bind(property, collection);
final var domainClass = property.getHibernateAssociatedEntity();
if (domainClass != null) {
var joinColumnMappingOptional =
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinder.java
index a9b188fae9..1f8684e35e 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinder.java
@@ -21,16 +21,25 @@ package org.grails.orm.hibernate.cfg.domainbinding.secondpass;
import java.util.Optional;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.hibernate.FetchMode;
+import org.hibernate.boot.spi.MetadataBuildingContext;
+import org.hibernate.mapping.Collection;
import org.hibernate.mapping.ManyToOne;
-/** Binds inverse values for unidirectional one-to-many associations. */
+/** Creates and binds a {@link ManyToOne} element for unidirectional to-many join-table associations. */
public class UnidirectionalOneToManyInverseValuesBinder {
- public void bindUnidirectionalOneToManyInverseValues(
- HibernateToManyProperty property, ManyToOne manyToOne) {
+ private final MetadataBuildingContext metadataBuildingContext;
+
+ public UnidirectionalOneToManyInverseValuesBinder(MetadataBuildingContext metadataBuildingContext) {
+ this.metadataBuildingContext = metadataBuildingContext;
+ }
+
+ public ManyToOne bind(HibernateToManyProperty property, Collection collection) {
+ ManyToOne manyToOne = new ManyToOne(metadataBuildingContext, collection.getCollectionTable());
manyToOne.setIgnoreNotFound(property.getIgnoreNotFound());
manyToOne.setLazy(!FetchMode.JOIN.equals(property.getFetchMode()));
Optional.ofNullable(property.getLazy()).ifPresent(manyToOne::setLazy);
manyToOne.setReferencedEntityName(property.getHibernateAssociatedEntity().getName());
+ return manyToOne;
}
}
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 2cd4ee88dc..66c88059fe 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
@@ -34,7 +34,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def pkvc = new PrimaryKeyValueCreator(mbc)
def botml = new BidirectionalOneToManyLinker(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver())
def dkvb = new DependentKeyValueBinder(svb, citmto)
- def cwjtb = new CollectionWithJoinTableBinder(mbc, ns, new UnidirectionalOneToManyInverseValuesBinder(), citmto, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), new BasicCollectionElementBinder(mbc, ns, null, new SimpleValueColumnBinder(), svcf, null))
+ def cwjtb = new CollectionWithJoinTableBinder(ns, new UnidirectionalOneToManyInverseValuesBinder(mbc), citmto, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), new BasicCollectionElementBinder(mbc, ns, null, new SimpleValueColumnBinder(), svcf, null))
def uotmb = new UnidirectionalOneToManyBinder(cwjtb, mbc.getMetadataCollector())
def cfpcb = new CollectionForPropertyConfigBinder()
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
index abc62e0ebd..1aa0ab2809 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinderSpec.groovy
@@ -29,7 +29,6 @@ class CollectionWithJoinTableBinderSpec extends HibernateGormDatastoreSpec {
void setup() {
def domainBinder = getGrailsDomainBinder()
binder = new CollectionWithJoinTableBinder(
- domainBinder.metadataBuildingContext,
domainBinder.namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
compositeIdentifierToManyToOneBinder,
@@ -76,6 +75,9 @@ class CollectionWithJoinTableBinderSpec extends HibernateGormDatastoreSpec {
Collection collection = new Set(domainBinder.metadataBuildingContext, owner)
collection.setCollectionTable(new Table("CWJTB_BOOKS"))
+ def manyToOne = new ManyToOne(domainBinder.metadataBuildingContext, collection.getCollectionTable())
+ unidirectionalOneToManyInverseValuesBinder.bind(property, collection) >> manyToOne
+
when:
binder.bindCollectionWithJoinTable(property, mappings, collection)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
index c26c011306..f3d756f127 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
@@ -39,14 +39,13 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
def backticksRemover = new BackticksRemover()
def columnNameForPropertyAndPathFetcher = new ColumnNameForPropertyAndPathFetcher(namingStrategy, defaultColumnNameFetcher, backticksRemover)
- def unidirectionalOneToManyInverseValuesBinder = new UnidirectionalOneToManyInverseValuesBinder()
+ def unidirectionalOneToManyInverseValuesBinder = new UnidirectionalOneToManyInverseValuesBinder(metadataBuildingContext)
def enumTypeBinder = new EnumTypeBinder(metadataBuildingContext, columnNameForPropertyAndPathFetcher)
def compositeIdentifierToManyToOneBinder = new CompositeIdentifierToManyToOneBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment)
def simpleValueColumnFetcher = new SimpleValueColumnFetcher()
def collectionForPropertyConfigBinder = new CollectionForPropertyConfigBinder()
def collectionWithJoinTableBinder = new CollectionWithJoinTableBinder(
- metadataBuildingContext,
namingStrategy,
unidirectionalOneToManyInverseValuesBinder,
compositeIdentifierToManyToOneBinder,
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinderSpec.groovy
index 7894137c75..0cd6035923 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyInverseValuesBinderSpec.groovy
@@ -11,18 +11,24 @@ import spock.lang.Subject
class UnidirectionalOneToManyInverseValuesBinderSpec extends HibernateGormDatastoreSpec {
@Subject
- UnidirectionalOneToManyInverseValuesBinder binder = new UnidirectionalOneToManyInverseValuesBinder()
+ UnidirectionalOneToManyInverseValuesBinder binder
+
+ void setup() {
+ binder = new UnidirectionalOneToManyInverseValuesBinder(getGrailsDomainBinder().metadataBuildingContext)
+ }
void "test bindUnidirectionalOneToManyInverseValues"() {
given:
createPersistentEntity(UOTMBook)
PersistentEntity authorEntity = createPersistentEntity(UOTMAuthor)
HibernateToManyProperty property = (HibernateToManyProperty) authorEntity.getPropertyByName("books")
-
- ManyToOne manyToOne = new ManyToOne(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ def owner = new org.hibernate.mapping.RootClass(getGrailsDomainBinder().metadataBuildingContext)
+ org.hibernate.mapping.Collection collection = new org.hibernate.mapping.Set(getGrailsDomainBinder().metadataBuildingContext, owner)
+ collection.setCollectionTable(new org.hibernate.mapping.Table("UOTM_BOOKS"))
when:
- binder.bindUnidirectionalOneToManyInverseValues(property, manyToOne)
+ ManyToOne manyToOne = binder.bind(property, collection)
then:
manyToOne.isIgnoreNotFound() == false
@@ -36,10 +42,12 @@ class UnidirectionalOneToManyInverseValuesBinderSpec extends HibernateGormDatast
PersistentEntity authorEntity = createPersistentEntity(UOTMAuthorCustom)
HibernateToManyProperty property = (HibernateToManyProperty) authorEntity.getPropertyByName("books")
- ManyToOne manyToOne = new ManyToOne(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+ def owner = new org.hibernate.mapping.RootClass(getGrailsDomainBinder().metadataBuildingContext)
+ org.hibernate.mapping.Collection collection = new org.hibernate.mapping.Set(getGrailsDomainBinder().metadataBuildingContext, owner)
+ collection.setCollectionTable(new org.hibernate.mapping.Table("UOTM_BOOKS_CUSTOM"))
when:
- binder.bindUnidirectionalOneToManyInverseValues(property, manyToOne)
+ ManyToOne manyToOne = binder.bind(property, collection)
then:
manyToOne.isIgnoreNotFound() == true
(grails-core) 24/24: refactor: extract resolveJoinTableForeignKeyColumnName into HibernateToManyProperty
Posted by bo...@apache.org.
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 2900b582eeae1d33a5d7c132033c75b29dd81ede
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 16:46:20 2026 -0600
refactor: extract resolveJoinTableForeignKeyColumnName into HibernateToManyProperty
---
.../hibernate/HibernateToManyProperty.java | 11 +++
.../secondpass/CollectionWithJoinTableBinder.java | 15 +---
.../hibernate/HibernateToManyPropertySpec.groovy | 81 ++++++++++++++++++++++
3 files changed, 95 insertions(+), 12 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
index 5dad0c18d9..542b426b3c 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyProperty.java
@@ -112,4 +112,15 @@ public interface HibernateToManyProperty
+ GrailsDomainBinder.UNDERSCORE
+ IndexedCollection.DEFAULT_ELEMENT_COLUMN_NAME);
}
+
+ default String resolveJoinTableForeignKeyColumnName(PersistentEntityNamingStrategy namingStrategy) {
+ return java.util.Optional.ofNullable(getMappedForm())
+ .map(PropertyConfig::getJoinTableColumnConfig)
+ .map(ColumnConfig::getName)
+ .orElseGet(
+ () ->
+ namingStrategy.resolveColumnName(
+ getHibernateAssociatedEntity().getHibernateRootEntity().getJavaClass().getSimpleName())
+ + GrailsDomainBinder.FOREIGN_KEY_SUFFIX);
+ }
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
index e83dea0456..be671fdf8f 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionWithJoinTableBinder.java
@@ -21,7 +21,6 @@ package org.grails.orm.hibernate.cfg.domainbinding.secondpass;
import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder.*;
import jakarta.annotation.Nonnull;
-import java.util.Optional;
import org.grails.orm.hibernate.cfg.*;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder;
@@ -72,21 +71,13 @@ public class CollectionWithJoinTableBinder {
element = unidirectionalOneToManyInverseValuesBinder.bind(property, collection);
final var domainClass = property.getHibernateAssociatedEntity();
if (domainClass != null) {
- var joinColumnMappingOptional =
- Optional.ofNullable(property.getMappedForm())
- .map(PropertyConfig::getJoinTableColumnConfig);
- if (domainClass.getHibernateCompositeIdentity().isPresent()) {
+ if (domainClass.getHibernateCompositeIdentity().isPresent()) {
CompositeIdentity ci = domainClass.getHibernateCompositeIdentity().get();
compositeIdentifierToManyToOneBinder.bindCompositeIdentifierToManyToOne(
property, element, ci, domainClass, EMPTY_PATH);
} else {
- String columnName =
- joinColumnMappingOptional.isPresent()
- ? joinColumnMappingOptional.get().getName()
- : namingStrategy.resolveColumnName(
- domainClass.getHibernateRootEntity().getJavaClass().getSimpleName())
- + FOREIGN_KEY_SUFFIX;
- simpleValueColumnBinder.bindSimpleValue(element, "long", columnName, true);
+ simpleValueColumnBinder.bindSimpleValue(
+ element, "long", property.resolveJoinTableForeignKeyColumnName(namingStrategy), true);
}
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyPropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyPropertySpec.groovy
new file mode 100644
index 0000000000..d597720af5
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateToManyPropertySpec.groovy
@@ -0,0 +1,81 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate
+
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+
+class HibernateToManyPropertySpec extends HibernateGormDatastoreSpec {
+
+ def setupSpec() {
+ manager.addAllDomainClasses([HTMPBook, HTMPAuthor, HTMPAuthorCustom])
+ }
+
+ void "resolveJoinTableForeignKeyColumnName derives name from associated entity when no explicit config"() {
+ given:
+ def authorEntity = mappingContext.getPersistentEntity(HTMPAuthor.name)
+ HibernateToManyProperty property = (HibernateToManyProperty) authorEntity.getPropertyByName("books")
+ def namingStrategy = getGrailsDomainBinder().namingStrategy
+
+ when:
+ String columnName = property.resolveJoinTableForeignKeyColumnName(namingStrategy)
+
+ then:
+ columnName == "htmpbook_id"
+ }
+
+ void "resolveJoinTableForeignKeyColumnName uses explicit join table column name when configured"() {
+ given:
+ def authorEntity = mappingContext.getPersistentEntity(HTMPAuthorCustom.name)
+ HibernateToManyProperty property = (HibernateToManyProperty) authorEntity.getPropertyByName("books")
+ def namingStrategy = getGrailsDomainBinder().namingStrategy
+
+ when:
+ String columnName = property.resolveJoinTableForeignKeyColumnName(namingStrategy)
+
+ then:
+ columnName == "custom_book_fk"
+ }
+}
+
+@Entity
+class HTMPBook {
+ Long id
+ String title
+}
+
+@Entity
+class HTMPAuthor {
+ Long id
+ String name
+ Set<HTMPBook> books
+ static hasMany = [books: HTMPBook]
+}
+
+@Entity
+class HTMPAuthorCustom {
+ Long id
+ String name
+ Set<HTMPBook> books
+ static hasMany = [books: HTMPBook]
+ static mapping = {
+ books joinTable: [column: 'custom_book_fk']
+ }
+}
(grails-core) 13/24: refactor CollectionSecondPassBinder
Posted by bo...@apache.org.
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"() {
(grails-core) 01/24: fix: share single InFlightMetadataCollector across binders in test specs
Posted by bo...@apache.org.
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 0ac03dca75430f030a310d443ca5bd2c0137f300
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Mar 1 19:12:13 2026 -0600
fix: share single InFlightMetadataCollector across binders in test specs
getBinders() now accepts an optional collector parameter (defaulting to
getCollector()) so callers can pass a pre-created collector, ensuring
second-pass registrations made during bindRoot are visible to the same
collector used in the test body.
CollectionBinderSpec, ListSecondPassBinderSpec and MapSecondPassBinderSpec
received the same fix.
Co-authored-by: Copilot <22...@users.noreply.github.com>
---
.../cfg/domainbinding/binder/ClassBinder.java | 10 +++++---
.../cfg/domainbinding/binder/CollectionBinder.java | 15 ++++++------
.../domainbinding/binder/GrailsDomainBinder.java | 20 +++++++++-------
.../domainbinding/binder/GrailsPropertyBinder.java | 2 +-
.../domainbinding/binder/JoinedSubClassBinder.java | 16 ++++++-------
.../cfg/domainbinding/binder/RootBinder.java | 15 ++++++------
.../RootPersistentClassCommonValuesBinder.java | 10 ++++----
.../binder/SingleTableSubclassBinder.java | 2 +-
.../cfg/domainbinding/binder/SubClassBinder.java | 12 +++++-----
.../binder/SubclassMappingBinder.java | 4 ++--
.../domainbinding/binder/UnionSubclassBinder.java | 11 +++++----
.../secondpass/CollectionSecondPassBinder.java | 2 +-
.../domainbinding/secondpass/ListSecondPass.java | 2 +-
.../secondpass/ListSecondPassBinder.java | 6 +++--
.../secondpass/UnidirectionalOneToManyBinder.java | 9 +++----
.../cfg/domainbinding/ClassBinderSpec.groovy | 12 +++++-----
.../cfg/domainbinding/CollectionBinderSpec.groovy | 22 +++++++++--------
.../domainbinding/GrailsPropertyBinderSpec.groovy | 28 +++++++++++-----------
.../binder/JoinedSubClassBinderSpec.groovy | 7 +++---
.../cfg/domainbinding/binder/RootBinderSpec.groovy | 27 +++++++++++----------
...ootPersistentClassCommonValuesBinderSpec.groovy | 11 +++++----
.../binder/SingleTableSubclassBinderSpec.groovy | 4 +++-
.../domainbinding/binder/SubClassBinderSpec.groovy | 19 ++++++++-------
.../binder/SubclassMappingBinderSpec.groovy | 4 ++--
.../binder/UnionSubclassBinderSpec.groovy | 7 +++---
.../CollectionSecondPassBinderSpec.groovy | 2 +-
.../secondpass/ListSecondPassBinderSpec.groovy | 22 +++++++++--------
.../secondpass/MapSecondPassBinderSpec.groovy | 22 +++++++++--------
.../UnidirectionalOneToManyBinderSpec.groovy | 6 ++---
29 files changed, 180 insertions(+), 149 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassBinder.java
index b21de08b94..2938476f65 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ClassBinder.java
@@ -29,18 +29,22 @@ import org.hibernate.mapping.PersistentClass;
/** The class binder class. */
public class ClassBinder {
+ private final InFlightMetadataCollector collector;
+
+ public ClassBinder(@Nonnull InFlightMetadataCollector collector) {
+ this.collector = collector;
+ }
+
/**
* Binds the specified persistant class to the runtime model based on the properties defined in
* the domain class
*
* @param persistentEntity The Grails domain class
* @param persistentClass The persistant class
- * @param collector Existing collector
*/
public void bindClass(
@Nonnull GrailsHibernatePersistentEntity persistentEntity,
- PersistentClass persistentClass,
- @Nonnull InFlightMetadataCollector collector) {
+ PersistentClass persistentClass) {
persistentClass.setLazy(true);
var entityName = persistentEntity.getName();
persistentClass.setEntityName(entityName);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index b540fd3f24..319d9b12c0 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -69,6 +69,7 @@ public class CollectionBinder {
private final ListSecondPassBinder listSecondPassBinder;
private final CollectionSecondPassBinder collectionSecondPassBinder;
private final MapSecondPassBinder mapSecondPassBinder;
+ private final InFlightMetadataCollector mappings;
/** Creates a new {@link CollectionBinder} instance. */
public CollectionBinder(
@@ -79,10 +80,12 @@ public class CollectionBinder {
ManyToOneBinder manyToOneBinder,
CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder,
SimpleValueColumnFetcher simpleValueColumnFetcher,
- CollectionHolder collectionHolder) {
+ CollectionHolder collectionHolder,
+ InFlightMetadataCollector mappings) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.collectionHolder = collectionHolder;
+ this.mappings = mappings;
GrailsPropertyResolver grailsPropertyResolver = new GrailsPropertyResolver();
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder =
new CollectionForPropertyConfigBinder();
@@ -107,7 +110,7 @@ public class CollectionBinder {
new CollectionKeyColumnUpdater(),
new BidirectionalOneToManyLinker(grailsPropertyResolver),
new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
- new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder),
+ new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
collectionWithJoinTableBinder,
collectionForPropertyConfigBinder,
new DefaultColumnNameFetcher(namingStrategy),
@@ -117,7 +120,8 @@ public class CollectionBinder {
metadataBuildingContext,
namingStrategy,
collectionSecondPassBinder,
- simpleValueColumnBinder);
+ simpleValueColumnBinder,
+ mappings);
this.mapSecondPassBinder =
new MapSecondPassBinder(
metadataBuildingContext,
@@ -133,14 +137,12 @@ public class CollectionBinder {
*
* @param property The GrailsDomainClassProperty instance
* @param owner The owning persistent class
- * @param mappings The Hibernate mappings instance
* @param path The property path
* @return the result
*/
public Collection bindCollection(
HibernateToManyProperty property,
PersistentClass owner,
- @Nonnull InFlightMetadataCollector mappings,
String path) {
CollectionType collectionType = collectionHolder.get(property.getType());
Collection collection = collectionType.create(property, owner);
@@ -170,7 +172,7 @@ public class CollectionBinder {
collection.setElement(oneToMany);
bindOneToMany((HibernateOneToManyProperty) property, oneToMany);
} else {
- bindCollectionTable(property, mappings, collection, owner.getTable());
+ bindCollectionTable(property, collection, owner.getTable());
if (property.isBidirectional()) {
if (!property.isOwningSide()) {
@@ -214,7 +216,6 @@ public class CollectionBinder {
private void bindCollectionTable(
HibernateToManyProperty property,
- @Nonnull InFlightMetadataCollector mappings,
Collection collection,
Table ownerTable) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
index cbf3bf98eb..d411b136be 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.java
@@ -126,7 +126,7 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
EnumTypeBinder enumTypeBinder =
new EnumTypeBinder(metadataBuildingContext, columnNameForPropertyAndPathFetcher);
PropertyFromValueCreator propertyFromValueCreator = new PropertyFromValueCreator();
- ClassBinder classBinder = new ClassBinder();
+ ClassBinder classBinder = new ClassBinder(metadataCollector);
SimpleValueColumnFetcher simpleValueColumnFetcher = new SimpleValueColumnFetcher();
CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder =
new CompositeIdentifierToManyToOneBinder(
@@ -154,7 +154,8 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
manyToOneBinder,
compositeIdentifierToManyToOneBinder,
simpleValueColumnFetcher,
- collectionHolder);
+ collectionHolder,
+ metadataCollector);
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator);
ComponentBinder componentBinder =
new ComponentBinder(metadataBuildingContext, getMappingCacheHolder(), componentUpdater);
@@ -197,9 +198,10 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
namingStrategy,
new SimpleValueColumnBinder(),
columnNameForPropertyAndPathFetcher,
- classBinder);
+ classBinder,
+ metadataCollector);
UnionSubclassBinder unionSubclassBinder =
- new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder);
+ new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder, metadataCollector);
SingleTableSubclassBinder singleTableSubclassBinder =
new SingleTableSubclassBinder(classBinder);
@@ -212,7 +214,7 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
classPropertiesBinder);
SubClassBinder subClassBinder =
new SubClassBinder(
- mappingCacheHolder, subclassMappingBinder, multiTenantFilterBinder, dataSourceName);
+ mappingCacheHolder, subclassMappingBinder, multiTenantFilterBinder, dataSourceName, metadataCollector);
RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder =
new RootPersistentClassCommonValuesBinder(
metadataBuildingContext,
@@ -220,7 +222,8 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
identityBinder,
versionBinder,
classBinder,
- classPropertiesBinder);
+ classPropertiesBinder,
+ metadataCollector);
DiscriminatorPropertyBinder discriminatorPropertyBinder =
new DiscriminatorPropertyBinder(
metadataBuildingContext,
@@ -234,13 +237,14 @@ public class GrailsDomainBinder implements AdditionalMappingContributor, TypeCon
multiTenantFilterBinder,
subClassBinder,
rootPersistentClassCommonValuesBinder,
- discriminatorPropertyBinder);
+ discriminatorPropertyBinder,
+ metadataCollector);
hibernateMappingContext.getHibernatePersistentEntities(dataSourceName).stream()
.filter(persistentEntity -> persistentEntity.forGrailsDomainMapping(dataSourceName))
.forEach(
hibernatePersistentEntity ->
- rootBinder.bindRoot(hibernatePersistentEntity, metadataCollector));
+ rootBinder.bindRoot(hibernatePersistentEntity));
}
/**
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index 924ad91188..9bb67de3f4 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -98,7 +98,7 @@ public class GrailsPropertyBinder {
} else if (currentGrailsProp instanceof HibernateToManyProperty toMany
&& !currentGrailsProp.isSerializableType()) {
// HibernateToManyProperty
- value = collectionBinder.bindCollection(toMany, persistentClass, mappings, path);
+ value = collectionBinder.bindCollection(toMany, persistentClass, path);
} else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) {
value = componentBinder.bindComponent(persistentClass, embedded, mappings, path);
} else {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinder.java
index 3ea370501a..35830aac15 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinder.java
@@ -47,18 +47,21 @@ public class JoinedSubClassBinder {
private final SimpleValueColumnBinder simpleValueColumnBinder;
private final ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher;
private final ClassBinder classBinder;
+ private final InFlightMetadataCollector mappings;
public JoinedSubClassBinder(
MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
SimpleValueColumnBinder simpleValueColumnBinder,
ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher,
- ClassBinder classBinder) {
+ ClassBinder classBinder,
+ InFlightMetadataCollector mappings) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.simpleValueColumnBinder = simpleValueColumnBinder;
this.columnNameForPropertyAndPathFetcher = columnNameForPropertyAndPathFetcher;
this.classBinder = classBinder;
+ this.mappings = mappings;
}
/**
@@ -66,13 +69,11 @@ public class JoinedSubClassBinder {
*
* @param sub The Grails sub class
* @param joinedSubclass The Hibernate Subclass object
- * @param mappings The mappings Object
*/
public void bindJoinedSubClass(
GrailsHibernatePersistentEntity sub,
- JoinedSubclass joinedSubclass,
- InFlightMetadataCollector mappings) {
- classBinder.bindClass(sub, joinedSubclass, mappings);
+ JoinedSubclass joinedSubclass) {
+ classBinder.bindClass(sub, joinedSubclass);
String schemaName = sub.getSchema(mappings);
String catalogName = sub.getCatalog(mappings);
@@ -81,7 +82,7 @@ public class JoinedSubClassBinder {
mappings.addTable(
schemaName,
catalogName,
- getJoinedSubClassTableName(sub, joinedSubclass, null, mappings),
+ getJoinedSubClassTableName(sub, joinedSubclass, null),
null,
false,
metadataBuildingContext);
@@ -111,8 +112,7 @@ public class JoinedSubClassBinder {
private String getJoinedSubClassTableName(
GrailsHibernatePersistentEntity sub,
PersistentClass model,
- Table denormalizedSuperTable,
- InFlightMetadataCollector mappings) {
+ Table denormalizedSuperTable) {
String logicalTableName = GrailsHibernateUtil.unqualify(model.getEntityName());
String physicalTableName = sub.getTableName(namingStrategy);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinder.java
index 9c4840a514..2f52401cc7 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinder.java
@@ -20,9 +20,7 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import jakarta.annotation.Nonnull;
import java.util.Collection;
-import org.grails.orm.hibernate.cfg.Mapping;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
-import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterBinder;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.RootClass;
@@ -40,18 +38,21 @@ public class RootBinder {
private final SubClassBinder subClassBinder;
private final RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder;
private final DiscriminatorPropertyBinder discriminatorPropertyBinder;
+ private final InFlightMetadataCollector mappings;
public RootBinder(
String dataSourceName,
MultiTenantFilterBinder multiTenantFilterBinder,
SubClassBinder subClassBinder,
RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder,
- DiscriminatorPropertyBinder discriminatorPropertyBinder) {
+ DiscriminatorPropertyBinder discriminatorPropertyBinder,
+ InFlightMetadataCollector mappings) {
this.dataSourceName = dataSourceName;
this.multiTenantFilterBinder = multiTenantFilterBinder;
this.subClassBinder = subClassBinder;
this.rootPersistentClassCommonValuesBinder = rootPersistentClassCommonValuesBinder;
this.discriminatorPropertyBinder = discriminatorPropertyBinder;
+ this.mappings = mappings;
}
/**
@@ -59,11 +60,9 @@ public class RootBinder {
* Grails domain class
*
* @param entity The Grails domain class
- * @param mappings The Hibernate Mappings object
*/
public void bindRoot(
- @Nonnull GrailsHibernatePersistentEntity entity,
- @Nonnull InFlightMetadataCollector mappings) {
+ @Nonnull GrailsHibernatePersistentEntity entity) {
if (mappings.getEntityBinding(entity.getName()) != null) {
LOG.warn("[RootBinder] Class [" + entity.getName() + "] is already mapped, skipping.. ");
return;
@@ -72,14 +71,14 @@ public class RootBinder {
Collection<GrailsHibernatePersistentEntity> children = entity.getChildEntities(dataSourceName);
RootClass root =
rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(
- entity, children, mappings);
+ entity, children);
if (!children.isEmpty() && entity.isTablePerHierarchy()) {
discriminatorPropertyBinder.bindDiscriminatorProperty(root);
}
// bind the sub classes
- children.forEach(sub -> subClassBinder.bindSubClass(sub, root, mappings));
+ children.forEach(sub -> subClassBinder.bindSubClass(sub, root));
multiTenantFilterBinder.bind(entity, root);
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
index a10bfaf5aa..4433c496b1 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinder.java
@@ -42,6 +42,7 @@ public class RootPersistentClassCommonValuesBinder {
private final VersionBinder versionBinder;
private final ClassBinder classBinder;
private final ClassPropertiesBinder classPropertiesBinder;
+ private final InFlightMetadataCollector mappings;
public RootPersistentClassCommonValuesBinder(
MetadataBuildingContext metadataBuildingContext,
@@ -49,23 +50,24 @@ public class RootPersistentClassCommonValuesBinder {
IdentityBinder identityBinder,
VersionBinder versionBinder,
ClassBinder classBinder,
- ClassPropertiesBinder classPropertiesBinder) {
+ ClassPropertiesBinder classPropertiesBinder,
+ InFlightMetadataCollector mappings) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.identityBinder = identityBinder;
this.versionBinder = versionBinder;
this.classBinder = classBinder;
this.classPropertiesBinder = classPropertiesBinder;
+ this.mappings = mappings;
}
public RootClass bindRootPersistentClassCommonValues(
@Nonnull GrailsHibernatePersistentEntity domainClass,
- @Nonnull Collection<GrailsHibernatePersistentEntity> children,
- @Nonnull InFlightMetadataCollector mappings) {
+ @Nonnull Collection<GrailsHibernatePersistentEntity> children) {
RootClass root = new RootClass(this.metadataBuildingContext);
root.setAbstract(domainClass.isAbstract());
- classBinder.bindClass(domainClass, root, mappings);
+ classBinder.bindClass(domainClass, root);
// get the schema and catalog names from the configuration
Mapping gormMapping = domainClass.getMappedForm();
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinder.java
index abb6d76b21..574d99b87f 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinder.java
@@ -51,7 +51,7 @@ public class SingleTableSubclassBinder {
@Nonnull GrailsHibernatePersistentEntity sub,
SingleTableSubclass subClass,
@Nonnull InFlightMetadataCollector mappings) {
- classBinder.bindClass(sub, subClass, mappings);
+ classBinder.bindClass(sub, subClass);
subClass.setDiscriminatorValue(sub.getDiscriminatorValue());
if (LOG.isDebugEnabled()) {
LOG.debug(
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinder.java
index 1da4593f1c..2a5b3c1495 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinder.java
@@ -20,7 +20,6 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import jakarta.annotation.Nonnull;
import java.util.Collection;
-import org.grails.orm.hibernate.cfg.Mapping;
import org.grails.orm.hibernate.cfg.MappingCacheHolder;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterBinder;
@@ -38,16 +37,19 @@ public class SubClassBinder {
private final SubclassMappingBinder subclassMappingBinder;
private final MultiTenantFilterBinder multiTenantFilterBinder;
private final String dataSourceName;
+ private final InFlightMetadataCollector mappings;
public SubClassBinder(
MappingCacheHolder mappingCacheHolder,
SubclassMappingBinder subclassMappingBinder,
MultiTenantFilterBinder multiTenantFilterBinder,
- String dataSourceName) {
+ String dataSourceName,
+ InFlightMetadataCollector mappings) {
this.mappingCacheHolder = mappingCacheHolder;
this.subclassMappingBinder = subclassMappingBinder;
this.multiTenantFilterBinder = multiTenantFilterBinder;
this.dataSourceName = dataSourceName;
+ this.mappings = mappings;
}
/**
@@ -55,18 +57,16 @@ public class SubClassBinder {
*
* @param sub The sub domain class instance
* @param parent The parent persistent class instance
- * @param mappings The mappings instance
*/
public void bindSubClass(
@Nonnull GrailsHibernatePersistentEntity sub,
- PersistentClass parent,
- @Nonnull InFlightMetadataCollector mappings) {
+ PersistentClass parent) {
mappingCacheHolder.cacheMapping(sub);
Subclass subClass = subclassMappingBinder.createSubclassMapping(sub, parent, mappings);
parent.addSubclass(subClass);
mappings.addEntityBinding(subClass);
bindMultiTenantFilter(sub, subClass);
- sub.getChildEntities(dataSourceName).forEach(sub1 -> bindSubClass(sub1, subClass, mappings));
+ sub.getChildEntities(dataSourceName).forEach(sub1 -> bindSubClass(sub1, subClass));
}
private void bindMultiTenantFilter(GrailsHibernatePersistentEntity sub, Subclass subClass) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
index 755641307a..8a80745b3e 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinder.java
@@ -61,11 +61,11 @@ public class SubclassMappingBinder {
Mapping m = subEntity.getMappedForm();
if (subEntity.isJoinedSubclass()) {
var joined = new JoinedSubclass(parent, this.metadataBuildingContext);
- joinedSubClassBinder.bindJoinedSubClass(subEntity, joined, mappings);
+ joinedSubClassBinder.bindJoinedSubClass(subEntity, joined);
subClass = joined;
} else if (subEntity.isUnionSubclass()) {
var union = new UnionSubclass(parent, this.metadataBuildingContext);
- unionSubclassBinder.bindUnionSubclass(subEntity, union, mappings);
+ unionSubclassBinder.bindUnionSubclass(subEntity, union);
subClass = union;
} else {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinder.java
index ff03b45b0f..0587e4e1e7 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinder.java
@@ -41,14 +41,17 @@ public class UnionSubclassBinder {
private final MetadataBuildingContext metadataBuildingContext;
private final PersistentEntityNamingStrategy namingStrategy;
private final ClassBinder classBinder;
+ private final InFlightMetadataCollector mappings;
public UnionSubclassBinder(
MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
- ClassBinder classBinder) {
+ ClassBinder classBinder,
+ InFlightMetadataCollector mappings) {
this.metadataBuildingContext = metadataBuildingContext;
this.namingStrategy = namingStrategy;
this.classBinder = classBinder;
+ this.mappings = mappings;
}
/**
@@ -56,14 +59,12 @@ public class UnionSubclassBinder {
*
* @param subClass The Grails sub class
* @param unionSubclass The Hibernate UnionSubclass object
- * @param mappings The mappings Object
*/
public void bindUnionSubclass(
@Nonnull GrailsHibernatePersistentEntity subClass,
- UnionSubclass unionSubclass,
- @Nonnull InFlightMetadataCollector mappings)
+ UnionSubclass unionSubclass)
throws MappingException {
- classBinder.bindClass(subClass, unionSubclass, mappings);
+ classBinder.bindClass(subClass, unionSubclass);
String schema = subClass.getSchema(mappings);
String catalog = subClass.getCatalog(mappings);
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 b3d6063fca..2d0f524947 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
@@ -170,7 +170,7 @@ public class CollectionSecondPassBinder {
}
} else if (property.isUnidirectionalOneToMany()) {
unidirectionalOneToManyBinder.bind(
- (HibernateOneToManyProperty) property, mappings, collection);
+ (HibernateOneToManyProperty) property, collection);
} else if (property.supportsJoinColumnMapping()) {
collectionWithJoinTableBinder.bindCollectionWithJoinTable(property, mappings, collection);
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java
index 5fb6f9b412..edeb426bb7 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java
@@ -51,7 +51,7 @@ public class ListSecondPass
@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
listSecondPassBinder.bindListSecondPass(
- property, mappings, persistentClasses, (org.hibernate.mapping.List) collection);
+ property, persistentClasses, (org.hibernate.mapping.List) collection);
createCollectionKeys(collection);
}
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
index f83c4ea941..9d6cf3d0b1 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java
@@ -51,21 +51,23 @@ public class ListSecondPassBinder {
private final CollectionSecondPassBinder collectionSecondPassBinder;
private final PersistentEntityNamingStrategy namingStrategy;
private final SimpleValueColumnBinder simpleValueColumnBinder;
+ private final InFlightMetadataCollector mappings;
public ListSecondPassBinder(
MetadataBuildingContext metadataBuildingContext,
PersistentEntityNamingStrategy namingStrategy,
CollectionSecondPassBinder collectionSecondPassBinder,
- SimpleValueColumnBinder simpleValueColumnBinder) {
+ SimpleValueColumnBinder simpleValueColumnBinder,
+ InFlightMetadataCollector mappings) {
this.metadataBuildingContext = metadataBuildingContext;
this.collectionSecondPassBinder = collectionSecondPassBinder;
this.namingStrategy = namingStrategy;
this.simpleValueColumnBinder = simpleValueColumnBinder;
+ this.mappings = mappings;
}
public void bindListSecondPass(
@Nonnull HibernateToManyProperty property,
- @Nonnull InFlightMetadataCollector mappings,
Map<?, ?> persistentClasses,
@Nonnull List list) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinder.java
index 5badf0a454..1e2451e616 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinder.java
@@ -39,26 +39,27 @@ public class UnidirectionalOneToManyBinder {
private static final Logger LOG = LoggerFactory.getLogger(UnidirectionalOneToManyBinder.class);
private final CollectionWithJoinTableBinder collectionWithJoinTableBinder;
private final BackticksRemover backticksRemover = new BackticksRemover();
+ private final InFlightMetadataCollector mappings;
public UnidirectionalOneToManyBinder(
- CollectionWithJoinTableBinder collectionWithJoinTableBinder) {
+ CollectionWithJoinTableBinder collectionWithJoinTableBinder,
+ InFlightMetadataCollector mappings) {
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
+ this.mappings = mappings;
}
public void bind(
@Nonnull HibernateOneToManyProperty property,
- @Nonnull InFlightMetadataCollector mappings,
@Nonnull Collection collection) {
if (!property.shouldBindWithForeignKey()) {
collectionWithJoinTableBinder.bindCollectionWithJoinTable(property, mappings, collection);
} else {
- bindUnidirectionalOneToMany(property, mappings, collection);
+ bindUnidirectionalOneToMany(property, collection);
}
}
private void bindUnidirectionalOneToMany(
@Nonnull HibernateOneToManyProperty property,
- @Nonnull InFlightMetadataCollector mappings,
@Nonnull Collection collection) {
Value element = collection.getElement();
element.createForeignKey();
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ClassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ClassBinderSpec.groovy
index 79258531dd..39d060308f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ClassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ClassBinderSpec.groovy
@@ -18,9 +18,9 @@ class ClassBinderSpec extends HibernateGormDatastoreSpec {
def persistentName = "foo.Book"
def persistentEntity = createPersistentEntity(grailsDomainBinder,simpleName, [:], [:])
def root = new RootClass(grailsDomainBinder.metadataBuildingContext);
- def binder = new ClassBinder()
+ def binder = new ClassBinder(collector)
- binder.bindClass(persistentEntity,root, collector)
+ binder.bindClass(persistentEntity,root)
then:
root.getEntityName() == persistentName
root.getJpaEntityName() == simpleName
@@ -43,9 +43,9 @@ class ClassBinderSpec extends HibernateGormDatastoreSpec {
def persistentName = "foo.Book"
def persistentEntity = createPersistentEntity(grailsDomainBinder,simpleName, [:], [autoImport: "true"])
def root = new RootClass(grailsDomainBinder.metadataBuildingContext);
- def binder = new ClassBinder()
+ def binder = new ClassBinder(collector)
- binder.bindClass(persistentEntity,root, collector)
+ binder.bindClass(persistentEntity,root)
then:
root.getEntityName() == persistentName
root.getJpaEntityName() == simpleName
@@ -68,9 +68,9 @@ class ClassBinderSpec extends HibernateGormDatastoreSpec {
def persistentName = "foo.Book"
def persistentEntity = createPersistentEntity(grailsDomainBinder, simpleName, [:], [autoImport: "false"])
def root = new RootClass(grailsDomainBinder.metadataBuildingContext);
- def binder = new ClassBinder()
+ def binder = new ClassBinder(collector)
- binder.bindClass(persistentEntity,root, collector)
+ binder.bindClass(persistentEntity,root)
then:
root.getEntityName() == persistentName
root.getJpaEntityName() == persistentName
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
index 0da2b5f3d9..fdfbd839c9 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/CollectionBinderSpec.groovy
@@ -51,6 +51,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.SingleTableSubclassBind
class CollectionBinderSpec extends HibernateGormDatastoreSpec {
protected Map getBinders(GrailsDomainBinder binder) {
+ def collector = getCollector()
MetadataBuildingContext metadataBuildingContext = binder.getMetadataBuildingContext()
PersistentEntityNamingStrategy namingStrategy = binder.getNamingStrategy()
JdbcEnvironment jdbcEnvironment = binder.getJdbcEnvironment()
@@ -82,7 +83,8 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
compositeIdentifierToManyToOneBinder,
simpleValueColumnFetcher
,
- collectionHolder
+ collectionHolder,
+ getCollector()
)
PropertyFromValueCreator propertyFromValueCreator = new PropertyFromValueCreator()
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator)
@@ -110,18 +112,18 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
IdentityBinder identityBinder = new IdentityBinder(simpleIdBinder, compositeIdBinder)
VersionBinder versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinderHelper, BasicValue::new)
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder = new ClassBinder(getCollector())
ClassPropertiesBinder classPropertiesBinder = new ClassPropertiesBinder(propertyBinder, propertyFromValueCreator)
- MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), binder.getMetadataBuildingContext().getMetadataCollector(), defaultColumnNameFetcher)
- JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder)
- UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder)
+ MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), getCollector(), defaultColumnNameFetcher)
+ JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder, getCollector())
+ UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder, getCollector())
SingleTableSubclassBinder singleTableSubclassBinder = new SingleTableSubclassBinder(classBinder)
SubclassMappingBinder subclassMappingBinder = new SubclassMappingBinder(metadataBuildingContext, joinedSubClassBinder, unionSubclassBinder, singleTableSubclassBinder, classPropertiesBinder)
- SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource")
- RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder)
+ SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource", getCollector())
+ RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder, getCollector())
DiscriminatorPropertyBinder discriminatorPropertyBinder = new DiscriminatorPropertyBinder(metadataBuildingContext, binder.getMappingCacheHolder(), new org.grails.orm.hibernate.cfg.domainbinding.binder.ConfiguredDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), new ColumnConfigToColumnBinder()), new org.grails.orm.hibernate.cfg.domainbinding.binder.DefaultDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.Si [...]
- RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder)
+ RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder, getCollector())
return [
propertyBinder: propertyBinder,
@@ -143,7 +145,7 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
protected void bindRoot(GrailsDomainBinder binder, GrailsHibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
def binders = getBinders(binder)
- binders.rootBinder.bindRoot(entity, mappings)
+ binders.rootBinder.bindRoot(entity)
}
void setupSpec() {
@@ -170,7 +172,7 @@ class CollectionBinderSpec extends HibernateGormDatastoreSpec {
def petsProp = personEntity.getPropertyByName("pets") as HibernateToManyProperty
when:
- def collection = collectionBinder.bindCollection(petsProp, rootClass, collector, "")
+ def collection = collectionBinder.bindCollection(petsProp, rootClass, "")
then:
collection.role == "${personEntity.name}.pets".toString()
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
index 1c535e7f2a..b76048a4d6 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
@@ -109,7 +109,7 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
prop.getMappedForm() >> config
}
- protected Map getBinders(GrailsDomainBinder binder) {
+ protected Map getBinders(GrailsDomainBinder binder, InFlightMetadataCollector collector = getCollector()) {
MetadataBuildingContext metadataBuildingContext = binder.getMetadataBuildingContext()
PersistentEntityNamingStrategy namingStrategy = binder.getNamingStrategy()
JdbcEnvironment jdbcEnvironment = binder.getJdbcEnvironment()
@@ -141,7 +141,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
compositeIdentifierToManyToOneBinder,
simpleValueColumnFetcher
,
- collectionHolder
+ collectionHolder,
+ collector
)
PropertyFromValueCreator propertyFromValueCreator = new PropertyFromValueCreator()
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator)
@@ -170,18 +171,18 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
VersionBinder versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinderHelper, BasicValue::new)
NaturalIdentifierBinder naturalIdentifierBinder = new NaturalIdentifierBinder()
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder = new ClassBinder(collector)
ClassPropertiesBinder classPropertiesBinder = new ClassPropertiesBinder(propertyBinder, propertyFromValueCreator, naturalIdentifierBinder)
- MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), binder.getMetadataBuildingContext().getMetadataCollector(), defaultColumnNameFetcher)
- JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder)
- UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder)
+ MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), collector, defaultColumnNameFetcher)
+ JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder, collector)
+ UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder, collector)
SingleTableSubclassBinder singleTableSubclassBinder = new SingleTableSubclassBinder(classBinder)
SubclassMappingBinder subclassMappingBinder = new SubclassMappingBinder(metadataBuildingContext, joinedSubClassBinder, unionSubclassBinder, singleTableSubclassBinder, classPropertiesBinder)
- SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource")
- RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder)
+ SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource", collector)
+ RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder, collector)
DiscriminatorPropertyBinder discriminatorPropertyBinder = new DiscriminatorPropertyBinder(metadataBuildingContext, binder.getMappingCacheHolder(), new org.grails.orm.hibernate.cfg.domainbinding.binder.ConfiguredDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), new ColumnConfigToColumnBinder()), new org.grails.orm.hibernate.cfg.domainbinding.binder.DefaultDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.Si [...]
- RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder)
+ RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder, collector)
return [
propertyBinder: propertyBinder,
@@ -203,8 +204,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
}
protected void bindRoot(GrailsDomainBinder binder, GrailsHibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
- def binders = getBinders(binder)
- binders.rootBinder.bindRoot(entity, mappings)
+ def binders = getBinders(binder, mappings)
+ binders.rootBinder.bindRoot(entity)
}
void setupSpec() {
manager.addAllDomainClasses([
@@ -349,9 +350,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
void "Test bind list collection"() {
given:
def binder = getGrailsDomainBinder()
- def propertyBinder = getBinders(binder).propertyBinder
def collector = getCollector()
-
+ def propertyBinder = getBinders(binder, collector).propertyBinder
def bookEntity = createPersistentEntity(ListBook)
def authorEntity = createPersistentEntity(ListAuthor)
@@ -389,8 +389,8 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
void "Test bind map collection"() {
given:
def binder = getGrailsDomainBinder()
- def propertyBinder = getBinders(binder).propertyBinder
def collector = getCollector()
+ def propertyBinder = getBinders(binder, collector).propertyBinder
def bookEntity = createPersistentEntity(MapBook)
def authorEntity = createPersistentEntity(MapAuthor)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinderSpec.groovy
index 0a78b7dcac..64273c2d6e 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/JoinedSubClassBinderSpec.groovy
@@ -15,17 +15,18 @@ class JoinedSubClassBinderSpec extends HibernateGormDatastoreSpec {
JoinedSubClassBinder binder
ColumnNameForPropertyAndPathFetcher fetcher
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder
SimpleValueColumnBinder simpleValueColumnBinder = new SimpleValueColumnBinder()
void setup() {
def buildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
+ classBinder = new ClassBinder(buildingContext.getMetadataCollector())
def namingStrategy = getGrailsDomainBinder().getNamingStrategy()
def backticksRemover = new org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover()
def defaultColumnNameFetcher = new org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher(namingStrategy, backticksRemover)
fetcher = new org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher(namingStrategy, defaultColumnNameFetcher, backticksRemover)
- binder = new JoinedSubClassBinder(buildingContext, namingStrategy, simpleValueColumnBinder, fetcher, classBinder)
+ binder = new JoinedSubClassBinder(buildingContext, namingStrategy, simpleValueColumnBinder, fetcher, classBinder, buildingContext.getMetadataCollector())
}
void "test bind joined subclass with real entities"() {
@@ -58,7 +59,7 @@ class JoinedSubClassBinderSpec extends HibernateGormDatastoreSpec {
joinedSubclass.setEntityName(JoinedSubClassSub.name)
when:
- binder.bindJoinedSubClass(subEntity, joinedSubclass, mappings)
+ binder.bindJoinedSubClass(subEntity, joinedSubclass)
then:
joinedSubclass.getTable() != null
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinderSpec.groovy
index 9d99c2db1d..df21696b11 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinderSpec.groovy
@@ -18,11 +18,13 @@ class RootBinderSpec extends HibernateGormDatastoreSpec {
DiscriminatorPropertyBinder discriminatorPropertyBinder
MetadataBuildingContext metadataBuildingContext
PersistentEntityNamingStrategy namingStrategy
+ def sharedCollector
void setup() {
def gdb = getGrailsDomainBinder()
metadataBuildingContext = gdb.getMetadataBuildingContext()
namingStrategy = gdb.getNamingStrategy()
+ sharedCollector = getCollector()
multiTenantFilterBinder = Mock(MultiTenantFilterBinder)
subClassBinder = Mock(SubClassBinder)
@@ -34,7 +36,8 @@ class RootBinderSpec extends HibernateGormDatastoreSpec {
multiTenantFilterBinder,
subClassBinder,
rootPersistentClassCommonValuesBinder,
- discriminatorPropertyBinder
+ discriminatorPropertyBinder,
+ sharedCollector
)
}
@@ -45,18 +48,18 @@ class RootBinderSpec extends HibernateGormDatastoreSpec {
entity.getChildEntities("default") >> []
entity.getMappedForm() >> new Mapping()
- def mappings = getCollector()
+ def mappings = sharedCollector
def rootClass = new RootClass(metadataBuildingContext)
rootClass.setEntityName("Parent")
rootClass.setJpaEntityName("Parent")
when:
- binder.bindRoot(entity, mappings)
+ binder.bindRoot(entity)
then:
- 1 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(entity, [], mappings) >> rootClass
+ 1 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(entity, []) >> rootClass
0 * discriminatorPropertyBinder.bindDiscriminatorProperty(_)
- 0 * subClassBinder.bindSubClass(_, _, _)
+ 0 * subClassBinder.bindSubClass(_, _)
1 * multiTenantFilterBinder.bind(entity, rootClass)
mappings.getEntityBinding("Parent") == rootClass
}
@@ -72,18 +75,18 @@ class RootBinderSpec extends HibernateGormDatastoreSpec {
entity.getMappedForm() >> mapping
entity.isTablePerHierarchy() >> true
- def mappings = getCollector()
+ def mappings = sharedCollector
def rootClass = new RootClass(metadataBuildingContext)
rootClass.setEntityName("Parent")
rootClass.setJpaEntityName("Parent")
when:
- binder.bindRoot(entity, mappings)
+ binder.bindRoot(entity)
then:
- 1 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(entity, [childEntity], mappings) >> rootClass
+ 1 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(entity, [childEntity]) >> rootClass
1 * discriminatorPropertyBinder.bindDiscriminatorProperty(rootClass)
- 1 * subClassBinder.bindSubClass(childEntity, rootClass, mappings)
+ 1 * subClassBinder.bindSubClass(childEntity, rootClass)
1 * multiTenantFilterBinder.bind(entity, rootClass)
mappings.getEntityBinding("Parent") == rootClass
}
@@ -92,16 +95,16 @@ class RootBinderSpec extends HibernateGormDatastoreSpec {
given:
def entity = Mock(GrailsHibernatePersistentEntity)
entity.getName() >> "Parent"
- def mappings = getCollector()
+ def mappings = sharedCollector
def rootClass = new RootClass(metadataBuildingContext)
rootClass.setEntityName("Parent")
rootClass.setJpaEntityName("Parent")
mappings.addEntityBinding(rootClass)
when:
- binder.bindRoot(entity, mappings)
+ binder.bindRoot(entity)
then:
- 0 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(_, _, _)
+ 0 * rootPersistentClassCommonValuesBinder.bindRootPersistentClassCommonValues(_, _)
}
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
index a1ea763a5a..c9b66dbe4f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootPersistentClassCommonValuesBinderSpec.groovy
@@ -33,7 +33,7 @@ class RootPersistentClassCommonValuesBinderSpec extends HibernateGormDatastoreSp
def compositeIdBinder = new CompositeIdBinder(metadataBuildingContext, null, null)
identityBinder = new IdentityBinder(simpleIdBinder, compositeIdBinder)
versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinder, BasicValue::new)
- classBinder = new ClassBinder()
+ classBinder = new ClassBinder(getCollector())
classPropertiesBinder = Mock(ClassPropertiesBinder)
binder = new RootPersistentClassCommonValuesBinder(
@@ -42,7 +42,8 @@ class RootPersistentClassCommonValuesBinderSpec extends HibernateGormDatastoreSp
identityBinder,
versionBinder,
classBinder,
- classPropertiesBinder
+ classPropertiesBinder,
+ getCollector()
)
}
@@ -52,10 +53,10 @@ class RootPersistentClassCommonValuesBinderSpec extends HibernateGormDatastoreSp
def mappings = getCollector()
when:
- RootClass rootClass = binder.bindRootPersistentClassCommonValues(entity, [], mappings)
+ RootClass rootClass = binder.bindRootPersistentClassCommonValues(entity, [])
then:
- 1 * classPropertiesBinder.bindClassProperties(entity, _, mappings)
+ 1 * classPropertiesBinder.bindClassProperties(entity, _, _)
rootClass != null
rootClass.getEntityName() == TestEntity.name
rootClass.isAbstract() == false
@@ -68,7 +69,7 @@ class RootPersistentClassCommonValuesBinderSpec extends HibernateGormDatastoreSp
def mappings = getCollector()
when:
- RootClass rootClass = binder.bindRootPersistentClassCommonValues(entity, [], mappings)
+ RootClass rootClass = binder.bindRootPersistentClassCommonValues(entity, [])
then:
rootClass != null
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinderSpec.groovy
index 9e76d4a527..ee4844fdf0 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SingleTableSubclassBinderSpec.groovy
@@ -12,9 +12,11 @@ import org.hibernate.mapping.SingleTableSubclass
class SingleTableSubclassBinderSpec extends HibernateGormDatastoreSpec {
SingleTableSubclassBinder binder
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder
void setup() {
+ def buildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
+ classBinder = new ClassBinder(buildingContext.getMetadataCollector())
binder = new SingleTableSubclassBinder(classBinder)
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinderSpec.groovy
index eb7ba303dd..0583b54d19 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubClassBinderSpec.groovy
@@ -19,9 +19,11 @@ class SubClassBinderSpec extends HibernateGormDatastoreSpec {
MultiTenantFilterBinder multiTenantFilterBinder
MappingCacheHolder mappingCacheHolder
MetadataBuildingContext metadataBuildingContext
+ def sharedCollector
void setup() {
def gdb = getGrailsDomainBinder()
+ sharedCollector = getCollector()
metadataBuildingContext = gdb.getMetadataBuildingContext()
mappingCacheHolder = gdb.getMappingCacheHolder()
@@ -32,7 +34,8 @@ class SubClassBinderSpec extends HibernateGormDatastoreSpec {
mappingCacheHolder,
subclassMappingBinder,
multiTenantFilterBinder,
- "default"
+ "default",
+ sharedCollector
)
}
@@ -44,17 +47,17 @@ class SubClassBinderSpec extends HibernateGormDatastoreSpec {
def rootClass = new RootClass(metadataBuildingContext)
rootClass.setEntityName("Parent")
rootClass.setJpaEntityName("Parent")
- def mappings = getCollector()
+ def mappings = sharedCollector
def mapping = new Mapping()
def subClass = new org.hibernate.mapping.SingleTableSubclass(rootClass, metadataBuildingContext)
subClass.setEntityName("Child")
subClass.setJpaEntityName("Child")
when:
- binder.bindSubClass(subEntity, rootClass, mappings)
+ binder.bindSubClass(subEntity, rootClass)
then:
- 1 * subclassMappingBinder.createSubclassMapping(subEntity, rootClass, mappings) >> subClass
+ 1 * subclassMappingBinder.createSubclassMapping(subEntity, rootClass, _) >> subClass
1 * multiTenantFilterBinder.bind(subEntity, subClass)
rootClass.getSubclasses().contains(subClass)
mappings.getEntityBinding(subClass.getEntityName()) == subClass
@@ -72,7 +75,7 @@ class SubClassBinderSpec extends HibernateGormDatastoreSpec {
def rootClass = new RootClass(metadataBuildingContext)
rootClass.setEntityName("Parent")
rootClass.setJpaEntityName("Parent")
- def mappings = getCollector()
+ def mappings = sharedCollector
def mapping = new Mapping()
def subClass = new org.hibernate.mapping.SingleTableSubclass(rootClass, metadataBuildingContext)
@@ -83,11 +86,11 @@ class SubClassBinderSpec extends HibernateGormDatastoreSpec {
grandChildSubClass.setJpaEntityName("GrandChild")
when:
- binder.bindSubClass(subEntity, rootClass, mappings)
+ binder.bindSubClass(subEntity, rootClass)
then:
- 1 * subclassMappingBinder.createSubclassMapping(subEntity, rootClass, mappings) >> subClass
- 1 * subclassMappingBinder.createSubclassMapping(grandChildEntity, subClass, mappings) >> grandChildSubClass
+ 1 * subclassMappingBinder.createSubclassMapping(subEntity, rootClass, _) >> subClass
+ 1 * subclassMappingBinder.createSubclassMapping(grandChildEntity, subClass, _) >> grandChildSubClass
2 * multiTenantFilterBinder.bind(_, _)
rootClass.getSubclasses().contains(subClass)
subClass.getSubclasses().contains(grandChildSubClass)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
index 77492c8ad7..3d26e162a3 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SubclassMappingBinderSpec.groovy
@@ -73,7 +73,7 @@ class SubclassMappingBinderSpec extends HibernateGormDatastoreSpec {
then:
subEntity != null
- 1 * joinedSubClassBinder.bindJoinedSubClass(subEntity, _ as JoinedSubclass, mappings)
+ 1 * joinedSubClassBinder.bindJoinedSubClass(subEntity, _ as JoinedSubclass)
1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass, mappings)
subClass instanceof JoinedSubclass
subClass.getEntityName() == SMBSJoinedSub.name
@@ -93,7 +93,7 @@ class SubclassMappingBinderSpec extends HibernateGormDatastoreSpec {
then:
subEntity != null
- 1 * unionSubclassBinder.bindUnionSubclass(subEntity, _ as UnionSubclass, mappings)
+ 1 * unionSubclassBinder.bindUnionSubclass(subEntity, _ as UnionSubclass)
1 * classPropertiesBinder.bindClassProperties(subEntity, _ as Subclass, mappings)
subClass instanceof UnionSubclass
subClass.getEntityName() == SMBSUnionSub.name
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinderSpec.groovy
index f807a623a9..62aa2bbc0f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/UnionSubclassBinderSpec.groovy
@@ -13,12 +13,13 @@ import org.hibernate.mapping.UnionSubclass
class UnionSubclassBinderSpec extends HibernateGormDatastoreSpec {
UnionSubclassBinder binder
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder
void setup() {
def buildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
def namingStrategy = getGrailsDomainBinder().getNamingStrategy()
- binder = new UnionSubclassBinder(buildingContext, namingStrategy, classBinder)
+ classBinder = new ClassBinder(buildingContext.getMetadataCollector())
+ binder = new UnionSubclassBinder(buildingContext, namingStrategy, classBinder, buildingContext.getMetadataCollector())
}
void "test bind union subclass with real entities"() {
@@ -42,7 +43,7 @@ class UnionSubclassBinderSpec extends HibernateGormDatastoreSpec {
unionSubclass.setEntityName(UnionSubClassSub.name)
when:
- binder.bindUnionSubclass(subEntity, unionSubclass, mappings)
+ binder.bindUnionSubclass(subEntity, unionSubclass)
then:
unionSubclass.getTable() != null
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 535781933d..38eb6c4761 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
@@ -38,7 +38,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def botml = new BidirectionalOneToManyLinker(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver())
def dkvb = new DependentKeyValueBinder(svb, citmto)
def cwjtb = new CollectionWithJoinTableBinder(mbc, ns, new UnidirectionalOneToManyInverseValuesBinder(), null, citmto, svcf, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), null)
- def uotmb = new UnidirectionalOneToManyBinder(cwjtb)
+ def uotmb = new UnidirectionalOneToManyBinder(cwjtb, mbc.getMetadataCollector())
def cfpcb = new CollectionForPropertyConfigBinder()
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
index ce263b4dc2..f136a62ced 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinderSpec.groovy
@@ -50,6 +50,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.SingleTableSubclassBind
class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
protected Map getBinders(GrailsDomainBinder binder) {
+ def collector = getCollector()
MetadataBuildingContext metadataBuildingContext = binder.getMetadataBuildingContext()
PersistentEntityNamingStrategy namingStrategy = binder.getNamingStrategy()
JdbcEnvironment jdbcEnvironment = binder.getJdbcEnvironment()
@@ -81,7 +82,8 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
compositeIdentifierToManyToOneBinder,
simpleValueColumnFetcher
,
- collectionHolder
+ collectionHolder,
+ getCollector()
)
PropertyFromValueCreator propertyFromValueCreator = new PropertyFromValueCreator()
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator)
@@ -109,18 +111,18 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
IdentityBinder identityBinder = new IdentityBinder(simpleIdBinder, compositeIdBinder)
VersionBinder versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinderHelper, BasicValue::new)
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder = new ClassBinder(getCollector())
ClassPropertiesBinder classPropertiesBinder = new ClassPropertiesBinder(propertyBinder, propertyFromValueCreator)
- MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), binder.getMetadataBuildingContext().getMetadataCollector(), defaultColumnNameFetcher)
- JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder)
- UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder)
+ MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), getCollector(), defaultColumnNameFetcher)
+ JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder, getCollector())
+ UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder, getCollector())
SingleTableSubclassBinder singleTableSubclassBinder = new SingleTableSubclassBinder(classBinder)
SubclassMappingBinder subclassMappingBinder = new SubclassMappingBinder(metadataBuildingContext, joinedSubClassBinder, unionSubclassBinder, singleTableSubclassBinder, classPropertiesBinder)
- SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource")
- RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder)
+ SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource", getCollector())
+ RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder, getCollector())
DiscriminatorPropertyBinder discriminatorPropertyBinder = new DiscriminatorPropertyBinder(metadataBuildingContext, binder.getMappingCacheHolder(), new org.grails.orm.hibernate.cfg.domainbinding.binder.ConfiguredDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), new ColumnConfigToColumnBinder()), new org.grails.orm.hibernate.cfg.domainbinding.binder.DefaultDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.Si [...]
- RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder)
+ RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder, getCollector())
return [
propertyBinder: propertyBinder,
@@ -142,7 +144,7 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
protected void bindRoot(GrailsDomainBinder binder, GrailsHibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
def binders = getBinders(binder)
- binders.rootBinder.bindRoot(entity, mappings)
+ binders.rootBinder.bindRoot(entity)
}
void setupSpec() {
@@ -169,7 +171,7 @@ class ListSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def petsProp = personEntity.getPropertyByName("pets") as HibernateToManyProperty
when:
- def collection = collectionBinder.bindCollection(petsProp, rootClass, collector, "")
+ def collection = collectionBinder.bindCollection(petsProp, rootClass, "")
then:
collection.role == "${personEntity.name}.pets".toString()
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
index 430f44876a..cfbb66a509 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPassBinderSpec.groovy
@@ -50,6 +50,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.binder.SingleTableSubclassBind
class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
protected Map getBinders(GrailsDomainBinder binder) {
+ def collector = getCollector()
MetadataBuildingContext metadataBuildingContext = binder.getMetadataBuildingContext()
PersistentEntityNamingStrategy namingStrategy = binder.getNamingStrategy()
JdbcEnvironment jdbcEnvironment = binder.getJdbcEnvironment()
@@ -81,7 +82,8 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
compositeIdentifierToManyToOneBinder,
simpleValueColumnFetcher
,
- collectionHolder
+ collectionHolder,
+ getCollector()
)
PropertyFromValueCreator propertyFromValueCreator = new PropertyFromValueCreator()
ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator)
@@ -109,18 +111,18 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
IdentityBinder identityBinder = new IdentityBinder(simpleIdBinder, compositeIdBinder)
VersionBinder versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinderHelper, BasicValue::new)
- ClassBinder classBinder = new ClassBinder()
+ ClassBinder classBinder = new ClassBinder(getCollector())
ClassPropertiesBinder classPropertiesBinder = new ClassPropertiesBinder(propertyBinder, propertyFromValueCreator)
- MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), binder.getMetadataBuildingContext().getMetadataCollector(), defaultColumnNameFetcher)
- JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder)
- UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder)
+ MultiTenantFilterBinder multiTenantFilterBinder = new MultiTenantFilterBinder(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver(), new org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterDefinitionBinder(), getCollector(), defaultColumnNameFetcher)
+ JoinedSubClassBinder joinedSubClassBinder = new JoinedSubClassBinder(metadataBuildingContext, namingStrategy, new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), columnNameForPropertyAndPathFetcher, classBinder, getCollector())
+ UnionSubclassBinder unionSubclassBinder = new UnionSubclassBinder(metadataBuildingContext, namingStrategy, classBinder, getCollector())
SingleTableSubclassBinder singleTableSubclassBinder = new SingleTableSubclassBinder(classBinder)
SubclassMappingBinder subclassMappingBinder = new SubclassMappingBinder(metadataBuildingContext, joinedSubClassBinder, unionSubclassBinder, singleTableSubclassBinder, classPropertiesBinder)
- SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource")
- RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder)
+ SubClassBinder subClassBinder = new SubClassBinder(binder.getMappingCacheHolder(), subclassMappingBinder, multiTenantFilterBinder, "dataSource", getCollector())
+ RootPersistentClassCommonValuesBinder rootPersistentClassCommonValuesBinder = new RootPersistentClassCommonValuesBinder(metadataBuildingContext, namingStrategy, identityBinder, versionBinder, classBinder, classPropertiesBinder, getCollector())
DiscriminatorPropertyBinder discriminatorPropertyBinder = new DiscriminatorPropertyBinder(metadataBuildingContext, binder.getMappingCacheHolder(), new org.grails.orm.hibernate.cfg.domainbinding.binder.ConfiguredDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder(), new ColumnConfigToColumnBinder()), new org.grails.orm.hibernate.cfg.domainbinding.binder.DefaultDiscriminatorBinder(new org.grails.orm.hibernate.cfg.domainbinding.binder.Si [...]
- RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder)
+ RootBinder rootBinder = new RootBinder("default", multiTenantFilterBinder, subClassBinder, rootPersistentClassCommonValuesBinder, discriminatorPropertyBinder, getCollector())
return [
propertyBinder: propertyBinder,
@@ -142,7 +144,7 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
protected void bindRoot(GrailsDomainBinder binder, GrailsHibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
def binders = getBinders(binder)
- binders.rootBinder.bindRoot(entity, mappings)
+ binders.rootBinder.bindRoot(entity)
}
void setupSpec() {
@@ -169,7 +171,7 @@ class MapSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def petsProp = personEntity.getPropertyByName("pets") as HibernatePersistentProperty
when:
- def collection = collectionBinder.bindCollection(petsProp, rootClass, collector, "")
+ def collection = collectionBinder.bindCollection(petsProp, rootClass, "")
then:
collection.role == "${personEntity.name}.pets".toString()
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
index 159752cc1d..56682ccb04 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
@@ -56,7 +56,7 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
new SimpleValueColumnBinder(),
new ColumnConfigToColumnBinder()
)
- binder = new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder)
+ binder = new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, grailsDomainBinder.metadataBuildingContext.metadataCollector)
}
def "test bindUnidirectionalOneToMany with join table"() {
@@ -79,7 +79,7 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
collection.setKey(new BasicValue(grailsDomainBinder.metadataBuildingContext, ownerPersistentClass.getTable()))
when:
- binder.bind(ownerToPetsProperty, mappings, collection)
+ binder.bind(ownerToPetsProperty, collection)
then:
collection.isInverse() == false
@@ -113,7 +113,7 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
collection.setKey(new BasicValue(grailsDomainBinder.metadataBuildingContext, ownerPersistentClass.getTable()))
when:
- binder.bind(ownerToPetsProperty, mappings, collection)
+ binder.bind(ownerToPetsProperty, collection)
then:
collection.isInverse() == false
(grails-core) 03/24: Introduce HibernateEnumProperty marker interface; replace isEnumType() checks with instanceof
Posted by bo...@apache.org.
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 9441d2ed2605c82ac3b1317894f7e3f04e1e399e
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 08:48:21 2026 -0600
Introduce HibernateEnumProperty marker interface; replace isEnumType() checks with instanceof
---
.../domainbinding/binder/GrailsPropertyBinder.java | 18 +++-----
.../cfg/domainbinding/binder/PropertyBinder.java | 3 +-
.../hibernate/HibernateCustomEnumProperty.java | 40 +++++++++++++++++
.../hibernate/HibernateEnumProperty.java | 35 +++++++++++++++
.../hibernate/HibernateMappingFactory.groovy | 8 +++-
.../hibernate/HibernateSimpleEnumProperty.java | 35 +++++++++++++++
.../util/PropertyFromValueCreator.java | 3 +-
.../gorm/specs/HibernateMappingFactorySpec.groovy | 51 +++++++++++++++++++++-
.../domainbinding/GrailsPropertyBinderSpec.groovy | 10 ++++-
9 files changed, 184 insertions(+), 19 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index 9bb67de3f4..63e5c95542 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -20,8 +20,7 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import jakarta.annotation.Nonnull;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
@@ -76,25 +75,20 @@ public class GrailsPropertyBinder {
Value value = null;
// 1. Create Value and apply binders (consolidated block)
- if (currentGrailsProp.isEnumType()) {
- // HibernateEnumTypeProperty
+ if (currentGrailsProp instanceof HibernateEnumProperty) {
value =
enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, path);
- } else if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne) {
- // HibernateOneToOneProperty
- if (oneToOne.isHibernateOneToOne()) {
+ } else if (currentGrailsProp instanceof HibernateToOneProperty toOne) {
+ if (toOne.isHibernateOneToOne()) {
value =
oneToOneBinder.bindOneToOne(
- (org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
+ (org.grails.datastore.mapping.model.types.OneToOne) toOne,
persistentClass,
table,
path);
} else {
- value =
- manyToOneBinder.bindManyToOne((HibernateToOneProperty) currentGrailsProp, table, path);
+ value = manyToOneBinder.bindManyToOne(toOne, table, path);
}
- } else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) {
- value = manyToOneBinder.bindManyToOne(manyToOne, table, path);
} else if (currentGrailsProp instanceof HibernateToManyProperty toMany
&& !currentGrailsProp.isSerializableType()) {
// HibernateToManyProperty
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/PropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/PropertyBinder.java
index f1416f090d..f1bbf1d290 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/PropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/PropertyBinder.java
@@ -25,6 +25,7 @@ import org.grails.datastore.mapping.reflect.EntityReflector;
import org.grails.orm.hibernate.access.TraitPropertyAccessStrategy;
import org.grails.orm.hibernate.cfg.PropertyConfig;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.CascadeBehaviorFetcher;
import org.hibernate.boot.spi.AccessType;
@@ -84,7 +85,7 @@ public class PropertyBinder {
prop.setOptional(persistentProperty.isNullable());
if (persistentProperty instanceof Association<?> association
- && !persistentProperty.isEnumType()) {
+ && !(persistentProperty instanceof HibernateEnumProperty)) {
prop.setCascade(cascadeBehaviorFetcher.getCascadeBehaviour(association));
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateCustomEnumProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateCustomEnumProperty.java
new file mode 100644
index 0000000000..0ed1ac89b4
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateCustomEnumProperty.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate;
+
+import java.beans.PropertyDescriptor;
+import org.grails.datastore.mapping.engine.types.CustomTypeMarshaller;
+import org.grails.datastore.mapping.model.MappingContext;
+import org.grails.datastore.mapping.model.PersistentEntity;
+
+/**
+ * Hibernate custom property whose Java type is an enum backed by a registered {@link
+ * CustomTypeMarshaller}. Created by {@link HibernateMappingFactory#createCustom} when {@code
+ * pd.propertyType.isEnum()} is true and a matching marshaller is found.
+ */
+public class HibernateCustomEnumProperty extends HibernateCustomProperty
+ implements HibernateEnumProperty {
+ public HibernateCustomEnumProperty(
+ PersistentEntity entity,
+ MappingContext context,
+ PropertyDescriptor property,
+ CustomTypeMarshaller<?, ?, ?> customTypeMarshaller) {
+ super(entity, context, property, customTypeMarshaller);
+ }
+}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateEnumProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateEnumProperty.java
new file mode 100644
index 0000000000..a471622545
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateEnumProperty.java
@@ -0,0 +1,35 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate;
+
+/**
+ * Marker interface for Hibernate persistent properties whose Java type is an enum.
+ *
+ * <p>Two concrete subtypes exist, corresponding to the two creation paths in {@link
+ * HibernateMappingFactory}:
+ *
+ * <ul>
+ * <li>{@link HibernateSimpleEnumProperty} — plain enum with no custom type marshaller
+ * <li>{@link HibernateCustomEnumProperty} — enum backed by a custom type marshaller
+ * </ul>
+ *
+ * <p>Use {@code instanceof HibernateEnumProperty} instead of {@code isEnumType()} to branch on
+ * enum properties at binding time.
+ */
+public interface HibernateEnumProperty extends HibernatePersistentProperty {}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingFactory.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingFactory.groovy
index 2b70647979..dc1a80c224 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingFactory.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingFactory.groovy
@@ -80,7 +80,9 @@ class HibernateMappingFactory extends AbstractGormMappingFactory<Mapping, Proper
if (customTypeMarshaller == null && propertyType.isEnum()) {
customTypeMarshaller = findCustomType(context, Enum)
}
- HibernateCustomProperty custom = new HibernateCustomProperty(owner, context, pd, customTypeMarshaller)
+ HibernateCustomProperty custom = propertyType.isEnum()
+ ? new HibernateCustomEnumProperty(owner, context, pd, customTypeMarshaller)
+ : new HibernateCustomProperty(owner, context, pd, customTypeMarshaller)
custom.setMapping(createPropertyMapping(custom, owner))
custom
}
@@ -88,7 +90,9 @@ class HibernateMappingFactory extends AbstractGormMappingFactory<Mapping, Proper
@Override
Simple<PropertyConfig> createSimple(
PersistentEntity owner, MappingContext context, PropertyDescriptor pd) {
- HibernateSimpleProperty simple = new HibernateSimpleProperty(owner, context, pd)
+ HibernateSimpleProperty simple = pd.propertyType.isEnum()
+ ? new HibernateSimpleEnumProperty(owner, context, pd)
+ : new HibernateSimpleProperty(owner, context, pd)
simple.setMapping(createPropertyMapping(simple, owner))
simple
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateSimpleEnumProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateSimpleEnumProperty.java
new file mode 100644
index 0000000000..3e0a09d221
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateSimpleEnumProperty.java
@@ -0,0 +1,35 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate;
+
+import java.beans.PropertyDescriptor;
+import org.grails.datastore.mapping.model.MappingContext;
+import org.grails.datastore.mapping.model.PersistentEntity;
+
+/**
+ * Hibernate simple property whose Java type is an enum (no custom type marshaller). Created by
+ * {@link HibernateMappingFactory#createSimple} when {@code pd.propertyType.isEnum()} is true.
+ */
+public class HibernateSimpleEnumProperty extends HibernateSimpleProperty
+ implements HibernateEnumProperty {
+ public HibernateSimpleEnumProperty(
+ PersistentEntity entity, MappingContext context, PropertyDescriptor property) {
+ super(entity, context, property);
+ }
+}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/PropertyFromValueCreator.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/PropertyFromValueCreator.java
index 916bd13837..cb6795e0d4 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/PropertyFromValueCreator.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/util/PropertyFromValueCreator.java
@@ -19,6 +19,7 @@
package org.grails.orm.hibernate.cfg.domainbinding.util;
import org.grails.orm.hibernate.cfg.domainbinding.binder.PropertyBinder;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Value;
@@ -37,7 +38,7 @@ public class PropertyFromValueCreator {
public Property createProperty(Value value, HibernatePersistentProperty grailsProperty) {
// set type
- if (!grailsProperty.isEnumType()) {
+ if (!(grailsProperty instanceof HibernateEnumProperty)) {
value.setTypeUsingReflection(grailsProperty.getOwnerClassName(), grailsProperty.getName());
}
diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/HibernateMappingFactorySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/HibernateMappingFactorySpec.groovy
index 66863795a0..3e9db5e60f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/HibernateMappingFactorySpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/HibernateMappingFactorySpec.groovy
@@ -39,7 +39,7 @@ class HibernateMappingFactorySpec extends HibernateGormDatastoreSpec {
def setupSpec() {
manager.addAllDomainClasses([MappingFactoryBook, MappingFactoryAuthor, MappingFactoryTag,
- MappingFactoryArticle])
+ MappingFactoryArticle, MappingFactoryEnumBook])
}
// --- unit-style tests (standalone factory) ---
@@ -167,6 +167,29 @@ class HibernateMappingFactorySpec extends HibernateGormDatastoreSpec {
addrProp instanceof HibernateEmbeddedProperty
}
+ void "createSimple creates HibernateSimpleEnumProperty for a plain enum field"() {
+ when:
+ PersistentEntity entity = mappingContext.getPersistentEntity(MappingFactoryEnumBook.name)
+ def statusProp = entity.persistentProperties.find { it.name == 'status' }
+
+ then:
+ statusProp instanceof HibernateSimpleEnumProperty
+ }
+
+ void "createCustom creates HibernateCustomEnumProperty for an enum field with a registered marshaller"() {
+ given:
+ HibernateConnectionSourceSettings settings = new HibernateConnectionSourceSettings()
+ settings.custom.types = [new MappingFactoryEnumMarshaller()]
+ def ctx = new HibernateMappingContext(settings)
+ PersistentEntity entity = ctx.addPersistentEntity(MappingFactoryCustomEnumBook)
+
+ when:
+ def statusProp = entity.persistentProperties.find { it.name == 'status' }
+
+ then:
+ statusProp instanceof HibernateCustomEnumProperty
+ }
+
@Rollback
void "factory-created entities can be persisted and retrieved"() {
when:
@@ -234,3 +257,29 @@ class FactoryTypeMarshaller extends AbstractMappingAwareCustomTypeMarshaller {
@Override
protected Object readInternal(PersistentProperty property, String key, Object nativeSource) { nativeSource }
}
+
+enum MappingFactoryBookStatus { AVAILABLE, CHECKED_OUT }
+
+@Entity
+class MappingFactoryEnumBook implements HibernateEntity<MappingFactoryEnumBook> {
+ String title
+ MappingFactoryBookStatus status
+}
+
+@Entity
+class MappingFactoryCustomEnumBook implements HibernateEntity<MappingFactoryCustomEnumBook> {
+ String title
+ MappingFactoryBookStatus status
+}
+
+class MappingFactoryEnumMarshaller extends AbstractMappingAwareCustomTypeMarshaller {
+ MappingFactoryEnumMarshaller() { super(MappingFactoryBookStatus) }
+
+ @Override
+ protected Object writeInternal(PersistentProperty property, String key, Object value, Object nativeTarget) { value?.name() }
+
+ @Override
+ protected Object readInternal(PersistentProperty property, String key, Object nativeSource) {
+ nativeSource ? MappingFactoryBookStatus.valueOf(nativeSource.toString()) : null
+ }
+}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
index b76048a4d6..2989687ecb 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyBinderSpec.groovy
@@ -18,6 +18,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProper
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedProperty
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateSimpleEnumProperty
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateSimpleProperty
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionBinder
import org.grails.orm.hibernate.cfg.domainbinding.binder.ClassBinder
@@ -98,6 +99,12 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
}
}
+ abstract static class TestSimpleEnum extends HibernateSimpleEnumProperty {
+ TestSimpleEnum(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) {
+ super(owner, context, descriptor);
+ }
+ }
+
private void setupProperty(PersistentProperty prop, String name, Mapping mapping, PersistentEntity owner) {
prop.getName() >> name
_ * prop.getOwner() >> owner
@@ -249,10 +256,9 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec {
rootClass.setEntityName(persistentEntity.name)
rootClass.setTable(collector.addTable(null, null, "ENUM_BOOK", null, false, binder.getMetadataBuildingContext()))
- def statusProp = Mock(TestBasic)
+ def statusProp = Mock(TestSimpleEnum)
setupProperty(statusProp, "status", new Mapping(), persistentEntity)
statusProp.getType() >> java.util.concurrent.TimeUnit
- statusProp.isEnumType() >> true
statusProp.isHibernateOneToOne() >> false
statusProp.isHibernateManyToOne() >> false
(grails-core) 15/24: refactor: extract bindOrderBy and applyMultiTenantFilter from CollectionSecondPassBinder
Posted by bo...@apache.org.
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 ec1ca22399fc798b48eafe9de0c88ef2a631e2d2
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 10:37:50 2026 -0600
refactor: extract bindOrderBy and applyMultiTenantFilter from CollectionSecondPassBinder
---
.../cfg/domainbinding/binder/CollectionBinder.java | 5 +-
.../CollectionMultiTenantFilterBinder.java | 65 +++++++++
.../secondpass/CollectionSecondPassBinder.java | 38 +-----
.../CollectionMultiTenantFilterBinderSpec.groovy | 149 +++++++++++++++++++++
.../CollectionSecondPassBinderSpec.groovy | 2 +-
5 files changed, 223 insertions(+), 36 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 93648444f9..174e31d7da 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -30,6 +30,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.BidirectionalOneToManyLinker;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionKeyColumnUpdater;
+import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionMultiTenantFilterBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionOrderByBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionSecondPassBinder;
import org.grails.orm.hibernate.cfg.domainbinding.secondpass.CollectionWithJoinTableBinder;
@@ -114,9 +115,9 @@ public class CollectionBinder {
new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
collectionWithJoinTableBinder,
collectionForPropertyConfigBinder,
- new DefaultColumnNameFetcher(namingStrategy),
simpleValueColumnBinder,
- new CollectionOrderByBinder());
+ new CollectionOrderByBinder(),
+ new CollectionMultiTenantFilterBinder(new DefaultColumnNameFetcher(namingStrategy)));
this.listSecondPassBinder =
new ListSecondPassBinder(
metadataBuildingContext,
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinder.java
new file mode 100644
index 0000000000..f228053dce
--- /dev/null
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinder.java
@@ -0,0 +1,65 @@
+/*
+ * 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 org.grails.orm.hibernate.cfg.domainbinding.secondpass;
+
+import java.util.Collections;
+import java.util.Optional;
+import org.grails.datastore.mapping.model.config.GormProperties;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
+import org.hibernate.mapping.Collection;
+
+/** Applies multi-tenant filters to a collection based on the associated entity's tenancy. */
+public class CollectionMultiTenantFilterBinder {
+
+ private final DefaultColumnNameFetcher defaultColumnNameFetcher;
+
+ /** Creates a new {@link CollectionMultiTenantFilterBinder} instance. */
+ public CollectionMultiTenantFilterBinder(DefaultColumnNameFetcher defaultColumnNameFetcher) {
+ this.defaultColumnNameFetcher = defaultColumnNameFetcher;
+ }
+
+ /** Applies the multi-tenant filter to the collection if the associated entity is multi-tenant. */
+ public void bind(HibernateToManyProperty property, Collection collection) {
+ Optional.ofNullable(property.getHibernateAssociatedEntity())
+ .filter(
+ referenced ->
+ !(property instanceof HibernateManyToManyProperty) && referenced.isMultiTenant())
+ .map(referenced -> referenced.getMultiTenantFilterCondition(defaultColumnNameFetcher))
+ .ifPresent(
+ filterCondition -> {
+ if (property.isUnidirectionalOneToMany()) {
+ collection.addManyToManyFilter(
+ GormProperties.TENANT_IDENTITY,
+ filterCondition,
+ true,
+ Collections.emptyMap(),
+ Collections.emptyMap());
+ } else {
+ collection.addFilter(
+ GormProperties.TENANT_IDENTITY,
+ filterCondition,
+ true,
+ Collections.emptyMap(),
+ Collections.emptyMap());
+ }
+ });
+ }
+}
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 83410c5573..69356c87f2 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
@@ -23,7 +23,6 @@ import static org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBind
import jakarta.annotation.Nonnull;
import java.util.*;
import java.util.Map;
-import org.grails.datastore.mapping.model.config.GormProperties;
import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionForPropertyConfigBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.ManyToOneBinder;
import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder;
@@ -31,7 +30,6 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.hibernate.MappingException;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.*;
@@ -45,8 +43,8 @@ public class CollectionSecondPassBinder {
private static final Logger LOG = LoggerFactory.getLogger(CollectionSecondPassBinder.class);
- private final DefaultColumnNameFetcher defaultColumnNameFetcher;
private final CollectionOrderByBinder collectionOrderByBinder;
+ private final CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder;
private final ManyToOneBinder manyToOneBinder;
private final PrimaryKeyValueCreator primaryKeyValueCreator;
private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
@@ -67,9 +65,9 @@ public class CollectionSecondPassBinder {
UnidirectionalOneToManyBinder unidirectionalOneToManyBinder,
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
- DefaultColumnNameFetcher defaultColumnNameFetcher,
SimpleValueColumnBinder simpleValueColumnBinder,
- CollectionOrderByBinder collectionOrderByBinder) {
+ CollectionOrderByBinder collectionOrderByBinder,
+ CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder) {
this.manyToOneBinder = manyToOneBinder;
this.primaryKeyValueCreator = primaryKeyValueCreator;
this.collectionKeyColumnUpdater = collectionKeyColumnUpdater;
@@ -78,9 +76,9 @@ public class CollectionSecondPassBinder {
this.unidirectionalOneToManyBinder = unidirectionalOneToManyBinder;
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
- this.defaultColumnNameFetcher = defaultColumnNameFetcher;
this.simpleValueColumnBinder = simpleValueColumnBinder;
this.collectionOrderByBinder = collectionOrderByBinder;
+ this.collectionMultiTenantFilterBinder = collectionMultiTenantFilterBinder;
}
/** Bind collection second pass. */
@@ -94,8 +92,7 @@ public class CollectionSecondPassBinder {
collectionOrderByBinder.bind(property, collection, associatedClass);
bindOneToManyAssociation(property, associatedClass, collection);
- applyMultiTenantFilter(property, collection);
-
+ collectionMultiTenantFilterBinder.bind(property, collection);
if (property.isSorted()) {
collection.setSorted(true);
}
@@ -126,31 +123,6 @@ public class CollectionSecondPassBinder {
collectionForPropertyConfigBinder.bindCollectionForPropertyConfig(collection, property);
}
- private void applyMultiTenantFilter(HibernateToManyProperty property, Collection collection) {
- Optional.ofNullable(property.getHibernateAssociatedEntity())
- .filter(
- referenced ->
- !(property instanceof HibernateManyToManyProperty) && referenced.isMultiTenant())
- .map(referenced -> referenced.getMultiTenantFilterCondition(defaultColumnNameFetcher))
- .ifPresent(
- filterCondition -> {
- if (property.isUnidirectionalOneToMany()) {
- collection.addManyToManyFilter(
- GormProperties.TENANT_IDENTITY,
- filterCondition,
- true,
- Collections.emptyMap(),
- Collections.emptyMap());
- } else {
- collection.addFilter(
- GormProperties.TENANT_IDENTITY,
- filterCondition,
- true,
- Collections.emptyMap(),
- Collections.emptyMap());
- }
- });
- }
private void bindCollectionKey(
HibernateToManyProperty property,
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinderSpec.groovy
new file mode 100644
index 0000000000..7442b848db
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionMultiTenantFilterBinderSpec.groovy
@@ -0,0 +1,149 @@
+package org.grails.orm.hibernate.cfg.domainbinding.secondpass
+
+import grails.gorm.MultiTenant
+import grails.gorm.annotation.Entity
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.datastore.mapping.model.config.GormProperties
+import org.grails.datastore.mapping.multitenancy.MultiTenancySettings
+import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover
+import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher
+import org.hibernate.mapping.Bag
+import spock.lang.Subject
+
+class CollectionMultiTenantFilterBinderSpec extends HibernateGormDatastoreSpec {
+
+ @Subject
+ CollectionMultiTenantFilterBinder binder
+
+ void setupSpec() {
+ manager.addAllDomainClasses([
+ CMTBBidirectionalOwner,
+ CMTBBidirectionalItem,
+ CMTBUnidirectionalOwner,
+ CMTBUnidirectionalItem,
+ CMTBNonTenantOwner,
+ CMTBNonTenantItem,
+ CMTBManyToManyOwner,
+ CMTBManyToManyItem,
+ ])
+ manager.grailsConfig = [
+ "grails.gorm.multiTenancy.mode" : MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR,
+ "grails.gorm.multiTenancy.tenantResolverClass": SystemPropertyTenantResolver,
+ ]
+ }
+
+ void setup() {
+ def ns = getGrailsDomainBinder().getNamingStrategy()
+ binder = new CollectionMultiTenantFilterBinder(new DefaultColumnNameFetcher(ns, new BackticksRemover()))
+ }
+
+ private HibernateToManyProperty propertyFor(Class ownerClass, String name = "items") {
+ (getPersistentEntity(ownerClass) as GrailsHibernatePersistentEntity).getPropertyByName(name) as HibernateToManyProperty
+ }
+
+ def "bind adds collection filter for bidirectional one-to-many to multi-tenant entity"() {
+ given:
+ def property = propertyFor(CMTBBidirectionalOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getFilters().any { it.getName() == GormProperties.TENANT_IDENTITY }
+ collection.getManyToManyFilters().isEmpty()
+ }
+
+ def "bind adds manyToMany filter for unidirectional one-to-many to multi-tenant entity"() {
+ given:
+ def property = propertyFor(CMTBUnidirectionalOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getManyToManyFilters().any { it.getName() == GormProperties.TENANT_IDENTITY }
+ collection.getFilters().isEmpty()
+ }
+
+ def "bind does not add filter for ManyToMany even when associated entity is multi-tenant"() {
+ given:
+ def property = propertyFor(CMTBManyToManyOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getFilters().isEmpty()
+ collection.getManyToManyFilters().isEmpty()
+ }
+
+ def "bind does not add filter when associated entity is not multi-tenant"() {
+ given:
+ def property = propertyFor(CMTBNonTenantOwner)
+ def collection = new Bag(getGrailsDomainBinder().getMetadataBuildingContext(), null)
+
+ when:
+ binder.bind(property, collection)
+
+ then:
+ collection.getFilters().isEmpty()
+ collection.getManyToManyFilters().isEmpty()
+ }
+}
+
+@Entity
+class CMTBBidirectionalOwner {
+ Long id
+ static hasMany = [items: CMTBBidirectionalItem]
+}
+
+@Entity
+class CMTBBidirectionalItem implements MultiTenant<CMTBBidirectionalItem> {
+ Long id
+ Long tenantId
+ CMTBBidirectionalOwner owner
+ static belongsTo = [owner: CMTBBidirectionalOwner]
+}
+
+@Entity
+class CMTBUnidirectionalOwner {
+ Long id
+ static hasMany = [items: CMTBUnidirectionalItem]
+}
+
+@Entity
+class CMTBUnidirectionalItem implements MultiTenant<CMTBUnidirectionalItem> {
+ Long id
+ Long tenantId
+}
+
+@Entity
+class CMTBNonTenantOwner {
+ Long id
+ static hasMany = [items: CMTBNonTenantItem]
+}
+
+@Entity
+class CMTBNonTenantItem {
+ Long id
+ String name
+}
+
+@Entity
+class CMTBManyToManyOwner {
+ Long id
+ static hasMany = [items: CMTBManyToManyItem]
+}
+
+@Entity
+class CMTBManyToManyItem implements MultiTenant<CMTBManyToManyItem> {
+ Long id
+ Long tenantId
+ static hasMany = [owners: CMTBManyToManyOwner]
+}
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 09f56705c9..bce105641c 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
@@ -41,7 +41,7 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
- binder = new CollectionSecondPassBinder(mtob, pkvc, cku, botml, dkvb, uotmb, cwjtb, cfpcb, dcnf, svcb, new CollectionOrderByBinder())
+ binder = new CollectionSecondPassBinder(mtob, pkvc, cku, botml, dkvb, uotmb, cwjtb, cfpcb, svcb, new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {
(grails-core) 05/24: Clean up GrailsPropertyBinder/ManyToOneBinder/OneToOneBinder to use concrete types; eliminate OneToOne cast and HibernateToOneProperty overload
Posted by bo...@apache.org.
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 05fcd164f06a5f9a05d24d7b6bcc01fadb8c0905
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Mon Mar 2 10:30:40 2026 -0600
Clean up GrailsPropertyBinder/ManyToOneBinder/OneToOneBinder to use concrete types; eliminate OneToOne cast and HibernateToOneProperty overload
---
.../domainbinding/binder/GrailsPropertyBinder.java | 21 ++++-----
.../cfg/domainbinding/binder/ManyToOneBinder.java | 19 ++++++--
.../cfg/domainbinding/binder/OneToOneBinder.java | 8 ++--
.../secondpass/CollectionSecondPassBinder.java | 5 +-
.../cfg/domainbinding/OneToOneBinderSpec.groovy | 53 ++++++++++++----------
5 files changed, 60 insertions(+), 46 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
index 4d949b2746..e7332f0b24 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsPropertyBinder.java
@@ -21,9 +21,10 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import jakarta.annotation.Nonnull;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
@@ -78,17 +79,13 @@ public class GrailsPropertyBinder {
if (currentGrailsProp instanceof HibernateEnumProperty) {
value =
enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, path);
- } else if (currentGrailsProp instanceof HibernateToOneProperty toOne) {
- if (toOne.isValidHibernateOneToOne()) {
- value =
- oneToOneBinder.bindOneToOne(
- (org.grails.datastore.mapping.model.types.OneToOne) toOne,
- persistentClass,
- table,
- path);
- } else {
- value = manyToOneBinder.bindManyToOne(toOne, table, path);
- }
+ } else if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne
+ && oneToOne.isValidHibernateOneToOne()) {
+ value = oneToOneBinder.bindOneToOne(oneToOne, persistentClass, table, path);
+ } else if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne) {
+ value = manyToOneBinder.bindManyToOne(oneToOne, table, path);
+ } else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) {
+ value = manyToOneBinder.bindManyToOne(manyToOne, table, path);
} else if (currentGrailsProp instanceof HibernateToManyProperty toMany
&& !currentGrailsProp.isSerializableType()) {
// HibernateToManyProperty
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
index 9dd8fe4f9f..bad0ebc63e 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/ManyToOneBinder.java
@@ -29,6 +29,7 @@ import org.grails.orm.hibernate.cfg.PropertyConfig;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.SimpleValueColumnFetcher;
@@ -77,14 +78,24 @@ public class ManyToOneBinder {
new SimpleValueColumnFetcher());
}
- /** Binds a to-one (many-to-one or one-to-one) association. */
+ /** Binds a many-to-one association. */
public ManyToOne bindManyToOne(
- HibernateToOneProperty property, org.hibernate.mapping.Table table, String path) {
+ HibernateManyToOneProperty property, org.hibernate.mapping.Table table, String path) {
+ GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
+ return doBind(property, refDomainClass, isCompositeIdentifier(refDomainClass), table, path);
+ }
+
+ /**
+ * Binds a one-to-one association where the foreign key resides on the other side (not a true
+ * Hibernate one-to-one), i.e. {@code isHibernateOneToOne()} is false.
+ */
+ public ManyToOne bindManyToOne(
+ HibernateOneToOneProperty property, org.hibernate.mapping.Table table, String path) {
GrailsHibernatePersistentEntity refDomainClass = property.getHibernateAssociatedEntity();
boolean isComposite = isCompositeIdentifier(refDomainClass);
ManyToOne manyToOne = doBind(property, refDomainClass, isComposite, table, path);
- if (property instanceof HibernateOneToOneProperty oneToOne && !isComposite) {
- bindOneToOneUniqueKey(oneToOne, manyToOne);
+ if (!isComposite) {
+ bindOneToOneUniqueKey(property, manyToOne);
}
return manyToOne;
}
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
index 40f3f763a3..236e2a6e07 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/OneToOneBinder.java
@@ -21,6 +21,7 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder;
import org.grails.datastore.mapping.model.types.Association;
import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy;
import org.grails.orm.hibernate.cfg.PropertyConfig;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.hibernate.FetchMode;
import org.hibernate.boot.spi.MetadataBuildingContext;
@@ -50,12 +51,12 @@ public class OneToOneBinder {
}
public OneToOne bindOneToOne(
- final org.grails.datastore.mapping.model.types.OneToOne property,
+ final HibernateOneToOneProperty property,
PersistentClass owner,
org.hibernate.mapping.Table table,
String path) {
OneToOne oneToOne = new OneToOne(metadataBuildingContext, table, owner);
- PropertyConfig config = ((HibernatePersistentProperty) property).getMappedForm();
+ PropertyConfig config = property.getMappedForm();
final Association otherSide = property.getInverseSide();
final boolean hasOne = otherSide != null && otherSide.isHasOne();
@@ -78,8 +79,7 @@ public class OneToOneBinder {
oneToOne.setReferenceToPrimaryKey(false);
if (hasOne || otherSide == null) {
- simpleValueBinder.bindSimpleValue(
- (HibernatePersistentProperty) property, null, oneToOne, path);
+ simpleValueBinder.bindSimpleValue(property, null, oneToOne, path);
} else {
oneToOne.setReferencedPropertyName(otherSide.getName());
}
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 cb3ce556f7..9b54553ca5 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
@@ -33,7 +33,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyP
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToOneProperty;
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty;
import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher;
import org.grails.orm.hibernate.cfg.domainbinding.util.OrderByClauseBuilder;
import org.hibernate.MappingException;
@@ -208,7 +208,8 @@ public class CollectionSecondPassBinder {
private void bindBidirectionalMapElement(
HibernateToManyProperty property, Collection collection) {
- HibernateToOneProperty otherSide = (HibernateToOneProperty) property.getHibernateInverseSide();
+ HibernateManyToOneProperty otherSide =
+ (HibernateManyToOneProperty) property.getHibernateInverseSide();
ManyToOne element =
manyToOneBinder.bindManyToOne(otherSide, collection.getCollectionTable(), EMPTY_PATH);
element.setReferencedEntityName(otherSide.getOwner().getName());
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
index de96426166..4b5e9d2073 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/OneToOneBinderSpec.groovy
@@ -2,8 +2,10 @@ package org.grails.orm.hibernate.cfg.domainbinding
import grails.gorm.specs.HibernateGormDatastoreSpec
import org.grails.datastore.mapping.model.types.OneToOne as GormOneToOne
+import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
-import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentProperty
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
import org.grails.orm.hibernate.cfg.PropertyConfig
import org.hibernate.FetchMode
import org.hibernate.mapping.OneToOne as HibernateOneToOne
@@ -29,8 +31,8 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
given:
def metadataBuildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
def ownerRoot = new RootClass(metadataBuildingContext)
-
- def gormOneToOne = Mock(GormOneToOne, additionalInterfaces: [HibernatePersistentProperty])
+
+ def gormOneToOne = Mock(TestOneToOne)
def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
def otherOwner = Mock(PersistentEntity)
@@ -38,17 +40,16 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getName() >> "myOneToOne"
gormOneToOne.getOwner() >> owner
-
+ gormOneToOne.getMappedForm() >> new PropertyConfig()
+
otherSide.isHasOne() >> false
otherSide.getOwner() >> otherOwner
otherSide.getName() >> "otherSide"
-
- otherOwner.getName() >> "OtherEntity"
- ((HibernatePersistentProperty)gormOneToOne).getMappedForm() >> new PropertyConfig()
+ otherOwner.getName() >> "OtherEntity"
when:
- def hibernateOneToOne = binder.bindOneToOne(gormOneToOne as GormOneToOne, ownerRoot, null, "")
+ def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
then:
hibernateOneToOne instanceof HibernateOneToOne
@@ -65,8 +66,8 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
given:
def metadataBuildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
def ownerRoot = new RootClass(metadataBuildingContext)
-
- def gormOneToOne = Mock(GormOneToOne, additionalInterfaces: [HibernatePersistentProperty])
+
+ def gormOneToOne = Mock(TestOneToOne)
def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
def otherOwner = Mock(PersistentEntity)
@@ -74,17 +75,15 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getName() >> "myOneToOne"
gormOneToOne.getOwner() >> owner
-
+ gormOneToOne.getMappedForm() >> new PropertyConfig()
+
otherSide.isHasOne() >> true
otherSide.getOwner() >> otherOwner
-
- otherOwner.getName() >> "OtherEntity"
- def propertyConfig = new PropertyConfig()
- ((HibernatePersistentProperty)gormOneToOne).getMappedForm() >> propertyConfig
+ otherOwner.getName() >> "OtherEntity"
when:
- def hibernateOneToOne = binder.bindOneToOne(gormOneToOne as GormOneToOne, ownerRoot, null, "")
+ def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
then:
hibernateOneToOne.isConstrained()
@@ -96,26 +95,32 @@ class OneToOneBinderSpec extends HibernateGormDatastoreSpec {
given:
def metadataBuildingContext = getGrailsDomainBinder().getMetadataBuildingContext()
def ownerRoot = new RootClass(metadataBuildingContext)
-
- def gormOneToOne = Mock(GormOneToOne, additionalInterfaces: [HibernatePersistentProperty])
+
+ def propertyConfig = new PropertyConfig()
+ propertyConfig.setFetch("join")
+
+ def gormOneToOne = Mock(TestOneToOne)
def otherSide = Mock(GormOneToOne)
def owner = Mock(PersistentEntity)
def otherOwner = Mock(PersistentEntity)
-
- def propertyConfig = new PropertyConfig()
- propertyConfig.setFetch("join")
gormOneToOne.getInverseSide() >> otherSide
gormOneToOne.getOwner() >> owner
+ gormOneToOne.getMappedForm() >> propertyConfig
+
otherSide.getOwner() >> otherOwner
otherSide.isHasOne() >> false
-
- ((HibernatePersistentProperty)gormOneToOne).getMappedForm() >> propertyConfig
when:
- def hibernateOneToOne = binder.bindOneToOne(gormOneToOne as GormOneToOne, ownerRoot, null, "")
+ def hibernateOneToOne = binder.bindOneToOne(gormOneToOne, ownerRoot, null, "")
then:
hibernateOneToOne.getFetchMode() == FetchMode.JOIN
}
}
+
+abstract class TestOneToOne extends HibernateOneToOneProperty {
+ TestOneToOne(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) {
+ super(owner, context, descriptor)
+ }
+}
(grails-core) 21/24: refactor: move CollectionKeyBinder into CollectionKeyColumnUpdater
Posted by bo...@apache.org.
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 66435d5a3562e87ec9132019c6f96ef23ef4581e
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Tue Mar 3 13:59:43 2026 -0600
refactor: move CollectionKeyBinder into CollectionKeyColumnUpdater
---
.../cfg/domainbinding/binder/CollectionBinder.java | 12 ++++++------
.../secondpass/CollectionKeyColumnUpdater.java | 18 ++++++++++++++++++
.../secondpass/CollectionSecondPassBinder.java | 9 +++------
.../secondpass/CollectionKeyColumnUpdaterSpec.groovy | 20 +++++++++++++++++++-
.../secondpass/CollectionSecondPassBinderSpec.groovy | 4 ++--
5 files changed, 48 insertions(+), 15 deletions(-)
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
index 7749b30556..b397d360ad 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java
@@ -110,15 +110,15 @@ public class CollectionBinder {
new ColumnConfigToColumnBinder());
this.collectionSecondPassBinder =
new CollectionSecondPassBinder(
- new CollectionKeyColumnUpdater(),
+ new CollectionKeyColumnUpdater(
+ new CollectionKeyBinder(
+ new BidirectionalOneToManyLinker(grailsPropertyResolver),
+ new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
+ simpleValueColumnBinder,
+ new PrimaryKeyValueCreator(metadataBuildingContext))),
new UnidirectionalOneToManyBinder(collectionWithJoinTableBinder, mappings),
collectionWithJoinTableBinder,
collectionForPropertyConfigBinder,
- new CollectionKeyBinder(
- new BidirectionalOneToManyLinker(grailsPropertyResolver),
- new DependentKeyValueBinder(simpleValueBinder, compositeIdentifierToManyToOneBinder),
- simpleValueColumnBinder,
- new PrimaryKeyValueCreator(metadataBuildingContext)),
new BidirectionalMapElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new ManyToManyElementBinder(manyToOneBinder, collectionForPropertyConfigBinder),
new CollectionOrderByBinder(),
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
index 0d9f2f55e3..64fe89ac08 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdater.java
@@ -21,11 +21,29 @@ package org.grails.orm.hibernate.cfg.domainbinding.secondpass;
import java.util.Objects;
import java.util.stream.StreamSupport;
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty;
+import org.hibernate.mapping.Collection;
import org.hibernate.mapping.DependantValue;
+import org.hibernate.mapping.PersistentClass;
/** Forces columns to be nullable and checks if the key is updateable. */
public class CollectionKeyColumnUpdater {
+ private final CollectionKeyBinder collectionKeyBinder;
+
+ /** Creates a new {@link CollectionKeyColumnUpdater} instance. */
+ public CollectionKeyColumnUpdater(CollectionKeyBinder collectionKeyBinder) {
+ this.collectionKeyBinder = collectionKeyBinder;
+ }
+
+ /** Creates the key, sets it on the collection, and updates its columns. */
+ public void bind(
+ HibernateToManyProperty property,
+ PersistentClass associatedClass,
+ Collection collection) {
+ DependantValue key = collectionKeyBinder.bind(property, associatedClass, collection);
+ forceNullableAndCheckUpdatable(key, property);
+ }
+
/** Force nullable and check updatable. */
public void forceNullableAndCheckUpdatable(DependantValue key, HibernateToManyProperty property) {
key.getColumns().stream()
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 4507e52732..26b2f34cd0 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
@@ -35,10 +35,9 @@ import org.hibernate.mapping.Collection;
public class CollectionSecondPassBinder {
private final CollectionOrderByBinder collectionOrderByBinder;
private final CollectionMultiTenantFilterBinder collectionMultiTenantFilterBinder;
- private final CollectionKeyBinder collectionKeyBinder;
+ private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
private final BidirectionalMapElementBinder bidirectionalMapElementBinder;
private final ManyToManyElementBinder manyToManyElementBinder;
- private final CollectionKeyColumnUpdater collectionKeyColumnUpdater;
private final UnidirectionalOneToManyBinder unidirectionalOneToManyBinder;
private final CollectionWithJoinTableBinder collectionWithJoinTableBinder;
private final CollectionForPropertyConfigBinder collectionForPropertyConfigBinder;
@@ -49,7 +48,6 @@ public class CollectionSecondPassBinder {
UnidirectionalOneToManyBinder unidirectionalOneToManyBinder,
CollectionWithJoinTableBinder collectionWithJoinTableBinder,
CollectionForPropertyConfigBinder collectionForPropertyConfigBinder,
- CollectionKeyBinder collectionKeyBinder,
BidirectionalMapElementBinder bidirectionalMapElementBinder,
ManyToManyElementBinder manyToManyElementBinder,
CollectionOrderByBinder collectionOrderByBinder,
@@ -58,7 +56,6 @@ public class CollectionSecondPassBinder {
this.unidirectionalOneToManyBinder = unidirectionalOneToManyBinder;
this.collectionWithJoinTableBinder = collectionWithJoinTableBinder;
this.collectionForPropertyConfigBinder = collectionForPropertyConfigBinder;
- this.collectionKeyBinder = collectionKeyBinder;
this.bidirectionalMapElementBinder = bidirectionalMapElementBinder;
this.manyToManyElementBinder = manyToManyElementBinder;
this.collectionOrderByBinder = collectionOrderByBinder;
@@ -79,11 +76,11 @@ public class CollectionSecondPassBinder {
collectionMultiTenantFilterBinder.bind(property, collection);
collection.setSorted(property.isSorted());
- DependantValue key = collectionKeyBinder.bind(property, associatedClass, collection);
+ collectionKeyColumnUpdater.bind(property, associatedClass, collection);
collection.setCacheConcurrencyStrategy(property.getCacheUSage());
bindCollectionElement(property, mappings, collection);
- collectionKeyColumnUpdater.forceNullableAndCheckUpdatable(key, property);
+
}
private void bindOneToManyAssociation(
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdaterSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdaterSpec.groovy
index 0ba9db6f09..dc5e411c2b 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdaterSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/CollectionKeyColumnUpdaterSpec.groovy
@@ -2,7 +2,11 @@ package org.grails.orm.hibernate.cfg.domainbinding.secondpass
import grails.gorm.annotation.Entity
import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.grails.orm.hibernate.cfg.domainbinding.binder.CompositeIdentifierToManyToOneBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueBinder
+import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder
import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty
+import org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver
import org.hibernate.mapping.Column
import org.hibernate.mapping.DependantValue
import spock.lang.Subject
@@ -10,7 +14,21 @@ import spock.lang.Subject
class CollectionKeyColumnUpdaterSpec extends HibernateGormDatastoreSpec {
@Subject
- CollectionKeyColumnUpdater updater = new CollectionKeyColumnUpdater()
+ CollectionKeyColumnUpdater updater
+
+ void setup() {
+ def gdb = getGrailsDomainBinder()
+ def mbc = gdb.getMetadataBuildingContext()
+ def ns = gdb.getNamingStrategy()
+ def je = gdb.getJdbcEnvironment()
+ def svb = new SimpleValueBinder(mbc, ns, je)
+ def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
+ def botml = new BidirectionalOneToManyLinker(new GrailsPropertyResolver())
+ def dkvb = new DependentKeyValueBinder(svb, citmto)
+ def svcb = new SimpleValueColumnBinder()
+ def pkvc = new PrimaryKeyValueCreator(mbc)
+ updater = new CollectionKeyColumnUpdater(new CollectionKeyBinder(botml, dkvb, svcb, pkvc))
+ }
void "test forceNullableAndCheckUpdateable with single unidirectional association"() {
given:
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 46a18eba1c..d89e94b226 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
@@ -32,7 +32,6 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def citmto = new CompositeIdentifierToManyToOneBinder(mbc, ns, je)
def mtob = new ManyToOneBinder(mbc, ns, svb, new ManyToOneValuesBinder(), citmto)
def pkvc = new PrimaryKeyValueCreator(mbc)
- def cku = new CollectionKeyColumnUpdater()
def botml = new BidirectionalOneToManyLinker(new org.grails.orm.hibernate.cfg.domainbinding.util.GrailsPropertyResolver())
def dkvb = new DependentKeyValueBinder(svb, citmto)
def cwjtb = new CollectionWithJoinTableBinder(mbc, ns, new UnidirectionalOneToManyInverseValuesBinder(), null, citmto, svcf, new CollectionForPropertyConfigBinder(), new SimpleValueColumnBinder(), null)
@@ -40,8 +39,9 @@ class CollectionSecondPassBinderSpec extends HibernateGormDatastoreSpec {
def cfpcb = new CollectionForPropertyConfigBinder()
def dcnf = new DefaultColumnNameFetcher(ns, new BackticksRemover())
def svcb = new SimpleValueColumnBinder()
+ def cku = new CollectionKeyColumnUpdater(new CollectionKeyBinder(botml, dkvb, svcb, pkvc))
- binder = new CollectionSecondPassBinder(cku, uotmb, cwjtb, cfpcb, new CollectionKeyBinder(botml, dkvb, svcb, pkvc), new BidirectionalMapElementBinder(mtob, cfpcb), new ManyToManyElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
+ binder = new CollectionSecondPassBinder(cku, uotmb, cwjtb, cfpcb, new BidirectionalMapElementBinder(mtob, cfpcb), new ManyToManyElementBinder(mtob, cfpcb), new CollectionOrderByBinder(), new CollectionMultiTenantFilterBinder(dcnf))
}
protected HibernatePersistentProperty createTestHibernateToManyProperty(Class<?> domainClass = CSPBTestEntityWithMany, String propertyName = "items") {