hibernate/hibernate-orm · error · HibernateException

Could not instantiate class of type

Error message

Could not instantiate class of type 

What it means

HibernateException thrown by EntityCopyObserverFactoryFromClass.createEntityCopyObserver when Class.newInstance() on the configured entity-copy-observer class fails. The factory record wraps the class named by the hibernate.event.merge.entity_copy_observer setting when it is not one of the built-in values (disallow/allow/log); startup of the merge machinery needs an instance, and reflection instantiation failed for any reason (no accessible no-arg constructor, abstract/interface class, or constructor threw).

Source

Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/EntityCopyObserverFactoryInitiator.java:95

			return value;
		}
	}

	@Nonnull
	@Override
	public Class<EntityCopyObserverFactory> getServiceInitiated() {
		return EntityCopyObserverFactory.class;
	}

	private record EntityCopyObserverFactoryFromClass(Class<? extends EntityCopyObserver> observerClass)
			implements EntityCopyObserverFactory {
		@Override
		public @Nonnull EntityCopyObserver createEntityCopyObserver() {
			try {
				return observerClass.newInstance();
			}
			catch (Exception e) {
				throw new HibernateException( "Could not instantiate class of type " + observerClass.getName() );
			}
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Give the observer class a public no-arg constructor and make it a top-level or static nested class
  2. Verify it implements org.hibernate.event.spi.EntityCopyObserver and does its work in entityCopyDetected/topLevelShareDone rather than the constructor
  3. Move external dependencies to lazy lookup (e.g. static holder) so construction cannot fail at merge time

Example fix

// before
public class MyObserver implements EntityCopyObserver { // only a (DataSource ds) constructor -> boom
}

// after
public class MyObserver implements EntityCopyObserver {
    public MyObserver() { }
    private DataSource ds() { return AppContext.dataSource(); } // lazy dependency
    @Override public void entityCopyDetected(Object managed, Object m1, Object m2, EventSource s) { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

Class<?> clazz = Class.forName(observerClassName);
int mods = clazz.getModifiers();
if (!Modifier.isPublic(mods) || Modifier.isAbstract(mods)
        || !EntityCopyObserver.class.isAssignableFrom(clazz)) {
    throw new IllegalStateException(observerClassName + " is not a usable observer");
try {
    clazz.getConstructor(); // must have public no-arg constructor
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(observerClassName + " needs a public no-arg constructor");
}

Type guard

boolean instantiableObserver(String className) {
    try {
        Class<?> c = Class.forName(className);
        return EntityCopyObserver.class.isAssignableFrom(c) && c.getConstructor() != null;
    } catch (Exception e) { return false; }
}

Try / catch

try {
    sf = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
    if (e.getMessage() != null && e.getMessage().contains("Could not instantiate")) {
        // fix the hibernate.event.merge.entity_copy_observer class definition
    }
    throw e;
}

Prevention

When it happens

Trigger: Setting hibernate.event.merge.entity_copy_observer to a custom class name (e.g. com.acme.MyObserver) that has no public no-arg constructor, is a non-static inner class, is abstract, or whose constructor throws; the exception fires the first time a session performs a merge that detects a copy.

Common situations: Custom observer implemented as an inner class without 'static'; observer classes that expect constructor injection (Spring-style) but are configured by class name so the container never constructs them; constructor doing eager configuration lookups that fail in the target environment.

Related errors


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