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 c0894754390bf026ef16cd348c438c82a19a9faf
Author: Walter Duque de Estrada <[email protected]>
AuthorDate: Mon Sep 8 20:31:30 2025 -0500

    Multiple Datasources
---
 .../orm/hibernate/HibernateGormStaticApi.groovy    |  8 ++++-
 .../specs/UniqueWithMultipleDataSourcesSpec.groovy | 42 +++++++++++++++-------
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
index 213815f8d9..8ba94db17b 100644
--- 
a/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
+++ 
b/grails-data-hibernate6/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
@@ -25,6 +25,7 @@ import org.grails.datastore.gorm.GormEnhancer
 import org.grails.datastore.gorm.GormStaticApi
 import org.grails.datastore.gorm.finders.DynamicFinder
 import org.grails.datastore.gorm.finders.FinderMethod
+import org.grails.datastore.mapping.core.connections.ConnectionSourcesProvider
 import org.grails.datastore.mapping.query.api.BuildableCriteria as 
GrailsCriteria
 import org.grails.datastore.mapping.query.event.PostQueryEvent
 import org.grails.datastore.mapping.query.event.PreQueryEvent
@@ -650,7 +651,12 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> {
 
     @Override
     def propertyMissing(String name) {
-        return GormEnhancer.findStaticApi(persistentClass, name)
+        if(datastore instanceof ConnectionSourcesProvider) {
+            return GormEnhancer.findStaticApi(persistentClass, name)
+        }
+        else {
+            throw new MissingPropertyException(name, persistentClass)
+        }
     }
 
     @Override
diff --git 
a/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/UniqueWithMultipleDataSourcesSpec.groovy
 
b/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/UniqueWithMultipleDataSourcesSpec.groovy
index 749f77e734..dbf30db109 100644
--- 
a/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/UniqueWithMultipleDataSourcesSpec.groovy
+++ 
b/grails-data-hibernate6/core/src/test/groovy/grails/gorm/specs/UniqueWithMultipleDataSourcesSpec.groovy
@@ -33,32 +33,49 @@ class UniqueWithMultipleDataSourcesSpec extends 
HibernateGormDatastoreSpec {
     def setupSpec() {
         manager.addAllDomainClasses([Abc])
         manager.grailsConfig = [
-                'dataSource.url'        : 
"jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000",
-                'dataSource.dbCreate'   : 'update',
-                'dataSource.dialect'    : H2Dialect.name,
-                'dataSource.formatSql'  : 'true',
-                'hibernate.flush.mode'  : 'COMMIT',
-                'hibernate.cache.queries': 'true',
-                'hibernate.cache'       : ['use_second_level_cache': true, 
'region.factory_class': 
'org.hibernate.cache.jcache.internal.JCacheRegionFactory'],
-                'hibernate.hbm2ddl.auto': 'create',
-                'dataSources.second'    : [url: 
"jdbc:h2:mem:second;LOCK_TIMEOUT=10000"],
+                'dataSource': [
+                        'url'        : 
"jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000",
+                        'dbCreate'   : 'update',
+                        'dialect'    : H2Dialect.name,
+                        'formatSql'  : 'true'
+                ],
+                'dataSources': [
+                        'second': [
+                                'url'        : 
"jdbc:h2:mem:second;LOCK_TIMEOUT=10000",
+                                'dbCreate'   : 'update',
+                                'dialect'    : H2Dialect.name,
+                                'formatSql'  : 'true'
+                        ]
+                ],
+                'hibernate': [
+                        'flush.mode'  : 'COMMIT',
+                        'cache.queries': 'true',
+                        'cache'       : ['use_second_level_cache': true, 
'region.factory_class': 
'org.hibernate.cache.jcache.internal.JCacheRegionFactory'],
+                        'hbm2ddl.auto': 'create-drop'
+                ]
         ]
     }
+    
+    def setup() {
+        // The HibernateGormDatastoreSpec only initializes the default 
datasource by default.
+        // We need to explicitly initialize the 'second' datasource to ensure 
its schema is created.
+        manager.getHibernateDatastore().getDatastoreForConnection('second')
+    }
 
     @Rollback
     @Issue('https://github.com/grails/grails-core/issues/10481')
     void "test multiple data sources and unique constraint"() {
         when:
         Abc abc = new Abc(temp: "testing")
-        abc.save()
+        abc.save(flush: true)
 
         Abc abc1 = new Abc(temp: "testing")
         Abc.second.withNewSession {
-            abc1.second.save()
+            abc1.second.save(flush: true)
         }
 
         then:
-        !abc1.hasErrors()
+        abc1.hasErrors()
     }
 }
 
@@ -75,4 +92,3 @@ class Abc {
         datasource(ConnectionSource.ALL)
     }
 }
-

Reply via email to