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 2026/10/04 02:06:00 UTC

(grails-core) 02/02: refactor: bind with Hibernate's building context and an explicit entity list

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase2
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 1c3dd1a89958b106e4ce6073fe91eb10bd17219d
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sat Oct 3 20:57:10 2026 -0500

    refactor: bind with Hibernate's building context and an explicit entity list
    
    GrailsDomainBinder ignored the MetadataBuildingContext Hibernate hands to an
    AdditionalMappingContributor and built its own MetadataBuildingContextRootImpl
    from internal classes. Use the supplied context instead.
    
    The binder also pulled its entities from the mapping context itself, twice, and
    HibernateMappingContext.getHibernatePersistentEntities(dataSourceName) set the
    data source on every entity as a side effect of a getter. The getter is now a
    pure filter; HibernateMappingContextConfiguration computes the entity list once,
    after the additional classes have been added, sets the data source on each, and
    passes the list to the binder. The unused three-argument constructor is removed
    and the test-only re-run takes an explicit entity list.
    
    Entity tagging and the physical schema are unchanged (MappingSnapshotSpec).
    AdditionalClassBindingSpec covers entities registered through annotatedClasses,
    which only exist once the session factory is being built.
    
    Co-Authored-By: Claude Sonnet 5.5 <no...@anthropic.com>
---
 .../hibernate/cfg/HibernateMappingContext.groovy   |  3 +-
 .../HibernateMappingContextConfiguration.groovy    | 20 ++++--
 .../domainbinding/binder/GrailsDomainBinder.groovy | 52 ++++++--------
 .../gorm/tests/HibernateGormDatastoreSpec.groovy   |  6 +-
 .../cfg/AdditionalClassBindingSpec.groovy          | 84 ++++++++++++++++++++++
 .../cfg/GrailsHibernatePersistentEntitySpec.groovy |  7 +-
 .../cfg/HibernateMappingContextSpec.groovy         | 16 +++--
 .../cfg/domainbinding/SimpleIdBinderSpec.groovy    |  3 +-
 .../UnidirectionalOneToManyBinderSpec.groovy       |  8 +--
 9 files changed, 144 insertions(+), 55 deletions(-)

diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContext.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContext.groovy
index b2a1969849..0a6982291c 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContext.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContext.groovy
@@ -121,11 +121,10 @@ class HibernateMappingContext extends AbstractMappingContext {
         return super.getPersistentEntity(entityName)
     }
 
