diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/enhance/EnhancingClassTransformerImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/enhance/EnhancingClassTransformerImpl.java index 99b119d408..77383de470 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/internal/enhance/EnhancingClassTransformerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/enhance/EnhancingClassTransformerImpl.java @@ -31,6 +31,9 @@ public class EnhancingClassTransformerImpl implements ClassTransformer { private final ReentrantLock lock = new ReentrantLock(); private volatile WeakReference entryReference; + //This list is matching the constants used by CoreTypePool's default constructor + private static final String[] NO_TRANSFORM_PREFIXES = { "jakarta/", "java/", "org/hibernate/annotations/" }; + public EnhancingClassTransformerImpl(EnhancementContext enhancementContext) { Objects.requireNonNull( enhancementContext ); this.enhancementContext = enhancementContext; @@ -45,6 +48,16 @@ public class EnhancingClassTransformerImpl implements ClassTransformer { ProtectionDomain protectionDomain, byte[] classfileBuffer) throws TransformerException { + //Take care to not transform certain types; this is both an optimisation (we can skip this unnecessary work) + //and a safety precaution as we otherwise risk attempting to redefine classes which have already been loaded: + //see https://hibernate.atlassian.net/browse/HHH-18108 + //N.B. this className doesn't use the dot-format but the slashes for package separators. + for ( String prefix : NO_TRANSFORM_PREFIXES ) { + if ( className.startsWith( prefix ) ) { + return null; + } + } + try { return getEnhancer( loader ).enhance( className, classfileBuffer ); }