jamesfredley commented on code in PR #16299:
URL: https://github.com/apache/grails-core/pull/16299#discussion_r3930182273


##########
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/DevToolsClassLoaders.java:
##########
@@ -0,0 +1,70 @@
+/*
+ *  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.datastore.mapping.reflect;
+
+/**
+ * Resolves the class loader GORM and Hibernate should use when Spring Boot 
DevTools
+ * is on the classpath.
+ *
+ * <p>DevTools splits the classpath across a base loader (third-party jars) 
and a
+ * {@code RestartClassLoader} (application classes). Hibernate's JPA metamodel 
and
+ * GORM's entity registry key entities by {@link Class} identity, so domain 
classes
+ * loaded by the restart loader are "not an entity" if Hibernate resolved them
+ * through the base loader. Preferring the restart loader — typically the 
thread
+ * context class loader on {@code restartedMain} — keeps those identities 
aligned.</p>
+ *
+ * @since 8.0
+ */
+public final class DevToolsClassLoaders {
+
+    private static final String RESTART_CLASS_LOADER_SIMPLE_NAME = 
"RestartClassLoader";
+
+    private DevToolsClassLoaders() {
+    }
+
+    /**
+     * @param classLoader the loader to inspect, possibly {@code null}
+     * @return {@code true} when {@code classLoader} is Spring Boot DevTools'
+     * {@code RestartClassLoader}
+     */
+    public static boolean isRestartClassLoader(ClassLoader classLoader) {

Review Comment:
   Agreed. `isRestartClassLoader` now walks the class hierarchy matching 
`org.springframework.boot.devtools.restart.classloader.RestartClassLoader` 
first. The exact simple-name `RestartClassLoader` match is only a fallback for 
tests and shaded/relocated copies; case-insensitive matching is gone.



##########
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/DevToolsClassLoaders.java:
##########
@@ -0,0 +1,70 @@
+/*
+ *  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.datastore.mapping.reflect;
+
+/**
+ * Resolves the class loader GORM and Hibernate should use when Spring Boot 
DevTools
+ * is on the classpath.
+ *
+ * <p>DevTools splits the classpath across a base loader (third-party jars) 
and a
+ * {@code RestartClassLoader} (application classes). Hibernate's JPA metamodel 
and
+ * GORM's entity registry key entities by {@link Class} identity, so domain 
classes
+ * loaded by the restart loader are "not an entity" if Hibernate resolved them
+ * through the base loader. Preferring the restart loader — typically the 
thread
+ * context class loader on {@code restartedMain} — keeps those identities 
aligned.</p>
+ *
+ * @since 8.0
+ */
+public final class DevToolsClassLoaders {
+
+    private static final String RESTART_CLASS_LOADER_SIMPLE_NAME = 
"RestartClassLoader";
+
+    private DevToolsClassLoaders() {
+    }
+
+    /**
+     * @param classLoader the loader to inspect, possibly {@code null}
+     * @return {@code true} when {@code classLoader} is Spring Boot DevTools'
+     * {@code RestartClassLoader}
+     */
+    public static boolean isRestartClassLoader(ClassLoader classLoader) {
+        return classLoader != null &&
+                
RESTART_CLASS_LOADER_SIMPLE_NAME.equalsIgnoreCase(classLoader.getClass().getSimpleName());
+    }
+
+    /**
+     * Prefer the thread context class loader when it is DevTools'
+     * {@code RestartClassLoader}; otherwise return {@code fallback}, or this
+     * class's loader when {@code fallback} is {@code null}.
+     *
+     * @param fallback the loader to use when DevTools is not active
+     * @return a non-null class loader
+     */
+    @SuppressWarnings("PMD.UseProperClassLoader")
+    public static ClassLoader resolve(ClassLoader fallback) {

Review Comment:
   Done. `preferRestartClassLoader` keeps `fallback` when it is the restart 
loader or a descendant of it. The PMD suppression has a one-line reason, and 
`resolve()` is deprecated and delegates to `preferRestartClassLoader()`.



##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.java:
##########
@@ -153,10 +154,8 @@ public void setApplicationContext(@Nullable 
ApplicationContext applicationContex
             properties.put("hibernate.enhancer.enableLazyInitialization", 
FALSE_LITERAL);
             properties.put("hibernate.enhancer.enableDirtyTracking", 
FALSE_LITERAL);
             properties.put("hibernate.enhancer.enableAssociationManagement", 
FALSE_LITERAL);
-            ClassLoader classLoader = applicationContext.getClassLoader();
-            if (classLoader != null) {
-                properties.put(AvailableSettings.CLASSLOADERS, classLoader);
-            }
+            properties.put(AvailableSettings.CLASSLOADERS,
+                    
DevToolsClassLoaders.resolve(applicationContext.getClassLoader()));

Review Comment:
   Valid. `setApplicationContext` now writes `CLASSLOADERS` only when the 
application context loader is non-null or the TCCL is a `RestartClassLoader`. 
That preserves the old Hibernate 7 fallback to 
`HibernateMappingContextConfiguration.class.getClassLoader()` in 
`buildSessionFactory`. Covered by tests for both the null-loader and 
restart-TCCL cases.



##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/HibernateMappingContextConfiguration.java:
##########
@@ -292,13 +283,9 @@ public SessionFactory buildSessionFactory() throws 
HibernateException {
         SessionFactory sessionFactory;
 
         Object classLoaderObject = 
getProperties().get(AvailableSettings.CLASSLOADERS);
-        ClassLoader appClassLoader;
-
-        if (classLoaderObject instanceof ClassLoader) {
-            appClassLoader = (ClassLoader) classLoaderObject;
-        } else {
-            appClassLoader = getClass().getClassLoader();
-        }
+        ClassLoader storedClassLoader = classLoaderObject instanceof 
ClassLoader ?
+                (ClassLoader) classLoaderObject : getClass().getClassLoader();
+        ClassLoader appClassLoader = 
DevToolsClassLoaders.resolve(storedClassLoader);

Review Comment:
   Kept on purpose. `addProperties()` / a custom `configClass` can overwrite 
`CLASSLOADERS` after the setters, and Hibernate re-resolves entity `Class` 
objects via `ReflectHelper.classForName` (TCCL) during SessionFactory 
construction. `resolveSessionFactoryClassLoader()` is now tested, and 
`buildSessionFactory` sets TCCL around `super.buildSessionFactory` and restores 
it in `finally`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to