hibernate/hibernate-orm · error · EventListenerRegistrationException
Unable to instantiate specified event listener class:
Error message
Unable to instantiate specified event listener class:
What it means
EventListenerRegistrationException wrapping the failure of Class.newInstance() on an event listener class, thrown from EventListenerRegistryImpl.instantiateListener. It is used by the registry's class-based registration paths (e.g. setListenerClasses / listener classes declared in configuration); the original reflective error (NoSuchMethodException, IllegalAccessException, or a constructor exception) is attached as the cause. The message names the listener class that could not be built.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/event/service/internal/EventListenerRegistryImpl.java:155
private <T> @Nonnull T resolveListenerInstance(@Nonnull Class<T> listenerClass) {
final T listenerInstance = listenerClass.cast( listenerClassToInstanceMap.get( listenerClass ) );
if ( listenerInstance == null ) {
final T newListenerInstance = instantiateListener( listenerClass );
listenerClassToInstanceMap.put( listenerClass, newListenerInstance );
return newListenerInstance;
}
else {
return listenerInstance;
}
}
private <T> @Nonnull T instantiateListener(@Nonnull Class<T> listenerClass) {
try {
//noinspection deprecation
return listenerClass.newInstance();
}
catch ( Exception e ) {
throw new EventListenerRegistrationException(
"Unable to instantiate specified event listener class: " + listenerClass.getName(),
e
);
}
}
@Override
@SafeVarargs
public final <T> void setListeners(@Nonnull EventType<T> type, @Nullable T... listeners) {
final var registeredListeners = getEventListenerGroup( type );
registeredListeners.clear();
if ( listeners != null ) {
for ( T listener : listeners ) {
registeredListeners.appendListener( listener );
}
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Register listener instances instead of classes — construct them yourself (or via DI) and call registry.setListeners(eventType, instance), keeping dependencies injected
- If class-based registration is required, provide a public no-arg constructor and defer all dependency resolution to first callback
- Read the cause chain: NoSuchMethodException -> missing no-arg ctor; InvocationTargetException -> constructor logic failed — fix that code
Example fix
// before registry.setListenerClasses(EventType.POST_INSERT, MyAuditingListener.class); // MyAuditingListener(DataSource ds) only -> Unable to instantiate specified event listener class // after MyAuditingListener listener = new MyAuditingListener(dataSource); registry.setListeners(EventType.POST_INSERT, listener);
Defensive patterns
Strategy: validation
Validate before calling
try {
listenerClass.getConstructor(); // public no-arg constructor present?
} catch (NoSuchMethodException e) {
// register the instance instead of the class:
registry.setListeners(eventType, new MyListener(deps));
return;
}
registry.setListenerClasses(eventType, listenerClass); Type guard
boolean hasPublicNoArgCtor(Class<?> c) {
try { return Modifier.isPublic(c.getConstructor().getModifiers()); }
catch (NoSuchMethodException e) { return false; }
} Try / catch
try {
registry.setListenerClasses(EventType.POST_INSERT, MyListener.class);
} catch (EventListenerRegistrationException e) {
Throwable cause = e.getCause();
if (cause instanceof NoSuchMethodException) {
registry.setListeners(EventType.POST_INSERT, new MyListener()); // instance-based fallback
} else { throw e; }
} Prevention
- Prefer instance-based setListeners over class-based registration when listeners need dependencies
- Keep listener constructors public, no-arg, and side-effect free
- Smoke-test listener construction during application startup, not lazily at first event
When it happens
Trigger: Declaring an event listener as a class name in hibernate.cfg.xml <event> blocks or calling registry.setListenerClasses(type, MyListener.class) where MyListener lacks a public no-arg constructor, is non-public/abstract, or its constructor throws (NPE, missing dependency, failed config lookup) during bootstrap or runtime listener replacement.
Common situations: Listeners designed as Spring beans being registered by class instead of by instance, so dependency injection never happens and constructors fail; constructors performing environment lookups (JNDI, config files) that fail outside the dev environment; refactoring that added constructor parameters without updating a class-based registration path.
Related errors
- Could not instantiate event listener '{}'
- Unable to instantiate StatementObserver - {}
- Could not instantiate named strategy class [%s]
- Unable to instantiate ScanningProvider `%s`
- Unable to instantiate Scanner `%s`
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/10c67a6ce08166a6.
Report an issue: GitHub.