This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase3 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit ad60a4f0b3cffeb140e7d8a487fa08e0b10f6af7 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Oct 8 22:11:34 2026 -0500 feat(hibernate7): make native domain binding the default and deprecate the classic binder hibernate.generatedDomainClasses now defaults to true, in the settings (HibernateConnectionSourceSettings), in the configuration a session factory is built from (HibernateMappingContextConfiguration, for the callers that construct it themselves) and in the published configuration metadata, which is what an IDE shows as the default and was missing the key. Native binding accepts every mapping the classic binder can use and is checked against the frozen classic oracle, so the classic binder is now the fallback: false still selects it, and the configuration logs one WARN per data source at startup saying it is deprecated and will be removed, naming the setting. The test-only JVM property that seeded the default is inverted (grails.hibernate.classicDomainBinding) so a suite can still run through the classic binder as a whole. The specs cover the defaults, the configured value, the inverted JVM property, the metadata default and the warning (captured from the stderr the test logger writes to), booting with an existing fixture so the entity set of the differential specs is unchanged. Co-Authored-By: Claude Fable 5.1 <[email protected]> --- .../HibernateMappingContextConfiguration.groovy | 26 +++++- .../HibernateConnectionSourceSettings.groovy | 11 +-- .../additional-spring-configuration-metadata.json | 6 ++ .../cfg/ClassicDomainBindingDeprecationSpec.groovy | 97 ++++++++++++++++++++++ ...HibernateMappingContextConfigurationSpec.groovy | 8 +- .../HibernateConnectionSourceSettingsSpec.groovy | 62 ++++++++++++-- 6 files changed, 188 insertions(+), 22 deletions(-) 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 63c4be38e5..592223712f 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 @@ -43,6 +43,8 @@ import org.hibernate.cfg.JdbcSettings import org.hibernate.context.spi.CurrentSessionContext import org.hibernate.internal.util.config.ConfigurationHelper import org.hibernate.service.ServiceRegistry +import org.slf4j.Logger +import org.slf4j.LoggerFactory import org.springframework.beans.BeansException import org.springframework.context.ApplicationContext import org.springframework.context.ApplicationContextAware @@ -87,6 +89,17 @@ class HibernateMappingContextConfiguration extends Configuration private static final long serialVersionUID = -7115087342689305517L + private static final Logger LOG = LoggerFactory.getLogger(HibernateMappingContextConfiguration) + + /** + * The warning logged once per session factory when the classic domain binding is selected. + */ + static final String CLASSIC_DOMAIN_BINDING_DEPRECATED = + 'Classic domain binding is deprecated and will be removed: hibernate.generatedDomainClasses is false ' + + 'for data source [%s]. Native domain binding, the default, binds the domain classes with Hibernate\'s own ' + + 'annotation binder; remove the setting, or set it to true, after reviewing the Native Domain Binding chapter ' + + 'of the GORM for Hibernate 7 manual.' + private static final String RESOURCE_PATTERN = '/**/*.class' private static final TypeFilter[] ENTITY_TYPE_FILTERS = [ @@ -110,16 +123,18 @@ class HibernateMappingContextConfiguration extends Configuration private transient ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver() private transient NamingStrategyProvider namingStrategyProvider = new NamingStrategyProvider() protected GrailsBytecodeProvider bytecodeProvider - private boolean generatedDomainClasses + private boolean generatedDomainClasses = true /** - * Whether Hibernate's annotation binder binds classes generated from the GORM mapping instead of the domain binder - * building Hibernate's boot model by hand. The real domain classes are still what is persisted and loaded. + * Whether Hibernate's annotation binder binds classes generated from the GORM mapping (native domain binding, the + * default) instead of the classic domain binder building Hibernate's boot model by hand. The real domain classes are + * still what is persisted and loaded. * * <p>Set it before the annotated classes are added: with the switch on, {@link #addAnnotatedClass} keeps the GORM entities * that are not JPA entities away from Hibernate's own sources (see there).</p> * - * @param generatedDomainClasses {@code true} to bind generated classes; {@code false} (the default) for the domain binder + * @param generatedDomainClasses {@code true} (the default) to bind generated classes; {@code false} for the classic + * domain binder, which is deprecated and will be removed */ void setGeneratedDomainClasses(boolean generatedDomainClasses) { this.generatedDomainClasses = generatedDomainClasses @@ -338,6 +353,9 @@ class HibernateMappingContextConfiguration extends Configuration persistentEntity.setDataSourceName(dataSourceName) } + if (!generatedDomainClasses) { + LOG.warn(String.format(CLASSIC_DOMAIN_BINDING_DEPRECATED, dataSourceName)) + } final GeneratedDomainClassBinder generatedBinder = generatedDomainClasses ? new GeneratedDomainClassBinder(dataSourceName, appClassLoader) : null final GrailsDomainBinder domainBinder = new GrailsDomainBinder( diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettings.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettings.groovy index bc88ce91ae..4423755392 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettings.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettings.groovy @@ -90,13 +90,14 @@ class HibernateConnectionSourceSettings extends ConnectionSourceSettings { */ boolean hibernateDirtyChecking = false - // the JVM-wide system property switches the default on to run an existing test suite through the generated path; a configured value wins + // the JVM-wide system property switches the default off to run an existing test suite through the classic domain binder; a configured value wins /** - * Whether Hibernate's annotation binder binds classes generated from the GORM mapping, instead of the domain - * binder building Hibernate's boot model by hand. The application's real domain classes are still what is - * persisted and loaded. Off by default, and intended to become the default. + * Whether Hibernate's annotation binder binds classes generated from the GORM mapping (native domain binding), + * instead of the classic domain binder building Hibernate's boot model by hand. The application's real domain + * classes are still what is persisted and loaded. On by default. {@code false} selects the classic domain + * binding, which is deprecated and will be removed. */ - boolean generatedDomainClasses = Boolean.getBoolean('grails.hibernate.generatedDomainClasses') + boolean generatedDomainClasses = !Boolean.getBoolean('grails.hibernate.classicDomainBinding') /** * Cache settings diff --git a/grails-data-hibernate7/core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grails-data-hibernate7/core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 3f7709ecd5..f8c04ecacf 100644 --- a/grails-data-hibernate7/core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grails-data-hibernate7/core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -28,6 +28,12 @@ "description": "Whether to enable Hibernate's query cache.", "defaultValue": false }, + { + "name": "hibernate.generatedDomainClasses", + "type": "java.lang.Boolean", + "description": "Whether Hibernate's own annotation binder binds the domain classes (native domain binding). When false, GORM's classic domain binder builds the Hibernate mapping itself; classic domain binding is deprecated and will be removed. See Native Domain Binding in the GORM for Hibernate 7 manual.", + "defaultValue": true + }, { "name": "hibernate.grails.proxy.lazy_to_string", "type": "java.lang.Boolean", diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy new file mode 100644 index 0000000000..8aef741e79 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/ClassicDomainBindingDeprecationSpec.groovy @@ -0,0 +1,97 @@ +/* + * 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.tests.DomainOne +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Specification + +import java.nio.charset.StandardCharsets + +/** + * Native domain binding is the default. An application that still selects the classic domain binder with + * {@code hibernate.generatedDomainClasses: false} boots, and is told once per data source, at startup, that the classic + * binding is deprecated and will be removed. The test JVM logs through slf4j-simple, which writes to {@code System.err}, + * so the warning is read from there. The entity is an existing fixture, so the scanned entity set of the differential + * specs is unchanged. + */ +class ClassicDomainBindingDeprecationSpec extends Specification { + + private static final String WARNING = 'Classic domain binding is deprecated and will be removed: ' + + 'hibernate.generatedDomainClasses is false for data source [default]' + + @AutoCleanup + HibernateDatastore datastore + + private String bootAndCaptureLog(Map settings) { + PrintStream original = System.err + ByteArrayOutputStream captured = new ByteArrayOutputStream() + System.setErr(new PrintStream(captured, true, StandardCharsets.UTF_8.name())) + try { + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'dataSource.url' : "jdbc:h2:mem:classicDeprecation${System.nanoTime()};LOCK_TIMEOUT=10000".toString(), + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create-drop', + 'hibernate.cache.queries' : 'false', + 'hibernate.cache.use_query_cache': 'false', + ] + settings), DomainOne) + } finally { + System.setErr(original) + } + return captured.toString(StandardCharsets.UTF_8.name()) + } + + void "selecting the classic domain binder logs that it is deprecated and will be removed"() { + when: + String log = bootAndCaptureLog(['hibernate.generatedDomainClasses': false]) + + then: 'the warning names the setting and the data source, and the application still works' + log.contains('WARN') + log.contains(WARNING) + log.contains('remove the setting, or set it to true') + DomainOne.withTransaction { + new DomainOne(controller: 'book', action: 'classic').save(flush: true, failOnError: true) + DomainOne.count() == 1 + } + } + + void "the default, native domain binding, starts without the warning"() { + when: + String log = bootAndCaptureLog([:]) + + then: + !log.contains(WARNING) + DomainOne.withTransaction { + new DomainOne(controller: 'book', action: 'native').save(flush: true, failOnError: true) + DomainOne.count() == 1 + } + } + + void "selecting native domain binding explicitly starts without the warning"() { + when: + String log = bootAndCaptureLog(['hibernate.generatedDomainClasses': true]) + + then: + !log.contains(WARNING) + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfigurationSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfigurationSpec.groovy index a1a7121530..7dc855dd61 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfigurationSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfigurationSpec.groovy @@ -49,18 +49,18 @@ class HibernateMappingContextConfigurationSpec extends Specification { config.dataSourceName == 'default' } - def "the generated domain classes switch is off by default and can be turned on"() { + def "native domain binding is on by default and the classic domain binder can be selected"() { given: def config = new HibernateMappingContextConfiguration() expect: - !config.generatedDomainClasses + config.generatedDomainClasses when: - config.setGeneratedDomainClasses(true) + config.setGeneratedDomainClasses(false) then: - config.generatedDomainClasses + !config.generatedDomainClasses } def "setBytecodeProvider stores the provider and getGrailsBytecodeProvider returns it"() { diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettingsSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettingsSpec.groovy index eabd4d8b9d..576e6f50cc 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettingsSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/HibernateConnectionSourceSettingsSpec.groovy @@ -18,6 +18,7 @@ */ package org.grails.orm.hibernate.connections +import groovy.json.JsonSlurper import org.grails.datastore.mapping.core.DatastoreUtils import org.hibernate.dialect.H2Dialect import org.springframework.core.io.UrlResource @@ -118,32 +119,56 @@ class HibernateConnectionSourceSettingsSpec extends Specification { listeners['create-onflush'].is(onFlushListener) } - void "the generated domain classes switch is off by default and can be turned on"() { + void "native domain binding is on by default and the classic domain binder can be selected"() { given: - String property = 'grails.hibernate.generatedDomainClasses' + String property = 'grails.hibernate.classicDomainBinding' String previous = System.clearProperty(property) when: def settings = new HibernateConnectionSourceSettings() + then: + settings.hibernate.generatedDomainClasses + + when: + settings.hibernate.generatedDomainClasses = false + then: !settings.hibernate.generatedDomainClasses + cleanup: + if (previous != null) { + System.setProperty(property, previous) + } + } + + void "a configured hibernate.generatedDomainClasses value is what the built settings hold"() { + given: + String property = 'grails.hibernate.classicDomainBinding' + String previous = System.clearProperty(property) + when: - settings.hibernate.generatedDomainClasses = true + HibernateConnectionSourceSettings settings = new HibernateConnectionSourceSettingsBuilder( + DatastoreUtils.createPropertyResolver(['hibernate.generatedDomainClasses': configured])).build() then: - settings.hibernate.generatedDomainClasses + settings.hibernate.generatedDomainClasses == expected cleanup: if (previous != null) { System.setProperty(property, previous) } + + where: + configured | expected + false | false + 'false' | false + true | true } - void "the JVM-wide system property switches the generated domain classes default on, and a configured value wins"() { + void "the JVM-wide system property switches the default to the classic domain binder, and a configured value wins"() { given: - String property = 'grails.hibernate.generatedDomainClasses' + String property = 'grails.hibernate.classicDomainBinding' String previous = System.getProperty(property) System.setProperty(property, 'true') @@ -151,13 +176,13 @@ class HibernateConnectionSourceSettingsSpec extends Specification { def settings = new HibernateConnectionSourceSettings() then: - settings.hibernate.generatedDomainClasses + !settings.hibernate.generatedDomainClasses when: - settings.hibernate.generatedDomainClasses = false + settings.hibernate.generatedDomainClasses = true then: - !settings.hibernate.generatedDomainClasses + settings.hibernate.generatedDomainClasses cleanup: if (previous == null) { @@ -167,6 +192,25 @@ class HibernateConnectionSourceSettingsSpec extends Specification { } } + void "the published configuration metadata states the default of hibernate.generatedDomainClasses"() { + given: 'the metadata an IDE shows for the key' + String previous = System.clearProperty('grails.hibernate.classicDomainBinding') + URL metadata = HibernateConnectionSourceSettings.getResource('/META-INF/additional-spring-configuration-metadata.json') + Map property = ((List<Map>) ((Map) new JsonSlurper().parse(metadata)).get('properties')).find { + it.get('name') == 'hibernate.generatedDomainClasses' + } + + expect: + property != null + property.get('defaultValue') == new HibernateConnectionSourceSettings().hibernate.generatedDomainClasses + (property.get('description') as String).contains('deprecated') + + cleanup: + if (previous != null) { + System.setProperty('grails.hibernate.classicDomainBinding', previous) + } + } + void "test toProperties with dirty checking and custom config"() { given: def settings = new HibernateConnectionSourceSettings()
