{"record":{"id":"1328d5ed2bafecd9","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-class-of-type","errorCode":null,"errorMessage":"Could not instantiate class of type ","messagePattern":"Could not instantiate class of type ","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/EntityCopyObserverFactoryInitiator.java","lineNumber":95,"sourceCode":"\t\t\treturn value;\n\t\t}\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic Class<EntityCopyObserverFactory> getServiceInitiated() {\n\t\treturn EntityCopyObserverFactory.class;\n\t}\n\n\tprivate record EntityCopyObserverFactoryFromClass(Class<? extends EntityCopyObserver> observerClass)\n\t\t\timplements EntityCopyObserverFactory {\n\t\t@Override\n\t\tpublic @Nonnull EntityCopyObserver createEntityCopyObserver() {\n\t\t\ttry {\n\t\t\t\treturn observerClass.newInstance();\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new HibernateException( \"Could not instantiate class of type \" + observerClass.getName() );\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":77,"sourceCodeEnd":100,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/EntityCopyObserverFactoryInitiator.java#L77-L100","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Give the observer class a public no-arg constructor and make it a top-level or static nested class","Verify it implements org.hibernate.event.spi.EntityCopyObserver and does its work in entityCopyDetected/topLevelShareDone rather than the constructor","Move external dependencies to lazy lookup (e.g. static holder) so construction cannot fail at merge time"],"exampleFix":"// before\npublic class MyObserver implements EntityCopyObserver { // only a (DataSource ds) constructor -> boom\n}\n\n// after\npublic class MyObserver implements EntityCopyObserver {\n    public MyObserver() { }\n    private DataSource ds() { return AppContext.dataSource(); } // lazy dependency\n    @Override public void entityCopyDetected(Object managed, Object m1, Object m2, EventSource s) { ... }\n}","handlingStrategy":"validation","validationCode":"Class<?> clazz = Class.forName(observerClassName);\nint mods = clazz.getModifiers();\nif (!Modifier.isPublic(mods) || Modifier.isAbstract(mods)\n        || !EntityCopyObserver.class.isAssignableFrom(clazz)) {\n    throw new IllegalStateException(observerClassName + \" is not a usable observer\");\ntry {\n    clazz.getConstructor(); // must have public no-arg constructor\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(observerClassName + \" needs a public no-arg constructor\");\n}","typeGuard":"boolean instantiableObserver(String className) {\n    try {\n        Class<?> c = Class.forName(className);\n        return EntityCopyObserver.class.isAssignableFrom(c) && c.getConstructor() != null;\n    } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n    sf = new Configuration().configure().buildSessionFactory();\n} catch (HibernateException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Could not instantiate\")) {\n        // fix the hibernate.event.merge.entity_copy_observer class definition\n    }\n    throw e;\n}","preventionTips":["Custom EntityCopyObserver implementations must be public with a public no-arg constructor","Unit-test instantiating configured-by-name classes at build time, not first merge","Keep observer constructors side-effect free"],"tags":["hibernate","configuration","reflection","entity-copy-observer","merge"],"backgroundTag":"config-class-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}