{"record":{"id":"61c1fad000a0bfb6","repo":"hibernate/hibernate-orm","slug":"persistence-unit-s-enhancement-requires-a-temp","errorCode":null,"errorMessage":"[persistence unit: %s] Enhancement requires a temp class loader, but none was given","messagePattern":"\\[persistence unit: (.+?)\\] Enhancement requires a temp class loader, but none was given","errorType":"exception","errorClass":"PersistenceException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java","lineNumber":319,"sourceCode":"\t\t\t\tENHANCER_ENABLE_ASSOCIATION_MANAGEMENT,\n\t\t\t\tintegrationSettings,\n\t\t\t\tpersistenceUnit,\n\t\t\t\tfalse\n\t\t);\n\n\t\tif ( !lazyInitializationEnabled ) {\n\t\t\t//noinspection removal\n\t\t\tDEPRECATION_LOGGER.deprecatedSettingForRemoval( ENHANCER_ENABLE_LAZY_INITIALIZATION, \"true\" );\n\t\t}\n\t\tif ( !dirtyTrackingEnabled ) {\n\t\t\t//noinspection removal\n\t\t\tDEPRECATION_LOGGER.deprecatedSettingForRemoval( ENHANCER_ENABLE_DIRTY_TRACKING, \"true\" );\n\t\t}\n\n\t\tif ( dirtyTrackingEnabled || lazyInitializationEnabled || associationManagementEnabled ) {\n\t\t\tfinal var classLoader = persistenceUnit.getNewTempClassLoader();\n\t\t\tif ( classLoader == null ) {\n\t\t\t\tthrow new PersistenceException( String.format( Locale.ROOT,\n\t\t\t\t\t\t\"[persistence unit: %s] Enhancement requires a temp class loader, but none was given\",\n\t\t\t\t\t\tpersistenceUnit.getPersistenceUnitName()\n\t\t\t\t) );\n\t\t\t}\n\n\t\t\tfinal var enhancementContext = createEnhancementContext(\n\t\t\t\t\tdirtyTrackingEnabled,\n\t\t\t\t\tlazyInitializationEnabled,\n\t\t\t\t\tassociationManagementEnabled,\n\t\t\t\t\tintegrationSettings,\n\t\t\t\t\tpersistenceUnit\n\t\t\t);\n\n\t\t\tfinal EnhancingClassTransformerImpl classTransformer =\n\t\t\t\t\tnew EnhancingClassTransformerImpl( enhancementContext );\n\n\t\t\t// NOTE : the ClassTransformer method is called discoverType, but in reality it\n\t\t\t// pre-enhances the classes...","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceProvider.java#L301-L337","documentation":"When runtime enhancement settings (hibernate.enhancer.enableDirtyTracking, enableLazyInitialization, enableAssociationManagement) are enabled, HibernatePersistenceProvider needs a temporary class loader to run the enhancer and asks PersistenceUnitInfo.getNewTempClassLoader() for it. Outside a full EE container that method commonly returns null, and bootstrap fails immediately with this PersistenceException naming the persistence unit.","triggerScenarios":"Java SE bootstrap or a plain Spring/Quarkus-like container whose PersistenceUnitInfo implementation returns null from getNewTempClassLoader(), while any of the three hibernate.enhancer.enable* settings is true (defaults are enabled, so merely setting one to true or leaving defaults active in such a container is enough).","commonSituations":"Migrating an EE app to Spring Boot without also moving enhancement to build time; custom PersistenceUnitInfo implementations that only implement getClassLoader; enabling association management at runtime for convenience in tests.","solutions":["Enhance at build time with the Hibernate Gradle/Maven enhancement plugin and set the three hibernate.enhancer.enable* settings to false","Or return a real throwaway ClassLoader from getNewTempClassLoader() in your PersistenceUnitInfo (EE containers do this)","If you do not need enhancement features, explicitly set all three settings to false to opt out"],"exampleFix":"// before\n// enhancer settings left enabled, container returns null temp classloader -> PersistenceException\n\n// after\nprops.put(\"hibernate.enhancer.enableDirtyTracking\", \"false\");\nprops.put(\"hibernate.enhancer.enableLazyInitialization\", \"false\");\nprops.put(\"hibernate.enhancer.enableAssociationManagement\", \"false\");\n// run the Hibernate enhancement plugin at build time instead","handlingStrategy":"validation","validationCode":"// Opt out of runtime enhancement; enhance at build time instead\nprops.put(\"hibernate.enhancer.enableDirtyTracking\", \"false\");\nprops.put(\"hibernate.enhancer.enableLazyInitialization\", \"false\");\nprops.put(\"hibernate.enhancer.enableAssociationManagement\", \"false\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer build-time enhancement via the Hibernate Gradle/Maven plugin","If you enable runtime enhancement, ensure PersistenceUnitInfo.getNewTempClassLoader() returns a real classloader","Add an integration test that bootstraps the EMF in the target container"],"tags":["jpa","bytecode-enhancement","bootstrap","classloader"],"backgroundTag":"bytecode-enhancement-misconfigured","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}