{"record":{"id":"191d71e2738aec97","repo":"hibernate/hibernate-orm","slug":"attempt-to-load-entity-from-cache-using-provided-o","errorCode":null,"errorMessage":"Attempt to load entity from cache using provided object instance, but cache is storing references: {}","messagePattern":"Attempt to load entity from cache using provided object instance, but cache is storing references: (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/loader/internal/CacheLoadHelper.java","lineNumber":184,"sourceCode":"\t\t\t}\n\t\t\telse {\n\t\t\t\tstatistics.entityCacheHit( rootEntityRole, regionName );\n\t\t\t}\n\t\t}\n\t\treturn cacheEntry;\n\t}\n\n\tprivate static Object processCachedEntry(\n\t\t\tfinal Object instanceToLoad,\n\t\t\tfinal EntityPersister persister,\n\t\t\tfinal Object cacheEntry,\n\t\t\tfinal SharedSessionContractImplementor source,\n\t\t\tfinal EntityKey entityKey) {\n\t\tfinal var entry = (CacheEntry)\n\t\t\t\tpersister.getCacheEntryStructure().destructure( cacheEntry, source.getFactory() );\n\t\tif ( entry.isReferenceEntry() ) {\n\t\t\tif ( instanceToLoad != null ) {\n\t\t\t\tthrow new HibernateException( \"Attempt to load entity from cache using provided object instance, \"\n\t\t\t\t\t\t+ \"but cache is storing references: \" + entityKey.getIdentifier() );\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn convertCacheReferenceEntryToEntity( (ReferenceCacheEntryImpl) entry, source, entityKey );\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tfinal Object entity =\n\t\t\t\t\tconvertCacheEntryToEntity(\n\t\t\t\t\t\t\tentry,\n\t\t\t\t\t\t\tentityKey.getIdentifier(),\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\tpersister,\n\t\t\t\t\t\t\tinstanceToLoad,\n\t\t\t\t\t\t\tentityKey\n\t\t\t\t\t);\n\t\t\tif ( entity == null ) {\n\t\t\t\treturn null;","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/loader/internal/CacheLoadHelper.java#L166-L202","documentation":"The second-level cache stores either a disassembled CacheEntry or, when reference caching is enabled, a ReferenceCacheEntryImpl holding the entity instance itself. When a cached lookup finds a reference entry but the load was given an existing instance to populate (instanceToLoad != null), Hibernate throws, because a shared cached reference cannot be written into a caller-provided object.","triggerScenarios":"hibernate.cache.use_reference_entries=true (AvailableSettings.USE_DIRECT_REFERENCE_CACHE_ENTRIES) combined with a load path that supplies an instance: two-arg session.load(id, instance), refresh into a provided instance, or a custom loader/event listener that sets an instance on the load event; the entity is immutable and cached.","commonSituations":"Enabling reference entries to speed up immutable entities and later calling load/refresh with a provided instance; Hibernate 5-to-6 migration where reference-cache-entry handling changed; custom event listeners injecting instances into LoadEvent.","solutions":["Remove hibernate.cache.use_reference_entries=true so the cache stores normal disassembled entries (default false).","Avoid load-with-instance calls (two-arg load, refresh into an instance) for entities cached with reference entries; use plain byId loads.","If reference caching must stay, keep those entities immutable and route all access through instance-free loads."],"exampleFix":"// before\nprops.put(AvailableSettings.USE_DIRECT_REFERENCE_CACHE_ENTRIES, true);\n// ...\nsession.load(userId, existingUser); // load-with-instance conflicts with reference entries\n\n// after: reference entries disabled (default)\nUser u = session.byId(User.class).load(userId);","handlingStrategy":"fallback","validationCode":"if ( sessionFactory.getSessionFactoryOptions().isDirectReferenceCacheEntriesEnabled() ) {\n    // do not pass instances into load/refresh for cached entities\n    return session.byId( entityClass ).load( id );\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.refresh( instance );\n}\ncatch ( org.hibernate.HibernateException e ) {\n    if ( e.getMessage() != null && e.getMessage().contains( \"cache is storing references\" ) ) {\n        // fall back to a plain load and copy state manually\n    }\n    else {\n        throw e;\n    }\n}","preventionTips":["Keep USE_DIRECT_REFERENCE_CACHE_ENTRIES off unless every access path avoids load-with-instance","Document per-entity cache strategies when mixing immutable and mutable entities","Integration-test cache hits through every load API the application uses"],"tags":["hibernate","second-level-cache","caching"],"backgroundTag":"second-level-cache-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}