{"record":{"id":"cd33945074e50c45","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-managed-bean-directly","errorCode":null,"errorMessage":"Could not instantiate managed bean directly","messagePattern":"Could not instantiate managed bean directly","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java","lineNumber":41,"sourceCode":"public class FallbackBeanInstanceProducer implements BeanInstanceProducer {\n\t/**\n\t * Singleton access\n\t */\n\tpublic static final FallbackBeanInstanceProducer INSTANCE = new FallbackBeanInstanceProducer();\n\n\tprivate FallbackBeanInstanceProducer() {\n\t}\n\n\t@Override\n\tpublic <B> B produceBeanInstance(Class<B> beanType) {\n\t\tBEANS_MSG_LOGGER.creatingManagedBeanUsingDirectInstantiation( beanType.getName() );\n\t\ttry {\n\t\t\tfinal var constructor = beanType.getDeclaredConstructor();\n\t\t\tconstructor.setAccessible( true );\n\t\t\treturn constructor.newInstance();\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new InstantiationException( \"Could not instantiate managed bean directly\", beanType, e );\n\t\t}\n\t}\n\n\t@Override\n\tpublic <B> B produceBeanInstance(String name, Class<B> beanType) {\n\t\treturn produceBeanInstance( beanType );\n\t}\n\n}\n","sourceCodeStart":23,"sourceCodeEnd":51,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java#L23-L51","documentation":"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').","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic final class CreatedAtListener {\n    private final Clock clock;\n    public CreatedAtListener(Clock clock) { this.clock = clock; } // no no-arg ctor\n}\n\n// after\npublic final class CreatedAtListener {\n    private final Clock clock = Clock.systemUTC();\n    public CreatedAtListener() { }\n}","handlingStrategy":"validation","validationCode":"// preflight: can the fallback producer construct this class?\nstatic void assertFallbackConstructable(Class<?> c) {\n    try {\n        java.lang.reflect.Constructor<?> ctor = c.getDeclaredConstructor();\n        ctor.setAccessible(true);\n    } catch (ReflectiveOperationException e) {\n        throw new IllegalStateException(c.getName() + \" needs an accessible no-arg constructor\", e);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return producer.produceBeanInstance(type);\n} catch (org.hibernate.InstantiationException e) {\n    // direct instantiation failed: report which class and why (missing/throwing ctor)\n    throw new BeanSetupException(\"Cannot instantiate \" + type.getName(), e);\n}","preventionTips":["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."],"tags":["hibernate","reflection","constructor","bean-instantiation","cdi"],"backgroundTag":"missing-no-arg-constructor","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}