hibernate/hibernate-orm · critical · InstantiationException

Unable to instantiate specified BeanContainer

Error message

Unable to instantiate specified BeanContainer

What it means

When 'hibernate.resource.beans.container' is configured with a class or class name (not a BeanContainer instance), ManagedBeanRegistryInitiator resolves the class and instantiates it reflectively via newInstance(). Any instantiation failure - no public no-arg constructor, abstract class, non-public class - is thrown as InstantiationException('Unable to instantiate specified BeanContainer') during SessionFactory bootstrap.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistryInitiator.java:113

		final Object beanManager = settings.get( JAKARTA_CDI_BEAN_MANAGER );
		return beanManager != null ? beanManager : settings.get( CDI_BEAN_MANAGER );
	}

	private BeanContainer interpretExplicitBeanContainer(Object explicitSetting, ServiceRegistry serviceRegistry) {
		if ( explicitSetting == null ) {
			return null;
		}
		else if ( explicitSetting instanceof BeanContainer beanContainer ) {
			return beanContainer;
		}
		else {
			// otherwise we ultimately need to resolve this to a class
			final Class<?> containerClass = containerClass( explicitSetting, serviceRegistry );
			try {
				return (BeanContainer) containerClass.newInstance();
			}
			catch (Exception e) {
				throw new InstantiationException( "Unable to instantiate specified BeanContainer", containerClass, e );
			}
		}
	}

	private static Class<?> containerClass(Object explicitSetting, ServiceRegistry serviceRegistry) {
		if ( explicitSetting instanceof Class<?> clazz ) {
			return clazz;
		}
		else {
			final String name = explicitSetting.toString();
			// try the StrategySelector service
			final Class<?> selected =
					serviceRegistry.requireService( StrategySelector.class )
							.selectStrategyImplementor( BeanContainer.class, name );
			return selected == null
					? serviceRegistry.requireService( ClassLoaderService.class ).classForName( name )
					: selected;
		}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Give the BeanContainer implementation a public no-arg constructor.
  2. Pass a ready instance instead of a class name: props.put("hibernate.resource.beans.container", new MyBeanContainer()).
  3. Verify the configured FQCN actually names a concrete public class implementing org.hibernate.resource.beans.spi.BeanContainer.
  4. Check the wrapped cause for constructor-side exceptions and fix them.

Example fix

// before
props.put("hibernate.resource.beans.container", MyBeanContainer.class.getName());
public MyBeanContainer(DataSource ds) { ... } // no no-arg ctor -> InstantiationException

// after
public MyBeanContainer() { ... } // public no-arg ctor
// or pass an instance: props.put("hibernate.resource.beans.container", new MyBeanContainer());
Defensive patterns

Strategy: validation

Validate before calling

// validate an explicitly configured container class before boot
static void assertValidContainer(String fqcn) throws Exception {
    Class<?> c = Class.forName(fqcn);
    if (!org.hibernate.resource.beans.spi.BeanContainer.class.isAssignableFrom(c)
            || Modifier.isAbstract(c.getModifiers()) || !Modifier.isPublic(c.getModifiers())) {
        throw new IllegalArgumentException("Not a public concrete BeanContainer: " + fqcn);
    }
    c.getConstructor(); // requires public no-arg ctor
}

Try / catch

try {
    emf = Persistence.createEntityManagerFactory("pu", props);
} catch (org.hibernate.InstantiationException e) {
    // configured BeanContainer class could not be instantiated: fix ctor or pass an instance
    throw new ConfigurationException("Bad hibernate.resource.beans.container class", e);
}

Prevention

When it happens

Trigger: The configured BeanContainer implementation has no public no-arg constructor (only constructors taking arguments), is abstract or an interface, or is non-public so reflective instantiation fails.

Common situations: Custom BeanContainer implementations written with constructor injection; refactors that removed the default constructor; typos resolving to the wrong class; copying configuration that names an abstract base class.

Related errors


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