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

  1. Add a public no-arg constructor to the bean class.
  2. Make the class public and top-level (or a static nested class).
  3. For Kotlin, give all constructor parameters defaults or add a secondary no-arg constructor.
  4. Check the nested cause: if the constructor itself threw, fix that failure rather than the signature.
  5. 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

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


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/cd33945074e50c45. Report an issue: GitHub.