{"record":{"id":"585783e08bb5d160","repo":"hibernate/hibernate-orm","slug":"cachemode-cannot-be-null","errorCode":null,"errorMessage":"CacheMode cannot be null","messagePattern":"CacheMode cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java","lineNumber":1632,"sourceCode":"\t}\n\n\t@Override\n\t@Nonnull\n\tpublic final JdbcServices getJdbcServices() {\n\t\treturn jdbcServices;\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic CacheMode getCacheMode() {\n\t\treturn cacheMode;\n\t}\n\n\t@Override\n\tpublic void setCacheMode(@Nonnull CacheMode cacheMode) {\n\t\t//noinspection ConstantValue\n\t\tif ( cacheMode == null ) {\n\t\t\tthrow new IllegalArgumentException( \"CacheMode cannot be null\" );\n\t\t}\n\t\tcheckSessionReentrancy();\n\t\tthis.cacheMode = cacheMode;\n\t}\n\n\t@Override\n\tpublic void setCriteriaCopyTreeEnabled(boolean jpaCriteriaCopyComplianceEnabled) {\n\t\tcriteriaCopyTreeEnabled = jpaCriteriaCopyComplianceEnabled;\n\t}\n\n\t@Override\n\tpublic boolean isCriteriaCopyTreeEnabled() {\n\t\treturn criteriaCopyTreeEnabled;\n\t}\n\n\t@Override\n\tpublic boolean isCriteriaPlanCacheEnabled() {\n\t\treturn criteriaPlanCacheEnabled;","sourceCodeStart":1614,"sourceCodeEnd":1650,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java#L1614-L1650","documentation":"Session.setCacheMode declares @Nonnull and rejects null immediately with IllegalArgumentException. CacheMode is an enum-like contract (NORMAL, IGNORE, GET, PUT, REFRESH); there is no 'unset' value — the default is NORMAL — so null cannot be interpreted and is treated as a caller bug.","triggerScenarios":"session.setCacheMode(null), or setCacheMode(configValue) where the config lookup returned null: missing properties entry, Optional.empty() path, map.get() with a wrong key, or a nullable field from a properties class.","commonSituations":"Wiring CacheMode from a Spring property/environment entry that is not guaranteed to exist; copy-paste from code where CacheMode was optional; renaming a configuration key so the lookup silently returns null.","solutions":["Default before setting: setCacheMode(cacheMode != null ? cacheMode : CacheMode.NORMAL).","Fix the property source so the value is present, and validate configuration keys at startup with a fail-fast check.","If the intent is 'cache normally', pass CacheMode.NORMAL explicitly."],"exampleFix":"// before\nCacheMode mode = props.containsKey(\"cache.mode\") ? CacheMode.parse(props.get(\"cache.mode\")) : null;\nsession.setCacheMode(mode); // IllegalArgumentException when key missing\n// after\nCacheMode mode = props.containsKey(\"cache.mode\") ? CacheMode.parse(props.get(\"cache.mode\")) : CacheMode.NORMAL;\nsession.setCacheMode(mode);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static CacheMode requireCacheMode(@Nullable CacheMode mode) {\n    return mode != null ? mode : CacheMode.NORMAL;\n}\n\n// usage\nsession.setCacheMode(requireCacheMode(resolveFromConfig(props)));","tryCatchPattern":null,"preventionTips":["Validate nullable config-derived values at startup, not at each setter call","Treat @Nonnull-annotated Hibernate API parameters as hard contracts in code review","Prefer passing explicit CacheMode.NORMAL over computed nulls"],"tags":["hibernate","cache","null-check","argument-validation","session"],"backgroundTag":"null-argument","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}