{"record":{"id":"d18a065e4cda5ec2","repo":"hibernate/hibernate-orm","slug":"entity-is-not-associated-with-the-session","errorCode":null,"errorMessage":"entity is not associated with the session: {}","messagePattern":"entity is not associated with the session: (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java","lineNumber":1569,"sourceCode":"\n\t\treturn conjunction;\n\t}\n\n\t@Override\n\tpublic Object initializeLazyProperty(String fieldName, Object entity, SharedSessionContractImplementor session) {\n\t\treturn hasCollections() && getPropertyTypes()[getPropertyIndex( fieldName )] instanceof CollectionType collectionType\n\t\t\t\t? initializedLazyCollection( fieldName, entity, collectionType, session )\n\t\t\t\t: initializedLazyField( fieldName, entity, session );\n\t}\n\n\tprivate Object initializedLazyField(\n\t\t\tString fieldName,\n\t\t\tObject entity,\n\t\t\tSharedSessionContractImplementor session) {\n\t\tfinal Object id = session.getContextEntityIdentifier( entity );\n\t\tfinal var entry = session.getPersistenceContext().getEntry( entity );\n\t\tif ( entry == null ) {\n\t\t\tthrow new HibernateException( \"entity is not associated with the session: \" + id );\n\t\t}\n\n\t\tif ( CORE_LOGGER.isTraceEnabled() ) {\n\t\t\tCORE_LOGGER.initializingLazyPropertiesOf(\n\t\t\t\t\tinfoString( this, id, getFactory() ),\n\t\t\t\t\tfieldName\n\t\t\t);\n\t\t}\n\n\t\t// attempt to read it from second-level cache\n\t\tif ( session.getCacheMode().isGetEnabled()\n\t\t\t\t&& isLazyPropertiesCacheable() ) {\n\t\t\tfinal Object cachedValue = initializeLazyFieldFromCache( fieldName, entity, session, id, entry );\n\t\t\tif ( cachedValue != UNFETCHED_PROPERTY ) {\n\t\t\t\t// The following should be redundant, since the setter should have set this already.\n\t\t\t\t// interceptor.attributeInitialized(fieldName);\n\n\t\t\t\t// NOTE EARLY EXIT!!!","sourceCodeStart":1551,"sourceCodeEnd":1587,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java#L1551-L1587","documentation":"Thrown during lazy property/field initialization: the entity instance being initialized is not present in the given session's persistence context (session.getContextEntityIdentifier could not tie the entity to the session). This is the guard for calling lazy initialization on a detached or otherwise unassociated entity; the faulty input is the (entity, session) pair that are not associated.","triggerScenarios":"Thrown when an operation requires the entity to be managed by the current session but the instance is detached or transient.","commonSituations":"See trigger scenarios.","solutions":["Re-attach the entity with merge() or load it in the current session before performing the operation.","Do not pass detached entities to operations requiring a managed instance."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}