{"record":{"id":"54e3e3b5e28a7c86","repo":"hibernate/hibernate-orm","slug":"attempted-to-load-into-an-instance-that-was-alread","errorCode":null,"errorMessage":"attempted to load into an instance that was already associated with the session: ${infoString(persister, event.getEntityId(), event.getFactory())}","messagePattern":"attempted to load into an instance that was already associated with the session: (.+?)","errorType":"exception","errorClass":"PersistentObjectException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java","lineNumber":193,"sourceCode":"\t *\n\t * @param event The initiating load request event\n\t * @param persister The persister corresponding to the entity to be loaded\n\t * @param keyToLoad The key of the entity to be loaded\n\t * @param options The defined load options\n\t *\n\t * @return The loaded entity.\n\t */\n\t@Nullable\n\tprivate Object load(\n\t\t\t@Nonnull LoadEvent event,\n\t\t\t@Nonnull EntityPersister persister,\n\t\t\t@Nonnull EntityKey keyToLoad,\n\t\t\t@Nonnull LoadType options) {\n\t\tfinal Object instanceToLoad = event.getInstanceToLoad();\n\t\tif ( instanceToLoad != null ) {\n\t\t\tfinal var session = event.getSession();\n\t\t\tif ( session.getPersistenceContextInternal().getEntry( instanceToLoad ) != null ) {\n\t\t\t\tthrow new PersistentObjectException(\n\t\t\t\t\t\t\"attempted to load into an instance that was already associated with the session: \"\n\t\t\t\t\t\t\t\t+ infoString( persister, event.getEntityId(), event.getFactory() )\n\t\t\t\t);\n\t\t\t}\n\t\t\tpersister.setIdentifier( instanceToLoad, event.getEntityId(), session );\n\t\t}\n\n\t\tfinal Object entity = doLoad( event, persister, keyToLoad, options );\n\t\tfinal boolean isOptionalInstance = instanceToLoad != null;\n\t\tif ( entity == null\n\t\t\t\t&& ( !options.isAllowNulls() || isOptionalInstance ) ) {\n\t\t\tevent.getFactory().getEntityNotFoundDelegate()\n\t\t\t\t\t.handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );\n\t\t}\n\t\telse if ( isOptionalInstance && entity != instanceToLoad ) {\n\t\t\tthrow new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );\n\t\t}\n\t\treturn entity;","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java#L175-L211","documentation":"Error \"attempted to load into an instance that was already associated with the session: ${infoString(persister, event.getEntityId(), event.getFactory())}\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when two distinct object instances with the same persistent identity would coexist in one persistence context.","commonSituations":"Typical situations: loading an entity while a detached copy with the same id is re-attached; merging a copy while the managed instance is already present; duplicate instances produced by manual construction.","solutions":["Use session.merge() instead of load() when you already hold an instance that may be associated with the session.","Evict the existing instance before loading, or reuse the already-associated instance from the 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"}