{"record":{"id":"2bd34529d926acc7","repo":"hibernate/hibernate-orm","slug":"null-passed-to-session-evict","errorCode":null,"errorMessage":"null passed to Session.evict()","messagePattern":"null passed to Session\\.evict\\(\\)","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java","lineNumber":45,"sourceCode":" *\n * @author Steve Ebersole\n */\npublic class DefaultEvictEventListener implements EvictEventListener {\n\n\t/**\n\t * Handle the given evict event.\n\t *\n\t * @param event The evict event to be handled.\n\t *\n\t */\n\t@Override\n\tpublic void onEvict(@Nonnull EvictEvent event) {\n\t\tfinal var source = event.getSession();\n\t\tfinal var persistenceContext = source.getPersistenceContextInternal();\n\t\tfinal Object object = event.getObject();\n\t\t//noinspection ConstantValue\n\t\tif ( object == null ) {\n\t\t\tthrow new NullPointerException( \"null passed to Session.evict()\" );\n\t\t}\n\t\tfinal var lazyInitializer = extractLazyInitializer( object );\n\t\tif ( lazyInitializer != null ) {\n\t\t\tfinal Object id = lazyInitializer.getInternalIdentifier();\n\t\t\tif ( id == null ) {\n\t\t\t\tthrow new IllegalArgumentException( \"Could not determine identifier of proxy passed to evict()\" );\n\t\t\t}\n\t\t\tfinal var persister =\n\t\t\t\t\tsource.getFactory().getMappingMetamodel()\n\t\t\t\t\t\t\t.getEntityDescriptor( lazyInitializer.getEntityName() );\n\t\t\tfinal var key = source.generateEntityKey( id, persister );\n\t\t\tfinal var holder = persistenceContext.detachEntity( key );\n\t\t\t// if the entity has been evicted then its holder is null\n\t\t\tif ( holder != null && !lazyInitializer.isUninitialized() ) {\n\t\t\t\tfinal Object entity = holder.getEntity();\n\t\t\t\tif ( entity != null ) {\n\t\t\t\t\tfinal var entry = persistenceContext.removeEntry( entity );\n\t\t\t\t\tdoEvict( entity, key, entry.getPersister(), event.getSession() );","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultEvictEventListener.java#L27-L63","documentation":"DefaultEvictEventListener.onEvict() rejects a null argument with NullPointerException('null passed to Session.evict()'). evict() detaches one specific managed entity; passing null is a caller programming error, and Hibernate fails fast with an explicit message instead of silently ignoring the call.","triggerScenarios":"Session.evict(null) or EntityManager.detach(null) — typically a null flowing from an optional lookup, Map.get() miss, or unwrapped Optional into the detach call.","commonSituations":"Cleanup code evicting entities from a map of optional results; helper methods that evict 'previous' entities when no previous exists; null returned by find() feeding an unconditional evict.","solutions":["Null-check the reference before evict/detach","Use Objects.requireNonNull(entity, ...) to fail with your own message at the correct call site","Reconsider whether evict() should run at all for optional references — it only applies to managed entities","Guard loops that evict collections with filter(Objects::nonNull)"],"exampleFix":"// before\nsession.evict(maybeCustomer); // maybeCustomer may be null\n\n// after\nif (maybeCustomer != null) {\n    session.evict(maybeCustomer);\n}","handlingStrategy":"validation","validationCode":"java.util.Objects.requireNonNull(entity, \"entity to evict must not be null\");\nsession.evict(entity);","typeGuard":"static boolean evictable(Object o) {\n    return o != null;\n}","tryCatchPattern":null,"preventionTips":["Null-check before every evict()/detach() call","Guard optional lookups: session.find(...) results can be null","Filter nulls before evicting from collections","Fail fast with requireNonNull at the source rather than deep in Hibernate"],"tags":["session","null-pointer","evict","validation"],"backgroundTag":"null-argument-to-api","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}