This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit bf226699346e3a47082b93c8bcb7cf3f7b6c6213 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Mon Feb 16 11:40:50 2026 -0600 Refactor collection and association binding in Hibernate 7 - Encapsulate Hibernate Collection creation and mapping registration within CollectionBinder. - Simplify GrailsPropertyBinder and ComponentBinder by delegating collection creation. - Consolidate serializable collection handling to map as simple values. - Decouple ComponentBinder and GrailsPropertyBinder from CollectionHolder. --- .../orm/hibernate/cfg/GrailsDomainBinder.java | 5 +- .../cfg/domainbinding/binder/CollectionBinder.java | 19 ++++-- .../cfg/domainbinding/binder/ComponentBinder.java | 56 ++++++++---------- .../domainbinding/binder/CompositeIdBinder.java | 2 +- .../domainbinding/binder/GrailsPropertyBinder.java | 67 ++++++++++------------ .../cfg/domainbinding/ComponentBinderSpec.groovy | 1 + .../cfg/domainbinding/CompositeIdBinderSpec.groovy | 2 +- .../domainbinding/GrailsPropertyBinderSpec.groovy | 62 +++++++++++++++++++- 8 files changed, 134 insertions(+), 80 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java index 440eec5b4d..1a33e52ecf 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java @@ -182,13 +182,13 @@ public class GrailsDomainBinder manyToOneBinder, compositeIdentifierToManyToOneBinder, simpleValueColumnFetcher, - columnNameForPropertyAndPathFetcher + columnNameForPropertyAndPathFetcher, + collectionHolder ); ComponentUpdater componentUpdater = new ComponentUpdater(propertyFromValueCreator); ComponentBinder componentBinder = new ComponentBinder( metadataBuildingContext, getMappingCacheHolder(), - collectionHolder, enumTypeBinder, collectionBinder, simpleValueBinder, @@ -201,7 +201,6 @@ public class GrailsDomainBinder GrailsPropertyBinder grailsPropertyBinder = new GrailsPropertyBinder( metadataBuildingContext, namingStrategy, - collectionHolder, enumTypeBinder, componentBinder, collectionBinder, 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 3b49d3b89b..529ccffbff 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 @@ -23,6 +23,8 @@ import org.grails.orm.hibernate.cfg.domainbinding.secondpass.MapSecondPass; import org.grails.orm.hibernate.cfg.domainbinding.secondpass.MapSecondPassBinder; import org.grails.orm.hibernate.cfg.domainbinding.secondpass.SetSecondPass; +import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionHolder; +import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionType; import org.hibernate.FetchMode; import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.MetadataBuildingContext; @@ -43,6 +45,7 @@ public class CollectionBinder { private final MetadataBuildingContext metadataBuildingContext; private final PersistentEntityNamingStrategy namingStrategy; + private final CollectionHolder collectionHolder; private final ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher; private final ListSecondPassBinder listSecondPassBinder; private final CollectionSecondPassBinder collectionSecondPassBinder; @@ -57,10 +60,12 @@ public class CollectionBinder { ManyToOneBinder manyToOneBinder, CompositeIdentifierToManyToOneBinder compositeIdentifierToManyToOneBinder, SimpleValueColumnFetcher simpleValueColumnFetcher, - ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher) { + ColumnNameForPropertyAndPathFetcher columnNameForPropertyAndPathFetcher, + CollectionHolder collectionHolder) { this.metadataBuildingContext = metadataBuildingContext; this.namingStrategy = namingStrategy; this.columnNameForPropertyAndPathFetcher = columnNameForPropertyAndPathFetcher; + this.collectionHolder = collectionHolder; this.collectionSecondPassBinder = new CollectionSecondPassBinder( metadataBuildingContext, namingStrategy, @@ -75,27 +80,29 @@ public class CollectionBinder { this.mapSecondPassBinder = new MapSecondPassBinder(metadataBuildingContext, namingStrategy, collectionSecondPassBinder); } - public CollectionBinder(MetadataBuildingContext metadataBuildingContext, PersistentEntityNamingStrategy namingStrategy, JdbcEnvironment jdbcEnvironment) { + public CollectionBinder(MetadataBuildingContext metadataBuildingContext, PersistentEntityNamingStrategy namingStrategy, JdbcEnvironment jdbcEnvironment, CollectionHolder collectionHolder) { this(metadataBuildingContext, namingStrategy, jdbcEnvironment, new SimpleValueBinder(namingStrategy, jdbcEnvironment), new EnumTypeBinder(metadataBuildingContext, new ColumnNameForPropertyAndPathFetcher(namingStrategy, new DefaultColumnNameFetcher(namingStrategy), new BackticksRemover())), new ManyToOneBinder(metadataBuildingContext, namingStrategy, jdbcEnvironment), new CompositeIdentifierToManyToOneBinder(namingStrategy, jdbcEnvironment), new SimpleValueColumnFetcher(), - new ColumnNameForPropertyAndPathFetcher(namingStrategy, new DefaultColumnNameFetcher(namingStrategy), new BackticksRemover())); + new ColumnNameForPropertyAndPathFetcher(namingStrategy, new DefaultColumnNameFetcher(namingStrategy), new BackticksRemover()), + collectionHolder); } /** * First pass to bind collection to Hibernate metamodel, sets up second pass * * @param property The GrailsDomainClassProperty instance - * @param collection The collection * @param owner The owning persistent class * @param mappings The Hibernate mappings instance * @param path The property path */ - public void bindCollection(HibernateToManyProperty property, Collection collection, + public Collection bindCollection(HibernateToManyProperty property, PersistentClass owner, @Nonnull InFlightMetadataCollector mappings, String path) { + CollectionType collectionType = collectionHolder.get(property.getType()); + Collection collection = collectionType.create(property, owner); // set role String propertyName = getNameForPropertyAndPath(property, path); @@ -148,6 +155,8 @@ public class CollectionBinder { else { // Collection -> Bag mappings.addSecondPass(new SetSecondPass(collectionSecondPassBinder, property, mappings, collection)); } + mappings.addCollectionBinding(collection); + return collection; } 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 a565011b72..34020c123c 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 @@ -5,8 +5,6 @@ import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Collection; import org.hibernate.mapping.Component; -import org.hibernate.mapping.ManyToOne; -import org.hibernate.mapping.OneToOne; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Table; @@ -20,13 +18,11 @@ import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity; import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty; import org.grails.orm.hibernate.cfg.GrailsHibernateUtil; import org.grails.orm.hibernate.cfg.MappingCacheHolder; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty; 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.HibernateOneToManyProperty; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty; -import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionHolder; -import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionType; import org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher; import jakarta.annotation.Nonnull; @@ -37,7 +33,6 @@ public class ComponentBinder { private final MetadataBuildingContext metadataBuildingContext; private final MappingCacheHolder mappingCacheHolder; - private final CollectionHolder collectionHolder; private final EnumTypeBinder enumTypeBinder; private final CollectionBinder collectionBinder; private final ManyToOneBinder manyToOneBinder; @@ -48,7 +43,6 @@ public class ComponentBinder { public ComponentBinder(MetadataBuildingContext metadataBuildingContext, MappingCacheHolder mappingCacheHolder, - CollectionHolder collectionHolder, EnumTypeBinder enumTypeBinder, CollectionBinder collectionBinder, SimpleValueBinder simpleValueBinder, @@ -58,7 +52,6 @@ public class ComponentBinder { ComponentUpdater componentUpdater) { this.metadataBuildingContext = metadataBuildingContext; this.mappingCacheHolder = mappingCacheHolder; - this.collectionHolder = collectionHolder; this.enumTypeBinder = enumTypeBinder; this.collectionBinder = collectionBinder; this.simpleValueBinder = simpleValueBinder; @@ -93,55 +86,52 @@ public class ComponentBinder { component.setParentProperty(currentGrailsProp.getName()); continue; } - var value = bindComponentProperty(component, property, currentGrailsProp, persistentClass, path, table, mappings); + var value = bindComponentProperty(property, currentGrailsProp, persistentClass, path, table, mappings); componentUpdater.updateComponent(component, property, currentGrailsProp, value); } return component; } - public Value bindComponentProperty(Component component, - GrailsHibernatePersistentProperty componentProperty, + public Value bindComponentProperty(GrailsHibernatePersistentProperty componentProperty, GrailsHibernatePersistentProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table, @Nonnull InFlightMetadataCollector mappings) { Value value; - // see if it's a collection type - CollectionType collectionType = collectionHolder.get(currentGrailsProp.getType()); - if (collectionType != null) { - //HibernateToManyProperty - // create collection - Collection collection = collectionType.create((HibernateToManyProperty) currentGrailsProp, persistentClass); - collectionBinder.bindCollection((HibernateToManyProperty) currentGrailsProp, collection, persistentClass, mappings, path); - mappings.addCollectionBinding(collection); - value = collection; - } // work out what type of relationship it is and bind value - else if (currentGrailsProp.isHibernateOneToOne()) { + if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne) { //HibernateOneToOneProperty - if (LOG.isDebugEnabled()) - LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne"); + if (oneToOne.isHibernateOneToOne()) { + if (LOG.isDebugEnabled()) + LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne"); + + value = oneToOneBinder.bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp, persistentClass, table, path); + } else { + if (LOG.isDebugEnabled()) + LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne"); - value = oneToOneBinder.bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp, persistentClass, table, path); - } else if (currentGrailsProp.isHibernateManyToOne()) { + value = manyToOneBinder.bindManyToOne((Association) currentGrailsProp, table, path); + } + } else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) { //HibernateManyToOneProperty if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne"); value = manyToOneBinder.bindManyToOne((Association) currentGrailsProp, table, path); - } - else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) { + } else if (currentGrailsProp instanceof HibernateToManyProperty toMany && !currentGrailsProp.isSerializableType()) { + //HibernateToManyProperty + value = collectionBinder.bindCollection(toMany, persistentClass, mappings, path); + } else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) { value = bindComponent(persistentClass, embedded, mappings); - } - else if (currentGrailsProp.getType().isEnum()) { + } else if (currentGrailsProp instanceof HibernateBasicProperty basic && basic.isEnumType()) { //HibernateEnumTypeProperty value = enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, path); - } else { + } else { //HibernateSimpleProperty - value = new BasicValue(metadataBuildingContext, table); - this.simpleValueBinder.bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path); + value = new BasicValue(metadataBuildingContext, table); + this.simpleValueBinder.bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path); } return value; } 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 b776560f8f..b81297a81c 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 @@ -63,7 +63,7 @@ public class CompositeIdBinder { "] is not a valid property!"); } - var value = componentBinder.bindComponentProperty(id, identifierProp, property, root, "", root.getTable(), mappings); + var value = componentBinder.bindComponentProperty(identifierProp, property, root, "", root.getTable(), mappings); componentUpdater.updateComponent(id, identifierProp, property, value); } } 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 797c389002..d2e402d4d6 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 @@ -3,13 +3,15 @@ package org.grails.orm.hibernate.cfg.domainbinding.binder; import jakarta.annotation.Nonnull; import org.grails.datastore.mapping.model.types.Association; import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty; 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.HibernateOneToManyProperty; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToOneProperty; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty; import org.grails.orm.hibernate.cfg.Mapping; import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy; import org.grails.orm.hibernate.cfg.domainbinding.util.PropertyFromValueCreator; -import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionType; -import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionHolder; import org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher; import org.grails.orm.hibernate.cfg.domainbinding.util.DefaultColumnNameFetcher; import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover; @@ -36,7 +38,6 @@ public class GrailsPropertyBinder { private static final Logger LOG = LoggerFactory.getLogger(GrailsPropertyBinder.class); private final MetadataBuildingContext metadataBuildingContext; - private final CollectionHolder collectionHolder; private final EnumTypeBinder enumTypeBinder; private final ComponentBinder componentBinder; private final CollectionBinder collectionBinder; @@ -49,7 +50,6 @@ public class GrailsPropertyBinder { public GrailsPropertyBinder( MetadataBuildingContext metadataBuildingContext, PersistentEntityNamingStrategy namingStrategy, - CollectionHolder collectionHolder, EnumTypeBinder enumTypeBinder, ComponentBinder componentBinder, CollectionBinder collectionBinder, @@ -59,7 +59,6 @@ public class GrailsPropertyBinder { ManyToOneBinder manyToOneBinder, PropertyFromValueCreator propertyFromValueCreator) { this.metadataBuildingContext = metadataBuildingContext; - this.collectionHolder = collectionHolder; this.enumTypeBinder = enumTypeBinder; this.componentBinder = componentBinder; this.collectionBinder = collectionBinder; @@ -82,42 +81,38 @@ public class GrailsPropertyBinder { Value value = null; - // see if it's a collection type - CollectionType collectionType = collectionHolder.get(currentGrailsProp.getType()); + // 1. Create Value and apply binders (consolidated block) + if (currentGrailsProp instanceof HibernateOneToOneProperty oneToOne) { + //HibernateOneToOneProperty + if (oneToOne.isHibernateOneToOne()) { + if (LOG.isDebugEnabled()) + LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne"); + value = oneToOneBinder.bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp, persistentClass, table, EMPTY_PATH); + } else { + if (LOG.isDebugEnabled()) + LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne"); - // 1. Create Value and apply binders (consolidated block) - if (currentGrailsProp.isUserButNotCollectionType()) { - value = new BasicValue(metadataBuildingContext, table); - simpleValueBinder.bindSimpleValue(currentGrailsProp, null,(SimpleValue) value, EMPTY_PATH); - } - else if (collectionType != null) { - if (currentGrailsProp.isSerializableType()) { - value = new BasicValue(metadataBuildingContext, table); - simpleValueBinder.bindSimpleValue(currentGrailsProp, null,(SimpleValue) value, EMPTY_PATH); - } - else { // Actual Collection - Collection collection = collectionType.create((HibernateToManyProperty) currentGrailsProp, persistentClass); - collectionBinder.bindCollection((HibernateToManyProperty) currentGrailsProp, collection, persistentClass, mappings, EMPTY_PATH); - mappings.addCollectionBinding(collection); - value = collection; + value = manyToOneBinder.bindManyToOne((Association) currentGrailsProp, table, EMPTY_PATH); } - } - else if (currentGrailsProp.getType().isEnum()) { - value = enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, EMPTY_PATH); - } - else if (currentGrailsProp.isHibernateOneToOne()) { - value = oneToOneBinder.bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne)currentGrailsProp, persistentClass, table, EMPTY_PATH); - } else if(currentGrailsProp.isHibernateManyToOne()) { - value = manyToOneBinder.bindManyToOne((Association)currentGrailsProp, table, EMPTY_PATH); - } - else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) { + } else if (currentGrailsProp instanceof HibernateManyToOneProperty manyToOne) { + //HibernateManyToOneProperty + if (LOG.isDebugEnabled()) + LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne"); + + value = manyToOneBinder.bindManyToOne((Association) currentGrailsProp, table, EMPTY_PATH); + } else if (currentGrailsProp instanceof HibernateToManyProperty toMany && !currentGrailsProp.isSerializableType()) { + //HibernateToManyProperty + value = collectionBinder.bindCollection(toMany, persistentClass, mappings, EMPTY_PATH); + } else if (currentGrailsProp instanceof HibernateEmbeddedProperty embedded) { value = componentBinder.bindComponent(persistentClass, embedded, mappings); - } - // work out what type of relationship it is and bind value - else { // Default BasicValue + } else if (currentGrailsProp instanceof HibernateBasicProperty basic && basic.isEnumType()) { + //HibernateEnumTypeProperty + value = enumTypeBinder.bindEnumType(currentGrailsProp, currentGrailsProp.getType(), table, EMPTY_PATH); + } else { + //HibernateSimpleProperty value = new BasicValue(metadataBuildingContext, table); - simpleValueBinder.bindSimpleValue(currentGrailsProp, null,(SimpleValue) value, EMPTY_PATH); + simpleValueBinder.bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH); } return value; 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 f89e4dacdc..b705a574ce 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 @@ -365,6 +365,7 @@ class ComponentBinderSpec extends HibernateGormDatastoreSpec { def mapping = new Mapping() ownerEntity.getMappedForm() >> mapping currentGrailsProp.getType() >> MyEnum + currentGrailsProp.isEnumType() >> true currentGrailsProp.isHibernateOneToOne() >> false currentGrailsProp.isHibernateManyToOne() >> false setupProperty(currentGrailsProp, "type", mapping, ownerEntity) 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 15b0032436..6a3f3aaf53 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 @@ -81,7 +81,7 @@ class CompositeIdBinderSpec extends HibernateGormDatastoreSpec { binder.bindCompositeId(domainClass, root, null, mappings) then: - 1 * componentBinder.bindComponentProperty(_ as Component, identifierProp, prop1, root, "", table, mappings) >> Mock(Value) + 1 * componentBinder.bindComponentProperty(identifierProp, prop1, root, "", table, mappings) >> Mock(Value) 1 * componentUpdater.updateComponent(_ as Component, identifierProp, prop1, _ as Value) } 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 ff8665247e..77409b4922 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 @@ -2,15 +2,23 @@ package org.grails.orm.hibernate.cfg.domainbinding import grails.gorm.annotation.Entity import grails.gorm.specs.HibernateGormDatastoreSpec +import org.grails.datastore.mapping.model.PersistentProperty import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty import org.grails.orm.hibernate.cfg.GrailsDomainBinder +import org.grails.orm.hibernate.cfg.Mapping +import org.grails.orm.hibernate.cfg.PropertyConfig import org.hibernate.mapping.ManyToOne import org.hibernate.mapping.Property import org.hibernate.mapping.RootClass import org.hibernate.mapping.SimpleValue import org.hibernate.mapping.Value +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty +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.HibernateSimpleProperty import org.grails.orm.hibernate.cfg.domainbinding.binder.CollectionBinder import org.grails.orm.hibernate.cfg.domainbinding.binder.ClassBinder import org.grails.orm.hibernate.cfg.domainbinding.binder.ComponentBinder @@ -47,9 +55,55 @@ import org.grails.orm.hibernate.cfg.domainbinding.util.MultiTenantFilterBinder import org.grails.orm.hibernate.cfg.domainbinding.binder.JoinedSubClassBinder import org.grails.orm.hibernate.cfg.domainbinding.binder.UnionSubclassBinder import org.grails.orm.hibernate.cfg.domainbinding.binder.SingleTableSubclassBinder +import org.hibernate.mapping.Table +import org.grails.datastore.mapping.model.PersistentEntity +import org.grails.datastore.mapping.model.MappingContext + +import static org.grails.orm.hibernate.cfg.GrailsDomainBinder.EMPTY_PATH class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec { + abstract static class TestManyToOne extends HibernateManyToOneProperty { + TestManyToOne(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) { + super(owner, context, descriptor); + } + } + + abstract static class TestOneToOne extends HibernateOneToOneProperty { + TestOneToOne(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) { + super(owner, context, descriptor); + } + } + + abstract static class TestEmbedded extends HibernateEmbeddedProperty { + TestEmbedded(PersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) { + super(owner, context, descriptor); + } + } + + abstract static class TestBasic extends HibernateBasicProperty { + TestBasic(GrailsHibernatePersistentEntity owner, MappingContext context, java.beans.PropertyDescriptor descriptor) { + super(owner, context, descriptor); + } + } + + abstract static class TestSimple extends HibernateSimpleProperty { + TestSimple(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 + if (prop instanceof GrailsHibernatePersistentProperty) { + _ * ((GrailsHibernatePersistentProperty)prop).getHibernateOwner() >> owner + } + def config = new PropertyConfig() + mapping.getColumns().put(name, config) + prop.getMappedForm() >> config + } + protected Map getBinders(GrailsDomainBinder binder) { MetadataBuildingContext metadataBuildingContext = binder.getMetadataBuildingContext() PersistentEntityNamingStrategy namingStrategy = binder.getNamingStrategy() @@ -194,8 +248,14 @@ class GrailsPropertyBinderSpec extends HibernateGormDatastoreSpec { rootClass.setEntityName(persistentEntity.name) rootClass.setTable(collector.addTable(null, null, "ENUM_BOOK", null, false, binder.getMetadataBuildingContext())) + def statusProp = Mock(TestBasic) + setupProperty(statusProp, "status", new Mapping(), persistentEntity) + statusProp.getType() >> java.util.concurrent.TimeUnit + statusProp.isEnumType() >> true + statusProp.isHibernateOneToOne() >> false + statusProp.isHibernateManyToOne() >> false + when: - def statusProp = persistentEntity.getPropertyByName("status") as GrailsHibernatePersistentProperty Value value = propertyBinder.bindProperty(rootClass, statusProp, collector) rootClass.addProperty(new PropertyFromValueCreator().createProperty(value, statusProp))
