hibernate/hibernate-orm · error · HibernateException
Unable to instantiate specified auto SessionEventListener
Error message
Unable to instantiate specified auto SessionEventListener: {} What it means
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.
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)
Example fix
// before:
props.put("hibernate.session.events.auto", AuditListener.class.getName());
// AuditListener only has AuditListener(AuditService svc)
// after:
public AuditListener() { this.svc = AuditServiceHolder.get(); } // no-arg, lazy collaborator Defensive patterns
Strategy: try-catch
Validate before calling
// validate the class named by hibernate.session.events.auto before boot
String name = (String) props.get("hibernate.session.events.auto");
if (name != null) {
Class<?> c = Class.forName(name);
if (!SessionEventListener.class.isAssignableFrom(c))
throw new IllegalStateException("Not a SessionEventListener: " + name);
c.getConstructor(); // requires public no-arg ctor
} Type guard
static boolean isValidAutoSessionEventListener(String fqcn) {
try {
Class<?> c = Class.forName(fqcn);
return SessionEventListener.class.isAssignableFrom(c) && hasPublicNoArgCtor(c);
} catch (ReflectiveOperationException e) { return false; }
} Try / catch
try {
sessionFactory = metadata.buildSessionFactory();
} catch (HibernateException e) {
if (e.getMessage() != null && e.getMessage().contains("auto SessionEventListener")) {
// remove/fix hibernate.session.events.auto; e.getCause() explains the failure
}
throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not instantiate class of type
- Could not instantiate event listener
- Could not instantiate named dialect class
- Could not instantiate named strategy class
- The class [ ] could not be instantiated
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/89b1bb26a9c4973d.
Report an issue: GitHub.
Appendix: source
Thrown at hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java:1210
public SessionEventListener[] buildSessionEventListeners() {
if ( StatisticalLoggingSessionEventListener.isLoggingEnabled() ) {
return autoSessionEventListener == null
? new SessionEventListener[] { statsListener() }
: new SessionEventListener[] { statsListener(), instantiateAutoSessionEventListener() };
}
else {
return autoSessionEventListener == null
? EMPTY_SESSION_EVENT_LISTENERS
: new SessionEventListener[] { instantiateAutoSessionEventListener() };
}
}
private SessionEventListener instantiateAutoSessionEventListener() {
try {
return autoSessionEventListener.getConstructor().newInstance();
}
catch (Exception e) {
throw new HibernateException(
"Unable to instantiate specified auto SessionEventListener: " + autoSessionEventListener.getName(),
e
);
}
}
private static SessionEventListener statsListener() {
return new StatisticalLoggingSessionEventListener();
}
@Override
public boolean isIdentifierRollbackEnabled() {
return identifierRollbackEnabled;
}
@Override
public boolean isCheckNullability() {
return checkNullability;View on GitHub (pinned to fad1729dce)