Sidestepped bug in Hibernate 5
authorKai Moritz <kai@juplo.de>
Thu, 17 Dec 2015 01:53:38 +0000 (02:53 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 20 Dec 2015 20:09:03 +0000 (21:09 +0100)
src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java

index b61d83c..d50a1e7 100644 (file)
@@ -438,10 +438,13 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       /** Load checksums for old mapping and configuration */
       tracker.load();
 
-      /** Create a BootstrapServiceRegistry with special ClassLoader */
+      /** Create the ClassLoader */
+      URLClassLoader classLoader = createClassLoader();
+
+      /** Create a BootstrapServiceRegistry with the created ClassLoader */
       BootstrapServiceRegistry bootstrapServiceRegitry =
           new BootstrapServiceRegistryBuilder()
-              .applyClassLoader(createClassLoader())
+              .applyClassLoader(classLoader)
               .build();
       ClassLoaderService classLoaderService =
           bootstrapServiceRegitry.getService(ClassLoaderService.class);
@@ -520,7 +523,22 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
             );
       }
 
-      build((MetadataImplementor)metadataBuilder.build());
+      /**
+       * Change class-loader of current thread.
+       * This is necessary, because still not all parts of Hibernate 5 use
+       * the newly introduced ClassLoaderService and will fail otherwise!
+       */
+      Thread thread = Thread.currentThread();
+      ClassLoader contextClassLoader = thread.getContextClassLoader();
+      try
+      {
+        thread.setContextClassLoader(classLoader);
+        build((MetadataImplementor)metadataBuilder.build());
+      }
+      finally
+      {
+        thread.setContextClassLoader(contextClassLoader);
+      }
     }
     finally
     {