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 803c2ffa7482c3aed65aad4863b4b22379e80450 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Sep 12 20:54:23 2025 -0500 MultipleDataSource test fix --- .../orm/hibernate/HibernateGormStaticApi.groovy | 47 ++++++---------------- .../MultipleDataSourcesWithEventsSpec.groovy | 30 +++++++++----- 2 files changed, 32 insertions(+), 45 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 8ba94db17b..8aa1ad0435 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 @@ -270,24 +270,12 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> { @Override List<D> findAllWhere(Map queryMap, Map args) { if (!queryMap) return null - (List<D>)hibernateTemplate.execute { Session session -> - Map<String, Object> processedQueryMap = [:] - queryMap.each{ key, value -> processedQueryMap[key.toString()] = value } - Map<String,Object> queryArgs = filterQueryArgumentMap(processedQueryMap) - List<String> nullNames = removeNullNames(queryArgs) - - CriteriaBuilder cb = session.getCriteriaBuilder() - CriteriaQuery cq = cb.createQuery(persistentEntity.javaClass) - def root = cq.from(persistentEntity.javaClass) - def listOfPredicates = queryArgs.collect { entry -> cb.equal(root.get(entry.key), entry.value) } - def nullPredicates = nullNames.collect { nullName -> cb.isNotNull(root.get(nullName)) } - def jpaPredicates = (listOfPredicates + nullPredicates).<JpaPredicate>toArray(new JpaPredicate[0]) - cq.select(root).where(cb.and(jpaPredicates)) - firePreQueryEvent() - List results = session.createQuery(cq).resultList - firePostQueryEvent(results) - return results - } + def hibernateQuery = new HibernateQuery(hibernateSession, persistentEntity) + queryMap.each {key,value -> hibernateQuery.eq(key.toString(),value)} + firePreQueryEvent() + def result = hibernateQuery.list() + firePostQueryEvent(result) + result as List<D> } @Override @@ -454,23 +442,12 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> { @Override D findWhere(Map queryMap, Map args) { if (!queryMap) return null - (D)hibernateTemplate.execute { Session session -> - Map<String, Object> processedQueryMap = [:] - queryMap.each{ key, value -> processedQueryMap[key.toString()] = value } - Map<String,Object> queryArgs = filterQueryArgumentMap(processedQueryMap) - List<String> nullNames = removeNullNames(queryArgs) - CriteriaBuilder cb = session.getCriteriaBuilder() - CriteriaQuery cq = cb.createQuery(persistentEntity.javaClass) - def root = cq.from(persistentEntity.javaClass) - def listOfPredicates = queryArgs.collect { entry -> cb.equal(root.get(entry.key), entry.value) } - def nullPredicates = nullNames.collect { nullName -> cb.isNotNull(root.get(nullName)) } - JpaPredicate[] jpaPredicates = (listOfPredicates + nullPredicates).<JpaPredicate>toArray(new JpaPredicate[0]) - cq.select(root).where(cb.and(jpaPredicates)) - firePreQueryEvent() - Object result = session.createQuery(cq).singleResult - firePostQueryEvent(result) - result - } + def hibernateQuery = new HibernateQuery(hibernateSession, persistentEntity) + queryMap.each {key,value -> hibernateQuery.eq(key.toString(),value)} + firePreQueryEvent() + def result = hibernateQuery.singleResult() + firePostQueryEvent(result) + result as D } List<D> getAll(List ids) { diff --git a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/connections/MultipleDataSourcesWithEventsSpec.groovy b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/connections/MultipleDataSourcesWithEventsSpec.groovy index eccdc9e3bb..542156fec3 100644 --- a/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/connections/MultipleDataSourcesWithEventsSpec.groovy +++ b/grails-data-hibernate6/core/src/test/groovy/org/grails/orm/hibernate/connections/MultipleDataSourcesWithEventsSpec.groovy @@ -27,21 +27,31 @@ import spock.lang.Issue /** * Created by graemerocher on 20/02/2017.*/ -//TODO Multiple data sources not working class MultipleDataSourcesWithEventsSpec extends HibernateGormDatastoreSpec { def setupSpec() { manager.addAllDomainClasses([EventsBook, SecondaryBook]) 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.ehcache.EhCacheRegionFactory'], - 'hibernate.hbm2ddl.auto': 'create', - 'dataSources.books' : [url: "jdbc:h2:mem:books;LOCK_TIMEOUT=10000"] + 'dataSource': [ + 'url' : "jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000", + 'dbCreate' : 'update', + 'dialect' : H2Dialect.name, + 'formatSql' : 'true' + ], + 'dataSources': [ + 'books': [ + 'url' : "jdbc:h2:mem:books;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' + ] ] }
