{"record":{"id":"646644d7acdadeb1","repo":"hibernate/hibernate-orm","slug":"given-proxy-does-not-belong-to-this-persistence-co","errorCode":null,"errorMessage":"Given proxy does not belong to this persistence context","messagePattern":"Given proxy does not belong to this persistence context","errorType":"exception","errorClass":"DetachedObjectException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/internal/ProxyUtil.java","lineNumber":54,"sourceCode":"\t\t\treturn lazyInitializer.getImplementation();\n\t\t}\n\t\telse {\n\t\t\treturn maybeProxy;\n\t\t}\n\t}\n\n\t/**\n\t * Get the entity instance underlying the given proxy, forcing\n\t * initialization if the proxy is uninitialized. If the given\n\t * object is not a proxy, simply return the argument.\n\t * @throws DetachedObjectException if the given proxy does not\n\t *         belong to the given session\n\t */\n\tpublic static Object forceInitialize(Object maybeProxy, SharedSessionContractImplementor session) {\n\t\tfinal var lazyInitializer = extractLazyInitializer( maybeProxy );\n\t\tif ( lazyInitializer != null ) {\n\t\t\tif ( lazyInitializer.getSession() != session ) {\n\t\t\t\tthrow new DetachedObjectException( \"Given proxy does not belong to this persistence context\" );\n\t\t\t}\n\t\t\t//initialize + unwrap the object and return it\n\t\t\treturn lazyInitializer.getImplementation();\n\t\t}\n\t\telse if ( isPersistentAttributeInterceptable( maybeProxy ) ) {\n\t\t\tfinal var interceptor =\n\t\t\t\t\tasPersistentAttributeInterceptable( maybeProxy )\n\t\t\t\t\t\t\t.$$_hibernate_getInterceptor();\n\t\t\tif ( interceptor instanceof EnhancementAsProxyLazinessInterceptor lazinessInterceptor ) {\n\t\t\t\tif ( lazinessInterceptor.getLinkedSession() != session ) {\n\t\t\t\t\tthrow new DetachedObjectException( \"Given proxy does not belong to this persistence context\" );\n\t\t\t\t}\n\t\t\t\tlazinessInterceptor.forceInitialize( maybeProxy, null );\n\t\t\t}\n\t\t\treturn maybeProxy;\n\t\t}\n\t\telse {\n\t\t\treturn maybeProxy;","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/internal/ProxyUtil.java#L36-L72","documentation":"Error \"Given proxy does not belong to this persistence context\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown at hibernate-core/src/main/java/org/hibernate/engine/internal/ProxyUtil.java:54 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Only operate on proxies loaded by the current session/persistence context","Reload the entity in the current session instead of passing a proxy from another session"],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}