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 2025/07/10 03:01:53 UTC
(grails-core) 15/22: checked in GrailsDomainBinderSpec
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch merge-hibernate6
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 6cb305081711dfb51ee8f02441fec94c0b8262fd
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sat Jul 5 00:39:38 2025 -0500
checked in GrailsDomainBinderSpec
---
.../orm/hibernate/cfg/GrailsDomainBinder.java | 2 +-
.../hibernate/cfg/GrailsDomainBinderSpec.groovy | 84 ++++++++++++++++++++++
2 files changed, 85 insertions(+), 1 deletion(-)
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 b3ea9b160e..df28d6aad7 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
@@ -1371,7 +1371,7 @@ public class GrailsDomainBinder implements MetadataContributor {
* @param mappings The Hibernate Mappings object
* @param sessionFactoryBeanName the session factory bean name
*/
- private void bindRoot(HibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
+ protected void bindRoot(HibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
if (mappings.getEntityBinding(entity.getName()) != null) {
LOG.info("[GrailsDomainBinder] Class [" + entity.getName() + "] is already mapped, skipping.. ");
return;
diff --git a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinderSpec.groovy b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinderSpec.groovy
new file mode 100644
index 0000000000..a719f62130
--- /dev/null
+++ b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinderSpec.groovy
@@ -0,0 +1,84 @@
+package org.grails.orm.hibernate.cfg
+
+import grails.gorm.specs.HibernateGormDatastoreSpec
+import org.hibernate.boot.MetadataSources
+import org.hibernate.boot.internal.BootstrapContextImpl
+import org.hibernate.boot.internal.InFlightMetadataCollectorImpl
+import org.hibernate.boot.internal.MetadataBuilderImpl
+import org.hibernate.boot.registry.BootstrapServiceRegistry
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder
+import org.hibernate.boot.registry.classloading.spi.ClassLoaderService
+import org.hibernate.internal.SessionFactoryImpl
+import org.hibernate.service.spi.ServiceRegistryImplementor
+import org.hibernate.dialect.H2Dialect
+import org.hibernate.boot.spi.MetadataContributor
+
+class GrailsDomainBinderSpec extends HibernateGormDatastoreSpec {
+
+ void "Test save()"() {
+ when:
+
+ def collector = getCollector()
+ def grailsDomainBinder = getGrailsDomainBinder()
+ def persistentEntity = createPersistentEntity("Book", [title: String, author: String, publishedYear: Integer])
+ grailsDomainBinder.bindRoot(persistentEntity, collector,"sessionFactoryName")
+ println("when")
+ then:
+ 1 == 1
+ }
+
+ HibernatePersistentEntity createPersistentEntity(String className, Map<String, Class> properties) {
+ def classLoader = new GroovyClassLoader()
+ def classText = """
+ import grails.gorm.annotation.Entity
+ import grails.gorm.hibernate.HibernateEntity
+ @Entity
+ class ${className} implements HibernateEntity<${className}> {
+ ${properties.collect { name, type -> "${type.simpleName} ${name}" }.join('\n ')}
+
+ static mapping = {
+ id generator: 'native'
+ version false
+ }
+ }
+ """
+
+ def clazz = classLoader.parseClass(classText)
+ getMappingContext().addPersistentEntity(clazz) as HibernatePersistentEntity
+ }
+
+ private InFlightMetadataCollectorImpl getCollector() {
+ def bootstrapServiceRegistry = getServiceRegistry()
+ .getParentServiceRegistry()
+ .getParentServiceRegistry() as BootstrapServiceRegistry
+ def serviceRegistry = new StandardServiceRegistryBuilder(bootstrapServiceRegistry)
+ .applySetting("hibernate.dialect", H2Dialect.class.getName())
+ .applySetting("jakarta.persistence.jdbc.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
+ .applySetting("jakarta.persistence.jdbc.driver", "org.h2.Driver")
+ .build()
+ def options = new MetadataBuilderImpl(
+ new MetadataSources(serviceRegistry)
+ ).getMetadataBuildingOptions()
+ new InFlightMetadataCollectorImpl(
+ new BootstrapContextImpl( serviceRegistry, options)
+ , options);
+ }
+
+ private HibernateMappingContext getMappingContext() {
+ manager.hibernateDatastore.getMappingContext()
+ }
+
+ private GrailsDomainBinder getGrailsDomainBinder() {
+ def registry = getServiceRegistry()
+ registry
+ .getParentServiceRegistry()
+ .getService(ClassLoaderService.class)
+ .loadJavaServices(MetadataContributor.class)
+ .find { it instanceof GrailsDomainBinder }
+ }
+
+ private ServiceRegistryImplementor getServiceRegistry() {
+ (manager.hibernateDatastore.sessionFactory as SessionFactoryImpl)
+ .getServiceRegistry()
+ }
+}
\ No newline at end of file