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 7a4eb3923ba7cb7a18bd2f5e9920c83415ac718d Author: Walter Duque de Estrada <[email protected]> AuthorDate: Tue Feb 3 12:49:32 2026 -0600 progress --- .../orm/hibernate/cfg/GrailsDomainBinder.java | 15 ++--- .../cfg/domainbinding/GrailsPropertyCreator.java | 32 ++++++++++ .../domainbinding/GrailsPropertyCreatorSpec.groovy | 36 +++++++++++ .../HibernateDatastoreSpringInitializerSpec.groovy | 30 ++++++++-- .../collectionType/CollectionType.java | 20 ------- .../collectionType/CollectionTypeSpec.groovy | 70 ---------------------- 6 files changed, 96 insertions(+), 107 deletions(-) diff --git a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java index 7c5ba1ecb6..2bb11e0d6f 100644 --- a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java +++ b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java @@ -14,6 +14,7 @@ */ package org.grails.orm.hibernate.cfg; +import org.grails.orm.hibernate.cfg.domainbinding.GrailsPropertyCreator; import groovy.lang.Closure; import jakarta.persistence.Entity; import org.grails.datastore.mapping.core.connections.ConnectionSource; @@ -153,6 +154,7 @@ public class GrailsDomainBinder implements MetadataContributor { private Closure defaultMapping; private PersistentEntityNamingStrategy namingStrategy; private MetadataBuildingContext metadataBuildingContext; + private GrailsPropertyCreator grailsPropertyCreator; public JdbcEnvironment getJdbcEnvironment() { @@ -214,6 +216,7 @@ public class GrailsDomainBinder implements MetadataContributor { metadataCollector.getMetadataBuildingOptions(), metadataCollector ); + this.grailsPropertyCreator = new GrailsPropertyCreator(metadataCollector, new PropertyBinder()); filterHibernateEntities(hibernateMappingContext.getHibernatePersistentEntities()) .forEach(hibernatePersistentEntity -> bindRoot(hibernatePersistentEntity, metadataCollector, sessionFactoryName)); @@ -1939,17 +1942,7 @@ public class GrailsDomainBinder implements MetadataContributor { * Creates a persistent class property based on the GrailDomainClassProperty instance. */ private Property createProperty(Value value, PersistentClass persistentClass, PersistentProperty grailsProperty, InFlightMetadataCollector mappings) { - // set type - value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName()); - - if (value.getTable() != null) { - value.createForeignKey(); - } - - Property prop = new Property(); - prop.setValue(value); - new PropertyBinder().bindProperty(grailsProperty, prop); - return prop; + return this.grailsPropertyCreator.createProperty(value, persistentClass, grailsProperty); } private void bindOneToMany(org.grails.datastore.mapping.model.types.OneToMany currentGrailsProp, OneToMany one, InFlightMetadataCollector mappings) { diff --git a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreator.java b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreator.java new file mode 100644 index 0000000000..147566fe68 --- /dev/null +++ b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreator.java @@ -0,0 +1,32 @@ +package org.grails.orm.hibernate.cfg.domainbinding; + +import org.grails.datastore.mapping.model.PersistentProperty; +import org.hibernate.boot.spi.InFlightMetadataCollector; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Property; +import org.hibernate.mapping.Value; + +public class GrailsPropertyCreator { + + private final InFlightMetadataCollector mappings; + private final PropertyBinder propertyBinder; + + public GrailsPropertyCreator(InFlightMetadataCollector mappings, PropertyBinder propertyBinder) { + this.mappings = mappings; + this.propertyBinder = propertyBinder; + } + + public Property createProperty(Value value, PersistentClass persistentClass, PersistentProperty grailsProperty) { + // set type + value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName()); + + if (value.getTable() != null) { + value.createForeignKey(); + } + + Property prop = new Property(); + prop.setValue(value); + propertyBinder.bindProperty(grailsProperty, prop); + return prop; + } +} diff --git a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreatorSpec.groovy b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreatorSpec.groovy new file mode 100644 index 0000000000..f93a71ae61 --- /dev/null +++ b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/GrailsPropertyCreatorSpec.groovy @@ -0,0 +1,36 @@ +package org.grails.orm.hibernate.cfg.domainbinding + +import grails.gorm.specs.HibernateGormDatastoreSpec +import org.grails.datastore.mapping.model.PersistentProperty +import org.hibernate.boot.spi.InFlightMetadataCollector +import org.hibernate.mapping.PersistentClass +import org.hibernate.mapping.Property +import org.hibernate.mapping.Value +import spock.lang.Specification + +class GrailsPropertyCreatorSpec extends HibernateGormDatastoreSpec { + + void "test createProperty method"() { + given: + def mockValue = Mock(Value) + def mockPersistentClass = Mock(PersistentClass) + def mockGrailsProperty = Mock(PersistentProperty) + def mockMappings = null // No need to mock InFlightMetadataCollector + def mockPropertyBinder = Mock(PropertyBinder) + + mockValue.setTypeUsingReflection(mockPersistentClass.getClassName(), mockGrailsProperty.getName()) >> { String className, String propertyName -> } + mockValue.getTable() >> null + + def grailsPropertyCreator = new GrailsPropertyCreator(mockMappings, mockPropertyBinder) + + when: + Property result = grailsPropertyCreator.createProperty(mockValue, mockPersistentClass, mockGrailsProperty) + + then: + 1 * mockValue.setTypeUsingReflection(mockPersistentClass.getClassName(), mockGrailsProperty.getName()) + 0 * mockValue.getTable() + 1 * mockPropertyBinder.bindProperty(mockGrailsProperty, result) + result != null + result.getValue() == mockValue + } +} diff --git a/grails-data-hibernate6/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy b/grails-data-hibernate6/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy index 88fb4c628d..66fde561f8 100644 --- a/grails-data-hibernate6/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy +++ b/grails-data-hibernate6/grails-plugin/src/test/groovy/grails/orm/bootstrap/HibernateDatastoreSpringInitializerSpec.groovy @@ -63,29 +63,47 @@ class HibernateDatastoreSpringInitializerSpec extends Specification{ and:"Each domain has the correct data source(s)" Person.withNewSession { Person.count() == 0 } Person.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:people" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:people" return true } Book.withNewSession { Book.count() == 0 } Book.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:books" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:books" return true } Book.moreBooks.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:moreBooks" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:moreBooks" return true } Author.withNewSession { Author.count() == 0 } Author.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:people" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:people" return true } Author.books.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:books" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:books" return true } Author.moreBooks.withNewSession { Session s -> - assert s.connection().metaData.getURL() == "jdbc:h2:mem:moreBooks" + def url = s.doReturningWork { connection -> + return connection.metaData.getURL() + } + assert url == "jdbc:h2:mem:moreBooks" return true } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionType.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionType.java index 9b972cc96a..5f2ef64905 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionType.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionType.java @@ -50,27 +50,7 @@ public abstract class CollectionType { return clazz.getName(); } - private void createInstances() { - if (initialized) { - return; - } - - initialized = true; - - CollectionType set = new SetCollectionType(binder); - INSTANCES.put(Set.class, set); - INSTANCES.put(SortedSet.class, set); - - INSTANCES.put(List.class, new ListCollectionType(binder)); - INSTANCES.put(java.util.Collection.class, new BagCollectionType(binder)); - INSTANCES.put(Map.class, new MapCollectionType(binder)); - } - - public CollectionType collectionTypeForClass(Class<?> clazz) { - createInstances(); - return INSTANCES.get(clazz); - } public String getTypeName(ToMany<?> property) { return property instanceof GrailsHibernatePersistentProperty ghpp ? ghpp.getTypeName() : null; diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionTypeSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionTypeSpec.groovy deleted file mode 100644 index 85dfcb97ec..0000000000 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/collectionType/CollectionTypeSpec.groovy +++ /dev/null @@ -1,70 +0,0 @@ -package org.grails.orm.hibernate.cfg.domainbinding.collectionType - -import grails.gorm.specs.HibernateGormDatastoreSpec -import org.grails.datastore.mapping.model.types.ToMany -import org.grails.orm.hibernate.cfg.GrailsDomainBinder -import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentEntity -import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty -import org.grails.orm.hibernate.cfg.HibernateOneToManyProperty -import org.hibernate.boot.spi.InFlightMetadataCollector -import org.hibernate.mapping.Collection -import org.hibernate.mapping.PersistentClass -import spock.lang.Subject - -class CollectionTypeSpec extends HibernateGormDatastoreSpec { - - // Concrete implementation for testing abstract base class - class TestCollectionType extends CollectionType { - TestCollectionType(GrailsDomainBinder binder) { - super(String, binder) - } - @Override - Collection create(ToMany property, PersistentClass owner, String path, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { - return null - } - } - - @Subject - def collectionType - - def setup() { - collectionType = new TestCollectionType(getGrailsDomainBinder()) - } - - def "toString should return class name"() { - expect: - collectionType.toString() == String.name - } - - def "should return correct collection type for class"() { - expect: - collectionType.collectionTypeForClass(Set) instanceof SetCollectionType - collectionType.collectionTypeForClass(List) instanceof ListCollectionType - collectionType.collectionTypeForClass(java.util.Collection) instanceof BagCollectionType - collectionType.collectionTypeForClass(Map) instanceof MapCollectionType - } - - def "getTypeName should return type name from GrailsHibernatePersistentProperty"() { - given: - // Use HibernateOneToManyProperty which implements both ToMany and GrailsHibernatePersistentProperty - def hibernateProp = Mock(HibernateOneToManyProperty) - - hibernateProp.getTypeName() >> "my.custom.Type" - - expect: - collectionType.getTypeName(hibernateProp) == "my.custom.Type" - } - - def "getTypeName should return null if not GrailsHibernatePersistentProperty"() { - given: - // Use a standard ToMany mock (which might fail if ToMany is considered final by Spock for some reason, - // but we'll try standard PersistentProperty if it fails) - def property = Mock(org.grails.datastore.mapping.model.types.OneToMany) - def domainClass = Mock(GrailsHibernatePersistentEntity) - property.getOwner() >> domainClass - domainClass.getMappedForm() >> null - - expect: - collectionType.getTypeName(property) == null - } -}
