{"record":{"id":"692068ca57ef0e00","repo":"hibernate/hibernate-orm","slug":"session-is-not-an-eventsource","errorCode":null,"errorMessage":"session is not an EventSource","messagePattern":"session is not an EventSource","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java","lineNumber":513,"sourceCode":"\t * <p>\n\t * Only stateful session are sources of events. If this object is\n\t * a stateless session, this method return {@code false}.\n\t */\n\tdefault boolean isEventSource() {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Cast this session to {@link EventSource} if possible.\n\t * <p>\n\t * Only stateful session are sources of events. If this object is\n\t * a stateless session, this method throws.\n\t *\n\t * @throws ClassCastException if the cast is not possible\n\t */\n\t@Nonnull\n\tdefault EventSource asEventSource() {\n\t\tthrow new ClassCastException( \"session is not an EventSource\" );\n\t}\n\n\t/**\n\t * Whether the session {@linkplain StatelessSessionImplementor stateless}, as opposed tp\n\t * {@linkplain SessionImplementor stateful}.\n\t *\n\t * @apiNote Essentially, whether casting this session to {@linkplain StatelessSessionImplementor} will succeed.\n\t */\n\tdefault boolean isStateless() {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Called after each operation on a {@link org.hibernate.ScrollableResults},\n\t * providing an opportunity for a stateless session to clear its\n\t * temporary persistence context. For a stateful session, this method\n\t * does nothing.\n\t */","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java#L495-L531","documentation":"SharedSessionContractImplementor.asEventSource() is the typed view of a session used by Hibernate's event/listener pipeline and ActionQueue; only the stateful SessionImpl (and delegators wrapping it) override the default, which throws ClassCastException('session is not an EventSource'). A StatelessSession therefore cannot act as an EventSource.","triggerScenarios":"Calling asEventSource() on a StatelessSession directly, or reaching internal code that does — bulk operation cleanup (BulkOperationCleanupAction), version increments, natural-id resolution callbacks, session event construction (AbstractSessionEvent), and result-set processing all call session.asEventSource() and fail on a stateless session.","commonSituations":"Batch jobs written against StatelessSession hitting features that assume a stateful session (action queue, event listeners, cascade via events, Envers-style integrations); user utilities casting SharedSessionContractImplementor to EventSource; mixing session types behind a shared interface.","solutions":["Use a regular stateful Session for code paths that need the event pipeline / ActionQueue","Branch first: check session.isStateless() (or session instanceof EventSource) before casting","Avoid StatelessSession features that internally require an ActionQueue (e.g. cascading version increments, listener-driven cascades) or restructure them as explicit operations"],"exampleFix":"// before\nEventSource src = (EventSource) session; // or session.asEventSource() on a StatelessSession -> ClassCastException\n\n// after\nif (session.isStateless()) {\n    throw new UnsupportedOperationException(\"this operation needs a stateful Session\");\n}\nEventSource src = session.asEventSource();","handlingStrategy":"type-guard","validationCode":"if (sharedSession.isStateless()) {\n    throw new UnsupportedOperationException(\"Operation requires a stateful Session (EventSource)\");\n}","typeGuard":"static boolean canActAsEventSource(SharedSessionContractImplementor s) {\n    return !s.isStateless(); // equivalent to: s instanceof EventSource\n}","tryCatchPattern":null,"preventionTips":["Check isStateless() before casting sessions to EventSource","Document which utility methods require a stateful session and enforce it with an early check","Don't run event-pipeline-dependent features (ActionQueue callbacks, listener cascades) on StatelessSession"],"tags":["hibernate","stateless-session","class-cast","event-source","session-type"],"backgroundTag":"wrong-session-type","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}