{"record":{"id":"89b1bb26a9c4973d","repo":"hibernate/hibernate-orm","slug":"unable-to-instantiate-specified-auto-sessioneventl","errorCode":null,"errorMessage":"Unable to instantiate specified auto SessionEventListener: {}","messagePattern":"Unable to instantiate specified auto SessionEventListener: (.+?)","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java","lineNumber":1210,"sourceCode":"\tpublic SessionEventListener[] buildSessionEventListeners() {\n\t\tif ( StatisticalLoggingSessionEventListener.isLoggingEnabled() ) {\n\t\t\treturn autoSessionEventListener == null\n\t\t\t\t\t? new SessionEventListener[] { statsListener() }\n\t\t\t\t\t: new SessionEventListener[] { statsListener(), instantiateAutoSessionEventListener() };\n\t\t}\n\t\telse {\n\t\t\treturn autoSessionEventListener == null\n\t\t\t\t\t? EMPTY_SESSION_EVENT_LISTENERS\n\t\t\t\t\t: new SessionEventListener[] { instantiateAutoSessionEventListener() };\n\t\t}\n\t}\n\n\tprivate SessionEventListener instantiateAutoSessionEventListener() {\n\t\ttry {\n\t\t\treturn autoSessionEventListener.getConstructor().newInstance();\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new HibernateException(\n\t\t\t\t\t\"Unable to instantiate specified auto SessionEventListener: \" + autoSessionEventListener.getName(),\n\t\t\t\t\te\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate static SessionEventListener statsListener() {\n\t\treturn new StatisticalLoggingSessionEventListener();\n\t}\n\n\t@Override\n\tpublic boolean isIdentifierRollbackEnabled() {\n\t\treturn identifierRollbackEnabled;\n\t}\n\n\t@Override\n\tpublic boolean isCheckNullability() {\n\t\treturn checkNullability;","sourceCodeStart":1192,"sourceCodeEnd":1228,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java#L1192-L1228","documentation":"The hibernate.session.events.auto setting names a SessionEventListener implementation class that Hibernate instantiates once per new Session. SessionFactoryOptionsBuilder.instantiateAutoSessionEventListener calls its public no-arg constructor; any failure throws HibernateException naming the class with the cause attached. Instantiation happens when the baseline listener array is built, i.e. at SessionFactory creation or first session use.","triggerScenarios":"Setting hibernate.session.events.auto=com.acme.MyListener where the class has no public no-arg constructor, is abstract, or its constructor throws when the SessionFactory initializes the auto listener array.","commonSituations":"Custom listener written with constructor injection only (expects a collaborator); constructor performs logging/metrics setup that fails in certain environments; class name typo or class not deployed; copying the built-in StatisticalLoggingSessionEventListener into a subclass with extra constructor args.","solutions":["Give the listener class a public no-argument constructor","Inspect the nested cause to see whether construction threw (InvocationTargetException) or the shape was wrong","Verify the class is on the classpath and the property value matches its FQCN","For stateless use, confirm the listener can be instantiated repeatedly (one instance per Session)"],"exampleFix":"// before:\nprops.put(\"hibernate.session.events.auto\", AuditListener.class.getName());\n// AuditListener only has AuditListener(AuditService svc)\n\n// after:\npublic AuditListener() { this.svc = AuditServiceHolder.get(); } // no-arg, lazy collaborator","handlingStrategy":"try-catch","validationCode":"// validate the class named by hibernate.session.events.auto before boot\nString name = (String) props.get(\"hibernate.session.events.auto\");\nif (name != null) {\n    Class<?> c = Class.forName(name);\n    if (!SessionEventListener.class.isAssignableFrom(c))\n        throw new IllegalStateException(\"Not a SessionEventListener: \" + name);\n    c.getConstructor(); // requires public no-arg ctor\n}","typeGuard":"static boolean isValidAutoSessionEventListener(String fqcn) {\n    try {\n        Class<?> c = Class.forName(fqcn);\n        return SessionEventListener.class.isAssignableFrom(c) && hasPublicNoArgCtor(c);\n    } catch (ReflectiveOperationException e) { return false; }\n}","tryCatchPattern":"try {\n    sessionFactory = metadata.buildSessionFactory();\n} catch (HibernateException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"auto SessionEventListener\")) {\n        // remove/fix hibernate.session.events.auto; e.getCause() explains the failure\n    }\n    throw e;\n}","preventionTips":["Keep the auto listener stateless with a public no-arg constructor — one instance is created per Session","Use the well-known built-in 'org.hibernate.internal.StatisticalLoggingSessionEventListener' when you just need logging","Prefer SessionFactoryBuilder#applyAutoSessionEventsListener or programmatic SessionBuilder listeners over raw properties"],"tags":["hibernate","session-events","listener","reflection","configuration"],"backgroundTag":"reflection-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}