-    List<HibernatePersistentEntity> getHibernatePersistentEntities(String dataSourceName) {
+    List<HibernatePersistentEntity> getHibernatePersistentEntities() {
         List<HibernatePersistentEntity> result = new ArrayList<>()
         for (PersistentEntity entity : persistentEntities) {
             if (entity instanceof HibernatePersistentEntity) {
-                entity.setDataSourceName(dataSourceName)
                 result.add(entity)
             }
         }
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.groovy
index 3e84a42b77..43411990f6 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.groovy
@@ -66,6 +66,7 @@ import org.grails.orm.hibernate.GrailsSessionContext
 import org.grails.orm.hibernate.HibernateEventListeners
 import org.grails.orm.hibernate.MetadataIntegrator
 import org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentEntity
 import org.grails.orm.hibernate.cfg.domainbinding.util.NamingStrategyProvider
 import org.grails.orm.hibernate.proxy.GrailsBytecodeProvider
 
@@ -281,13 +282,6 @@ class HibernateMappingContextConfiguration extends Configuration
 
         ConfigurationHelper.resolvePlaceHolders(properties)
 
-        final GrailsDomainBinder domainBinder = new GrailsDomainBinder(
-                dataSourceName,
-                sessionFactoryBeanName,
-                hibernateMappingContext,
-                namingStrategyProvider,
-                hibernateMappingContext.mappingCacheHolder)
-
         List<Class> annotatedClasses = new ArrayList<>()
         for (PersistentEntity persistentEntity : hibernateMappingContext.persistentEntities) {
             Class<?> javaClass = persistentEntity.javaClass
@@ -304,6 +298,18 @@ class HibernateMappingContextConfiguration extends Configuration
             }
         }
 
+        List<HibernatePersistentEntity> persistentEntities = hibernateMappingContext.hibernatePersistentEntities
+        for (HibernatePersistentEntity persistentEntity : persistentEntities) {
+            persistentEntity.setDataSourceName(dataSourceName)
+        }
+
+        final GrailsDomainBinder domainBinder = new GrailsDomainBinder(
+                dataSourceName,
+                sessionFactoryBeanName,
+                persistentEntities,
+                namingStrategyProvider,
+                hibernateMappingContext.mappingCacheHolder)
+
         addAnnotatedClasses(annotatedClasses.toArray(new Class[0]))
 
         ClassLoaderService classLoaderService = new ClassLoaderServiceImpl(appClassLoader) {
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.groovy
index 441a5fca83..f64e29f084 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/GrailsDomainBinder.groovy
@@ -22,7 +22,6 @@ import java.util.function.BiFunction
 
 import groovy.transform.CompileStatic
 import org.hibernate.boot.ResourceStreamLocator
-import org.hibernate.boot.internal.MetadataBuildingContextRootImpl
 import org.hibernate.boot.model.TypeContributions
 import org.hibernate.boot.model.TypeContributor
 import org.hibernate.boot.spi.AdditionalMappingContributions
@@ -36,8 +35,6 @@ import org.hibernate.service.ServiceRegistry
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 
-import org.grails.datastore.mapping.core.connections.ConnectionSource
-import org.grails.orm.hibernate.cfg.HibernateMappingContext
 import org.grails.orm.hibernate.cfg.MappingCacheHolder
 import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy
 import org.grails.orm.hibernate.cfg.domainbinding.collectionType.CollectionHolder
@@ -76,37 +73,29 @@ class GrailsDomainBinder implements AdditionalMappingContributor, TypeContributo
 
     private final String sessionFactoryName
     private final String dataSourceName
-    private final HibernateMappingContext hibernateMappingContext
+    private final List<HibernatePersistentEntity> persistentEntities
     private final NamingStrategyProvider namingStrategyProvider
     private final MappingCacheHolder mappingCacheHolder
     private PersistentEntityNamingStrategy namingStrategy
     private MetadataBuildingContext metadataBuildingContext
 
-    GrailsDomainBinder(
-            String dataSourceName, String sessionFactoryName, HibernateMappingContext hibernateMappingContext) {
-        this(
-                dataSourceName,
-                sessionFactoryName,
-                hibernateMappingContext,
-                new NamingStrategyProvider(),
-                new MappingCacheHolder())
-    }
-
+    /**
+     * @param persistentEntities the entities to bind; each must already carry the name of {@code dataSourceName}
+     */
     GrailsDomainBinder(
             String dataSourceName,
             String sessionFactoryName,
-            HibernateMappingContext hibernateMappingContext,
+            List<HibernatePersistentEntity> persistentEntities,
             NamingStrategyProvider namingStrategyProvider,
             MappingCacheHolder mappingCacheHolder) {
         this.sessionFactoryName = sessionFactoryName
         this.dataSourceName = dataSourceName
-        this.hibernateMappingContext = hibernateMappingContext
+        this.persistentEntities = persistentEntities
         this.namingStrategyProvider = namingStrategyProvider
         this.mappingCacheHolder = mappingCacheHolder
 
         // pre-build mappings
-        for (HibernatePersistentEntity persistentEntity :
-                hibernateMappingContext.getHibernatePersistentEntities(dataSourceName)) {
+        for (HibernatePersistentEntity persistentEntity : persistentEntities) {
             mappingCacheHolder.cacheMapping(persistentEntity)
         }
     }
@@ -116,18 +105,20 @@ class GrailsDomainBinder implements AdditionalMappingContributor, TypeContributo
     }
 
     @Override
-    @SuppressWarnings('PMD.DataflowAnomalyAnalysis')
     void contribute(
             AdditionalMappingContributions contributions,
             InFlightMetadataCollector metadataCollector,
             ResourceStreamLocator resourceStreamLocator,
             MetadataBuildingContext buildingContext) {
-        this.metadataBuildingContext = new MetadataBuildingContextRootImpl(
-                ConnectionSource.DEFAULT,
-                metadataCollector.bootstrapContext,
-                metadataCollector.metadataBuildingOptions,
-                metadataCollector,
-                null)
+        bind(metadataCollector, buildingContext, persistentEntities)
+    }
+
+    @SuppressWarnings('PMD.DataflowAnomalyAnalysis')
+    private void bind(
+            InFlightMetadataCollector metadataCollector,
+            MetadataBuildingContext buildingContext,
+            List<HibernatePersistentEntity> entities) {
+        this.metadataBuildingContext = buildingContext
         CollectionHolder collectionHolder = new CollectionHolder(metadataBuildingContext)
         BackticksRemover backticksRemover = new BackticksRemover()
         PersistentEntityNamingStrategy namingStrategy = getNamingStrategy()
@@ -245,8 +236,7 @@ class GrailsDomainBinder implements AdditionalMappingContributor, TypeContributo
                 metadataCollector,
                 mappingCacheHolder)
 
-        for (HibernatePersistentEntity persistentEntity :
-                hibernateMappingContext.getHibernatePersistentEntities(dataSourceName)) {
+        for (HibernatePersistentEntity persistentEntity : entities) {
             if (persistentEntity.forGrailsDomainMapping(dataSourceName)) {
                 rootBinder.bindRoot(persistentEntity)
             }
@@ -293,11 +283,11 @@ class GrailsDomainBinder implements AdditionalMappingContributor, TypeContributo
     }
 
     /**
-     * Manually triggers the contribution process. Useful for unit testing
-     * where the full Hibernate bootstrap is not invoked.
+     * Manually re-runs the contribution process for the given entities with the building context of the last
+     * bootstrap. Useful for unit testing where entities are registered after the session factory was built.
      */
-    void contribute(InFlightMetadataCollector metadataCollector) {
-        contribute(null, metadataCollector, null, metadataBuildingContext)
+    void contribute(InFlightMetadataCollector metadataCollector, List<HibernatePersistentEntity> entities) {
+        bind(metadataCollector, metadataBuildingContext, entities)
     }
 
 }
diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/HibernateGormDatastoreSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/HibernateGormDatastoreSpec.groovy
index c99bb40fe8..1fe085e38b 100644
--- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/HibernateGormDatastoreSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/HibernateGormDatastoreSpec.groovy
@@ -22,11 +22,13 @@ package grails.gorm.tests
 
 import org.apache.grails.data.hibernate7.core.GrailsDataHibernate7TckManager
 import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec
+import org.grails.datastore.mapping.core.connections.ConnectionSource
 import org.grails.datastore.mapping.model.PersistentEntity
 import org.grails.orm.hibernate.HibernateSession
 import org.grails.orm.hibernate.HibernateDatastore
 import org.grails.orm.hibernate.cfg.domainbinding.binder.GrailsDomainBinder
 import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernatePersistentEntity
 import org.grails.orm.hibernate.cfg.HibernateMappingContext
 import org.grails.orm.hibernate.query.HibernateQuery
 
@@ -180,7 +182,9 @@ class HibernateGormDatastoreSpec extends GrailsDataTckSpec<GrailsDataHibernate7T
     protected void hibernateFirstPass() {
         def gdb = getGrailsDomainBinder()
         def collector = gdb.getMetadataBuildingContext().getMetadataCollector()
-        gdb.contribute(collector)
+        List<HibernatePersistentEntity> entities = getMappingContext().getHibernatePersistentEntities()
+        entities.each { it.setDataSourceName(ConnectionSource.DEFAULT) }
+        gdb.contribute(collector, entities)
     }
 
     /**
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/AdditionalClassBindingSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/AdditionalClassBindingSpec.groovy
new file mode 100644
index 0000000000..ba947bd0ab
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/AdditionalClassBindingSpec.groovy
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.orm.hibernate.cfg
+
+import grails.gorm.annotation.Entity
+import org.grails.datastore.mapping.core.DatastoreUtils
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.grails.orm.hibernate.HibernateDatastore
+import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity
+import spock.lang.AutoCleanup
+import spock.lang.Shared
+import spock.lang.Specification
+
+/**
+ * A GORM entity can reach the datastore two ways: as a constructor argument, or through the
+ * {@code hibernate.annotatedClasses} setting, which only registers it while the session factory is being built.
+ * Both must end up bound to the datastore's data source and mapped to a table.
+ */
+class AdditionalClassBindingSpec extends Specification {
+
+    @Shared
+    @AutoCleanup
+    HibernateDatastore datastore = new HibernateDatastore(
+            DatastoreUtils.createPropertyResolver([
+                    'hibernate.annotatedClasses': [AdditionalBoundThing],
+                    'dataSource.url'            : 'jdbc:h2:mem:additionalClassBinding;LOCK_TIMEOUT=10000',
+                    'dataSource.dbCreate'       : 'create-drop',
+                    'hibernate.hbm2ddl.auto'    : 'create-drop',
+            ]),
+            PrimaryBoundThing)
+
+    void "an entity registered through annotatedClasses is bound to the datastore's data source"() {
+        when:
+        GrailsHibernatePersistentEntity entity =
+                (GrailsHibernatePersistentEntity) datastore.mappingContext.getPersistentEntity(AdditionalBoundThing.name)
+
+        then:
+        entity != null
+        entity.dataSourceName == ConnectionSource.DEFAULT
+    }
+
+    void "an entity registered through annotatedClasses is mapped to a table"() {
+        expect:
+        AdditionalBoundThing.withNewSession { AdditionalBoundThing.count() } == 0
+    }
+
+    void "an entity passed to the constructor is bound to the datastore's data source"() {
+        when:
+        GrailsHibernatePersistentEntity entity =
+                (GrailsHibernatePersistentEntity) datastore.mappingContext.getPersistentEntity(PrimaryBoundThing.name)
+
+        then:
+        entity.dataSourceName == ConnectionSource.DEFAULT
+        PrimaryBoundThing.withNewSession { PrimaryBoundThing.count() } == 0
+    }
+}
+
+@Entity
+class PrimaryBoundThing {
+
+    String name
+}
+
+@Entity
+class AdditionalBoundThing {
+
+    String name
+}
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentEntitySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentEntitySpec.groovy
index 58bb530640..d4c24774be 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentEntitySpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentEntitySpec.groovy
@@ -214,12 +214,13 @@ class GrailsHibernatePersistentEntitySpec extends HibernateGormDatastoreSpec {
         prop.mappedForm.derived == true
     }
 
-    void "test dataSourceName injection"() {
+    void "test the datastore binds every entity to its own data source"() {
         when:
-        def entities = getMappingContext().getHibernatePersistentEntities("customDS")
+        def entities = getMappingContext().getHibernatePersistentEntities()
 
         then:
-        entities.every { it.dataSourceName == "customDS" }
+        !entities.isEmpty()
+        entities.every { it.dataSourceName == ConnectionSource.DEFAULT }
     }
 
     void "test getHibernatePersistentProperties calls validateProperty"() {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextSpec.groovy
index d39f2c6746..4559e9a7f0 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextSpec.groovy
@@ -24,7 +24,6 @@ import grails.gorm.annotation.Entity
 import grails.gorm.hibernate.HibernateEntity
 import grails.gorm.tests.HibernateGormDatastoreSpec
 import grails.gorm.transactions.Rollback
-import org.grails.datastore.mapping.core.connections.ConnectionSource
 import org.grails.datastore.mapping.engine.types.AbstractMappingAwareCustomTypeMarshaller
 import org.grails.datastore.mapping.model.PersistentEntity
 import org.grails.datastore.mapping.model.PersistentProperty
@@ -152,19 +151,24 @@ class HibernateMappingContextSpec extends HibernateGormDatastoreSpec {
 
     void "getHibernatePersistentEntities returns GrailsHibernatePersistentEntity instances"() {
         when:
-        def entities = mappingContext.getHibernatePersistentEntities(ConnectionSource.DEFAULT)
+        def entities = mappingContext.getHibernatePersistentEntities()
 
         then:
+        !entities.isEmpty()
         entities.every { it instanceof GrailsHibernatePersistentEntity }
-        entities.every { it.dataSourceName == ConnectionSource.DEFAULT }
     }
 
-    void "getHibernatePersistentEntities sets the dataSourceName on each entity"() {
+    void "getHibernatePersistentEntities does not change the dataSourceName of any entity"() {
+        given:
+        Map<String, String> before = mappingContext.getHibernatePersistentEntities().collectEntries {
+            [(it.name): it.dataSourceName]
+        }
+
         when:
-        def entities = mappingContext.getHibernatePersistentEntities("myDs")
+        mappingContext.getHibernatePersistentEntities()
 
         then:
-        entities.every { it.dataSourceName == "myDs" }
+        mappingContext.getHibernatePersistentEntities().collectEntries { [(it.name): it.dataSourceName] } == before
     }
 
     void "embedded entity is created correctly"() {
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 82f5cee9de..210200ee92 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
@@ -167,11 +167,12 @@ class SimpleIdBinderSpec extends HibernateGormDatastoreSpec {
         currentTable = new Table("TEST_TABLE")
         currentTable.setName("TEST_TABLE")
         rootClass.setTable(currentTable)
+        def datastoreMappingContext = getMappingContext()
         def domainClass = Mock(HibernatePersistentEntity) {
             getMappedForm() >> mapping
             getIdentity() >> null
             getName() >> "TestEntity"
-            getMappingContext() >> getGrailsDomainBinder().hibernateMappingContext
+            getMappingContext() >> datastoreMappingContext
             getMapping() >> Mock(org.grails.datastore.mapping.model.ClassMapping)
             getReflector() >> reflector
             getIdentityProperty() >> Mock(HibernateSimpleIdentityProperty)
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
index 3b4e09dff5..241808717e 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/UnidirectionalOneToManyBinderSpec.groovy
@@ -84,8 +84,8 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
     def "test bindUnidirectionalOneToMany with join table"() {
         given:
         def grailsDomainBinder = getGrailsDomainBinder()
-        def ownerEntity = grailsDomainBinder.hibernateMappingContext.getPersistentEntity(UniOwner.name) as GrailsHibernatePersistentEntity
-        def petEntity = grailsDomainBinder.hibernateMappingContext.getPersistentEntity(UniPet.name) as GrailsHibernatePersistentEntity
+        def ownerEntity = mappingContext.getPersistentEntity(UniOwner.name) as GrailsHibernatePersistentEntity
+        def petEntity = mappingContext.getPersistentEntity(UniPet.name) as GrailsHibernatePersistentEntity
 
         def ownerToPetsProperty = ownerEntity.getPropertyByName("pets") as HibernateOneToManyProperty
 
@@ -114,8 +114,8 @@ class UnidirectionalOneToManyBinderSpec extends HibernateGormDatastoreSpec {
     def "test bindUnidirectionalOneToMany with backref"() {
         given:
         def grailsDomainBinder = getGrailsDomainBinder()
-        def ownerEntity = grailsDomainBinder.hibernateMappingContext.getPersistentEntity(UniOwner.name) as GrailsHibernatePersistentEntity
-        def petEntity = grailsDomainBinder.hibernateMappingContext.getPersistentEntity(UniPet.name) as GrailsHibernatePersistentEntity
+        def ownerEntity = mappingContext.getPersistentEntity(UniOwner.name) as GrailsHibernatePersistentEntity
+        def petEntity = mappingContext.getPersistentEntity(UniPet.name) as GrailsHibernatePersistentEntity
 
         def mappings = grailsDomainBinder.metadataBuildingContext.metadataCollector
         def ownerPersistentClass = mappings.getEntityBinding(UniOwner.name)