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 386ec93b857cb773ea39fff9290f34b0d8b30875 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Feb 13 14:11:34 2026 -0600 refactored PropertyBinder.bindProperty, updated its callers, and ensured all tests in grails-data-hibernate7-core pass. The refactoring includes: * Updating PropertyBinder.bindProperty(GrailsHibernatePersistentProperty, Value) to return a Property. * Updating PropertyFromValueCreator.createProperty, SimpleIdBinder.bindSimpleId, and VersionBinder.bindVersion to use the new return value. --- .../cfg/domainbinding/binder/PropertyBinder.java | 21 ++++++++++-- .../cfg/domainbinding/binder/SimpleIdBinder.java | 6 +--- .../cfg/domainbinding/binder/VersionBinder.java | 4 +-- .../util/PropertyFromValueCreator.java | 5 +-- .../cfg/domainbinding/PropertyBinderSpec.groovy | 32 +++++++++++++++--- .../PropertyFromValueCreatorSpec.groovy | 12 +++++-- .../cfg/domainbinding/SimpleIdBinderSpec.groovy | 5 +-- .../cfg/domainbinding/VersionBinderSpec.groovy | 38 +++++++++++++++++----- 8 files changed, 91 insertions(+), 32 deletions(-) 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 d2e0662993..12984457a9 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 @@ -11,6 +11,8 @@ import org.grails.orm.hibernate.cfg.domainbinding.util.CascadeBehaviorFetcher; import org.hibernate.boot.spi.AccessType; import org.hibernate.mapping.Property; +import org.hibernate.mapping.SimpleValue; +import org.hibernate.mapping.Value; import java.util.Optional; @@ -31,9 +33,12 @@ public class PropertyBinder { * Binds a property to Hibernate runtime meta model. Deals with cascade strategy based on the Grails domain model * * @param persistentProperty The grails property instance - * @param prop The Hibernate property + * @param value The Hibernate value + * @return The Hibernate property */ - public void bindProperty(GrailsHibernatePersistentProperty persistentProperty, Property prop) { + public Property bindProperty(GrailsHibernatePersistentProperty persistentProperty, Value value) { + var prop = new Property(); + prop.setValue(value); // set the property name prop.setName(persistentProperty.getName()); PropertyConfig config = persistentProperty.getMappedForm(); @@ -74,5 +79,17 @@ public class PropertyBinder { .orElse( persistentProperty instanceof Association); prop.setLazy(isLazy); } + return prop; + } + + public void bindProperty(GrailsHibernatePersistentProperty persistentProperty, Property prop) { + Property bound = bindProperty(persistentProperty, prop.getValue()); + prop.setName(bound.getName()); + prop.setInsertable(bound.isInsertable()); + prop.setUpdateable(bound.isUpdateable()); + prop.setPropertyAccessorName(bound.getPropertyAccessorName()); + prop.setOptional(bound.isOptional()); + prop.setCascade(bound.getCascade()); + prop.setLazy(bound.isLazy()); } } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SimpleIdBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SimpleIdBinder.java index 93dc0e9b0f..7ee4dee38f 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SimpleIdBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/SimpleIdBinder.java @@ -80,12 +80,8 @@ public class SimpleIdBinder { // set type simpleValueBinder.bindSimpleValue(identifier, null, id, EMPTY_PATH); - // create property - Property prop = new Property(); - prop.setValue(id); - // bind property - propertyBinder.bindProperty(identifier, prop); + Property prop = propertyBinder.bindProperty(identifier, id); // set identifier property entity.setIdentifierProperty(prop); diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/VersionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/VersionBinder.java index e3fcb4c4d4..cf8858b21f 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/VersionBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/VersionBinder.java @@ -50,9 +50,7 @@ public class VersionBinder { if (!val.isTypeSpecified()) { val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp"); } - Property prop = new Property(); - prop.setValue(val); - propertyBinder.bindProperty(version, prop); + Property prop = propertyBinder.bindProperty(version, val); prop.setLazy(false); val.setNullValue("undefined"); entity.setVersion(prop); 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 e5239ca0b9..7a6b51ba50 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 @@ -26,9 +26,6 @@ public class PropertyFromValueCreator { value.createForeignKey(); } - Property prop = new Property(); - prop.setValue(value); - propertyBinder.bindProperty(grailsProperty, prop); - return prop; + return propertyBinder.bindProperty(grailsProperty, value); } } \ No newline at end of file diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyBinderSpec.groovy index 52392f47dd..436d76c466 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyBinderSpec.groovy @@ -30,9 +30,7 @@ class PropertyBinderSpec extends HibernateGormDatastoreSpec { def binder = new PropertyBinder(cascadeBehaviorFetcher) def persistentProperty = Mock(GrailsHibernatePersistentProperty) - def property = new Property() def value = Mock(Value) - property.setValue(value) def config = Mock(PropertyConfig) when: @@ -49,7 +47,7 @@ class PropertyBinderSpec extends HibernateGormDatastoreSpec { persistentProperty.isLazyAble() >> lazyAble and: - binder.bindProperty(persistentProperty, property) + def property = binder.bindProperty(persistentProperty, value) then: property.getName() == propertyName @@ -74,14 +72,14 @@ class PropertyBinderSpec extends HibernateGormDatastoreSpec { def binder = new PropertyBinder(cascadeBehaviorFetcher) def association = Mock(TestAssociation) - def property = new Property() + def value = Mock(Value) def config = Mock(PropertyConfig) when: association.getMappedForm() >> config config.getAccessType() >> AccessType.PROPERTY cascadeBehaviorFetcher.getCascadeBehaviour(association as Association) >> "all-delete-orphan" - binder.bindProperty(association as GrailsHibernatePersistentProperty, property) + def property = binder.bindProperty(association as GrailsHibernatePersistentProperty, value) then: property.getCascade() == "all-delete-orphan" @@ -94,7 +92,29 @@ class PropertyBinderSpec extends HibernateGormDatastoreSpec { def persistentProperty = Mock(GrailsHibernatePersistentProperty) persistentProperty.getName() >> "name" + def value = Mock(Value) + def config = new PropertyConfig() + config.setAccessType(jakarta.persistence.AccessType.PROPERTY) + persistentProperty.getMappedForm() >> config + + when: + def property = binder.bindProperty(persistentProperty, value) + + then: + property.getPropertyAccessorName() == "property" + } + + void "test bindProperty with Property object"() { + given: + def cascadeBehaviorFetcher = Mock(CascadeBehaviorFetcher) + def binder = new PropertyBinder(cascadeBehaviorFetcher) + + def persistentProperty = Mock(GrailsHibernatePersistentProperty) + persistentProperty.getName() >> "name" + persistentProperty.isNullable() >> true def property = new Property() + def value = Mock(Value) + property.setValue(value) def config = new PropertyConfig() config.setAccessType(jakarta.persistence.AccessType.PROPERTY) persistentProperty.getMappedForm() >> config @@ -103,6 +123,8 @@ class PropertyBinderSpec extends HibernateGormDatastoreSpec { binder.bindProperty(persistentProperty, property) then: + property.getName() == "name" + property.isOptional() == true property.getPropertyAccessorName() == "property" } diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyFromValueCreatorSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyFromValueCreatorSpec.groovy index ee50375d70..7f22c0e359 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyFromValueCreatorSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/PropertyFromValueCreatorSpec.groovy @@ -23,6 +23,11 @@ class PropertyFromValueCreatorSpec extends Specification { grailsProperty.getOwnerClassName() >> "com.example.MyEntity" grailsProperty.getName() >> "myProp" value.getTable() >> table + propertyBinder.bindProperty(grailsProperty, value) >> { + def p = new Property() + p.setValue(value) + return p + } when: Property prop = creator.createProperty(value, grailsProperty) @@ -30,7 +35,6 @@ class PropertyFromValueCreatorSpec extends Specification { then: 1 * value.setTypeUsingReflection("com.example.MyEntity", "myProp") 1 * value.createForeignKey() - 1 * propertyBinder.bindProperty(grailsProperty, _ as Property) prop.getValue() == value } @@ -45,6 +49,11 @@ class PropertyFromValueCreatorSpec extends Specification { grailsProperty.getOwnerClassName() >> "com.example.MyEntity" grailsProperty.getName() >> "myProp" value.getTable() >> null + propertyBinder.bindProperty(grailsProperty, value) >> { + def p = new Property() + p.setValue(value) + return p + } when: Property prop = creator.createProperty(value, grailsProperty) @@ -52,7 +61,6 @@ class PropertyFromValueCreatorSpec extends Specification { then: 1 * value.setTypeUsingReflection("com.example.MyEntity", "myProp") 0 * value.createForeignKey() - 1 * propertyBinder.bindProperty(grailsProperty, _ as Property) prop.getValue() == value } } \ No newline at end of file diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleIdBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleIdBinderSpec.groovy index 1aa7db235b..9880baaa67 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleIdBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/SimpleIdBinderSpec.groovy @@ -8,6 +8,7 @@ import org.hibernate.boot.spi.MetadataBuildingContext import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment import org.hibernate.mapping.BasicValue import org.hibernate.mapping.PrimaryKey +import org.hibernate.mapping.Property import org.hibernate.mapping.RootClass import org.hibernate.mapping.Table @@ -40,7 +41,7 @@ class SimpleIdBinderSpec extends HibernateGormDatastoreSpec { // Mock the collaborators that can be safely mocked simpleValueBinder = Mock(SimpleValueBinder) - propertyBinder = Mock(PropertyBinder) + propertyBinder = Spy(PropertyBinder) simpleIdBinder = new SimpleIdBinder(basicValueIdCreator, simpleValueBinder, propertyBinder) } @@ -120,4 +121,4 @@ class SimpleIdBinderSpec extends HibernateGormDatastoreSpec { then: thrown(org.hibernate.MappingException) } -} \ No newline at end of file +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/VersionBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/VersionBinderSpec.groovy index 755ff4a888..88084fb6a0 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/VersionBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/VersionBinderSpec.groovy @@ -1,11 +1,16 @@ package org.grails.orm.hibernate.cfg.domainbinding import grails.gorm.specs.HibernateGormDatastoreSpec +import org.grails.datastore.mapping.model.PersistentEntity import org.grails.datastore.mapping.model.PersistentProperty +import org.grails.datastore.mapping.model.PropertyMapping +import org.grails.datastore.mapping.reflect.EntityReflector import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty +import org.grails.orm.hibernate.cfg.PropertyConfig import org.hibernate.boot.spi.MetadataBuildingContext import org.hibernate.engine.OptimisticLockStyle import org.hibernate.mapping.BasicValue +import org.hibernate.mapping.Property import org.hibernate.mapping.RootClass import org.hibernate.mapping.Table import java.util.function.BiFunction @@ -25,7 +30,7 @@ class VersionBinderSpec extends HibernateGormDatastoreSpec { def setup() { metadataBuildingContext = getGrailsDomainBinder().getMetadataBuildingContext() simpleValueBinder = Mock(SimpleValueBinder) - propertyBinder = Mock(PropertyBinder) + propertyBinder = Spy(PropertyBinder) basicValueFactory = Mock(BiFunction) versionBinder = new VersionBinder(metadataBuildingContext, simpleValueBinder, propertyBinder, basicValueFactory) @@ -37,9 +42,7 @@ class VersionBinderSpec extends HibernateGormDatastoreSpec { def table = new Table("TEST_TABLE") rootClass.setTable(table) - def versionProperty = Mock(PersistentProperty, additionalInterfaces: [GrailsHibernatePersistentProperty]) { - getName() >> "version" - } + def versionProperty = new StubGrailsHibernatePersistentProperty("version") def basicValue = new BasicValue(metadataBuildingContext, table) @@ -49,7 +52,7 @@ class VersionBinderSpec extends HibernateGormDatastoreSpec { then: 1 * basicValueFactory.apply(metadataBuildingContext, table) >> basicValue 1 * simpleValueBinder.bindSimpleValue(versionProperty, null, basicValue, "", _) - 1 * propertyBinder.bindProperty(versionProperty, _) + 1 * propertyBinder.bindProperty(versionProperty, basicValue) rootClass.getVersion() != null rootClass.getDeclaredVersion() != null @@ -76,9 +79,7 @@ class VersionBinderSpec extends HibernateGormDatastoreSpec { def table = new Table("TEST_TABLE") rootClass.setTable(table) - def versionProperty = Mock(PersistentProperty, additionalInterfaces: [GrailsHibernatePersistentProperty]) { - getName() >> "lastUpdated" - } + def versionProperty = new StubGrailsHibernatePersistentProperty("lastUpdated") def basicValue = new BasicValue(metadataBuildingContext, table) @@ -87,6 +88,25 @@ class VersionBinderSpec extends HibernateGormDatastoreSpec { then: 1 * basicValueFactory.apply(metadataBuildingContext, table) >> basicValue + 1 * propertyBinder.bindProperty(versionProperty, basicValue) basicValue.getTypeName() == "timestamp" } -} \ No newline at end of file + + static class StubGrailsHibernatePersistentProperty implements GrailsHibernatePersistentProperty { + String name + StubGrailsHibernatePersistentProperty(String name) { this.name = name } + @Override String getName() { name } + @Override String getCapitilizedName() { name.capitalize() } + @Override Class getType() { Object } + @Override boolean isNullable() { false } + @Override PersistentEntity getOwner() { null } + @Override PropertyMapping getMapping() { null } + @Override PropertyConfig getMappedForm() { new PropertyConfig() } + @Override boolean isInherited() { false } + @Override EntityReflector.PropertyReader getReader() { null } + @Override EntityReflector.PropertyWriter getWriter() { null } + @Override String getOwnerClassName() { "Test" } + @Override boolean isLazyAble() { false } + @Override boolean isBidirectionalManyToOneWithListMapping(Property prop) { false } + } +}
