{"record":{"id":"e72d3895974608d8","repo":"hibernate/hibernate-orm","slug":"this-store-does-not-support-null-keys","errorCode":null,"errorMessage":"This store does not support null keys","messagePattern":"This store does not support null keys","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/util/collections/InstanceIdentityStore.java","lineNumber":81,"sourceCode":"\t\t\t\tthrow new ConcurrentModificationException(\n\t\t\t\t\t\t\"Found a different instance corresponding to instanceId [\" + instanceId +\n\t\t\t\t\t\t\"], this might indicate a concurrent access to this persistence context.\"\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\treturn null;\n\t}\n\n\t/**\n\t * Associates the specified value with the specified key in this store (optional operation). If the store\n\t * previously contained a mapping for the key, the old value is replaced by the specified value.\n\t *\n\t * @param key key with which the specified value is to be associated\n\t * @param value value to be associated with the specified key\n\t */\n\tpublic void put(Object key, int instanceId, V value) {\n\t\tif ( key == null ) {\n\t\t\tthrow new NullPointerException( \"This store does not support null keys\" );\n\t\t}\n\t\telse if ( instanceId <= 0 ) {\n\t\t\tthrow new IllegalArgumentException( \"Instance ID must be a positive value\" );\n\t\t}\n\n\t\tfinal int keyIndex = toKeyIndex( instanceId );\n\t\tfinal Page<Object> page = getOrCreateEntryPage( keyIndex );\n\t\tfinal int pageOffset = toPageOffset( keyIndex );\n\t\tpage.set( pageOffset, key );\n\t\tpage.set( pageOffset + 1, value );\n\t}\n\n\t/**\n\t * Removes the mapping for an instance id from this store if it is present (optional operation).\n\t *\n\t * @param instanceId the instance id whose associated value is to be returned\n\t * @param key key instance to double-check instance equality\n\t * @implNote This method accesses the backing array with the provided instance id, but performs an instance","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/util/collections/InstanceIdentityStore.java#L63-L99","documentation":"InstanceIdentityStore.put(Object key, int instanceId, V value) rejects null keys up front with NullPointerException and 'This store does not support null keys'. Entries are addressed by instanceId, but the key is stored alongside for identity verification (the k == key checks in get/remove), so a null key cannot be stored without breaking that verification.","triggerScenarios":"put(null, instanceId, value) — an entity reference that is null because a preceding load/find returned null (row absent) or a variable was never assigned.","commonSituations":"Cache-fill code that puts the result of session.get() without a null check for missing rows; refactoring that turned a guaranteed non-null key into an Optional-unwrapped null.","solutions":["Check key != null before put and handle the missing-entity case explicitly","Trace the null to its source — usually session.find/get on a deleted or never-existing row","Do not 'fix' by passing a placeholder key; the identity checks would then throw elsewhere"],"exampleFix":"// before\nstore.put( entity, id, state );\n// after\nif ( entity == null ) {\n    return; // entity absent: nothing to cache\n}\nstore.put( entity, id, state );","handlingStrategy":"validation","validationCode":"Objects.requireNonNull( key, \"entity key must not be null\" );\nstore.put( key, instanceId, value );","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check entity references for null right after load/find, before caching them","Model 'entity not found' explicitly instead of storing null keys","Fail fast with a descriptive message at your own API boundary rather than letting the store throw"],"tags":["hibernate","null-key","map","validation","instance-identity"],"backgroundTag":"null-key-not-allowed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}