hibernate/hibernate-orm · error · InstantiationException
Could not instantiate managed bean directly
Error message
Could not instantiate managed bean directly
What it means
FallbackBeanInstanceProducer is Hibernate's last-resort producer: it locates the declared no-arg constructor, calls setAccessible(true) and newInstance(). Any failure there - no no-arg constructor, non-public class/constructor, abstract or interface type, or the constructor throwing - is reported as InstantiationException('Could not instantiate managed bean directly').
Source
Thrown at hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java:41
public class FallbackBeanInstanceProducer implements BeanInstanceProducer {
/**
* Singleton access
*/
public static final FallbackBeanInstanceProducer INSTANCE = new FallbackBeanInstanceProducer();
private FallbackBeanInstanceProducer() {
}
@Override
public <B> B produceBeanInstance(Class<B> beanType) {
BEANS_MSG_LOGGER.creatingManagedBeanUsingDirectInstantiation( beanType.getName() );
try {
final var constructor = beanType.getDeclaredConstructor();
constructor.setAccessible( true );
return constructor.newInstance();
}
catch (Exception e) {
throw new InstantiationException( "Could not instantiate managed bean directly", beanType, e );
}
}
@Override
public <B> B produceBeanInstance(String name, Class<B> beanType) {
return produceBeanInstance( beanType );
}
}
View on GitHub (pinned to fad1729dce)
Solutions
- Add a public no-arg constructor to the bean class.
- Make the class public and top-level (or a static nested class).
- For Kotlin, give all constructor parameters defaults or add a secondary no-arg constructor.
- Check the nested cause: if the constructor itself threw, fix that failure rather than the signature.
- Register the class as a real CDI bean so CDI handles creation instead of the fallback.
Example fix
// before
public final class CreatedAtListener {
private final Clock clock;
public CreatedAtListener(Clock clock) { this.clock = clock; } // no no-arg ctor
}
// after
public final class CreatedAtListener {
private final Clock clock = Clock.systemUTC();
public CreatedAtListener() { }
} Defensive patterns
Strategy: validation
Validate before calling
// preflight: can the fallback producer construct this class?
static void assertFallbackConstructable(Class<?> c) {
try {
java.lang.reflect.Constructor<?> ctor = c.getDeclaredConstructor();
ctor.setAccessible(true);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(c.getName() + " needs an accessible no-arg constructor", e);
}
} Try / catch
try {
return producer.produceBeanInstance(type);
} catch (org.hibernate.InstantiationException e) {
// direct instantiation failed: report which class and why (missing/throwing ctor)
throw new BeanSetupException("Cannot instantiate " + type.getName(), e);
} Prevention
- Add a public no-arg constructor to all listener/converter/strategy classes.
- In Kotlin, use default parameter values on constructors.
- Make classes public, concrete, top-level or statically nested.
- Check the cause chain for constructors that throw.
When it happens
Trigger: Hibernate falls back to direct instantiation for a managed bean class (listener, converter, strategy) whose class lacks an accessible no-arg constructor or whose constructor/initializer throws; also when the class is abstract, an interface, or a non-static inner class.
Common situations: Entity listeners with constructor injection only; Kotlin classes without default parameter values; Java records; classes made non-public or encapsulated under JPMS where setAccessible fails; constructors that throw on missing environment dependencies.
Related errors
- CDI BeanManager is not known to be ready for use and the fal
- Could not instantiate event listener '{}'
- Unable to instantiate StatementObserver - {}
- Could not instantiate named strategy class [{}]
- Cannot instantiate the class [{}] because it does not have a
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/cd33945074e50c45.
Report an issue: GitHub.