{"record":{"id":"e5b955ba779d2710","repo":"hibernate/hibernate-orm","slug":"given-entity-is-not-associated-with-the-persistenc-e5b955","errorCode":null,"errorMessage":"Given entity is not associated with the persistence context","messagePattern":"Given entity is not associated with the persistence context","errorType":"exception","errorClass":"DetachedObjectException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java","lineNumber":51,"sourceCode":"\t\tfinal Object instance = event.getObject();\n\t\t//noinspection ConstantValue\n\t\tif ( instance == null ) {\n\t\t\tthrow new NullPointerException( \"Attempted to lock null\" );\n\t\t}\n\n\t\tfinal var lockMode = event.getLockMode();\n\t\tif ( lockMode == LockMode.WRITE || lockMode == LockMode.UPGRADE_SKIPLOCKED ) {\n\t\t\tthrow new IllegalArgumentException( \"Invalid lock mode '\" + lockMode + \"' passed to 'lock()'\" );\n\t\t}\n\n\t\tfinal var source = event.getSession();\n\t\tfinal Object entity = forceInitialize( instance, source );\n\t\t//TODO: if instance was an uninitialized proxy, this is inefficient,\n\t\t//      resulting in two SQL selects\n\n\t\tfinal var entry = source.getPersistenceContextInternal().getEntry( entity );\n\t\tif ( entry == null && instance == entity ) {\n\t\t\tthrow new DetachedObjectException( \"Given entity is not associated with the persistence context\" );\n\t\t}\n\t\tassert entry != null;\n\n\t\tupgradeLock( entity, entry, event.getLockOptions(), source );\n\t}\n}\n","sourceCodeStart":33,"sourceCodeEnd":58,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java#L33-L58","documentation":"Error \"Given entity is not associated with the persistence context\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when an operation is invoked for an entity instance that the current persistence context does not manage.","commonSituations":"Typical situations: passing a transient or detached object to a persistence-context operation; using an entity loaded by a different (already closed) Session; evicting the entity before the call.","solutions":["Reattach the entity to the session (merge() or load it by id) before calling lock().","Only call lock() on entities that are managed or detached-but-known to this persistence context."],"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"}