{"record":{"id":"68b2a750f0b5d2d9","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-entity-68b2a7","errorCode":null,"errorMessage":"Could not instantiate entity","messagePattern":"Could not instantiate entity","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java","lineNumber":101,"sourceCode":"\t\treturn super.isInstance( object )\n\t\t\t// this one needed only for guessEntityMode()\n\t\t\t|| proxyInterface != null && proxyInterface.isInstance( object );\n\t}\n\n\t@Override\n\tpublic Object instantiate() {\n\t\tif ( isAbstract() ) {\n\t\t\tthrow new InstantiationException( \"Cannot instantiate abstract class or interface\", getMappedPojoClass() );\n\t\t}\n\t\telse if ( constructor == null ) {\n\t\t\tthrow new InstantiationException( \"No default constructor for entity\", getMappedPojoClass() );\n\t\t}\n\t\telse {\n\t\t\ttry {\n\t\t\t\treturn applyInterception( constructor.newInstance( (Object[]) null ) );\n\t\t\t}\n\t\t\tcatch ( Exception e ) {\n\t\t\t\tthrow new InstantiationException( \"Could not instantiate entity\", getMappedPojoClass(), e );\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":83,"sourceCodeEnd":106,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java#L83-L106","documentation":"Thrown by EntityInstantiatorPojoStandard.instantiate() when invoking the previously resolved default constructor throws. The class is concrete and the no-arg constructor exists, but constructor.newInstance((Object[]) null) fails - either the constructor body itself raises an exception, or reflective access is denied (non-public constructor/class in a package not accessible to Hibernate, Java 9+ strong encapsulation). The root cause is chained onto the InstantiationException.","triggerScenarios":"A default constructor that throws on its implicit defaults (NPE initializing fields from statics, failing time sources); package-private or private no-arg constructors in named modules without 'opens'; entity class non-public and the constructor not accessible from hibernate-core; constructors performing I/O or context lookups that fail in the persistence environment.","commonSituations":"Service-locator or clock calls inside default constructors that break inside Hibernate threads; JPMS modularized applications where entity packages are not opened to hibernate.core; exotic classloader setups (OSGi, war/ear redeploys) where accessibility checks fail intermittently.","solutions":["Inspect getCause() - IllegalAccessException means an access problem, anything else points at the constructor body","Make the no-arg constructor and the entity class at least package-visible to Hibernate, and add 'opens <entity.package> to hibernate.core' under JPMS","Move initialization logic that can throw out of the default constructor into @PostLoad/@PrePersist callbacks or factories","Verify no classpath/classloader split puts a stale (non-public-ctor) version of the class in front of Hibernate"],"exampleFix":"// before - default ctor does environment-dependent work\n@Entity\npublic class Report {\n    protected Report() {\n        this.generatedAt = ClockHolder.clock().instant();  // throws if holder unset\n    }\n}\n\n// after - keep the ctor trivial\n@Entity\npublic class Report {\n    protected Report() { }\n\n    @PrePersist\n    void stamp() { this.generatedAt = Instant.now(); }\n}","handlingStrategy":"try-catch","validationCode":"// Startup: verify the default ctor is callable in the runtime environment\nfor ( EntityType<?> t : emf.getMetamodel().getEntities() ) {\n    Class<?> c = t.getJavaType();\n    if ( c != null && !c.isInterface() && !Modifier.isAbstract(c.getModifiers()) ) {\n        try { c.getDeclaredConstructor().newInstance(); }\n        catch (ReflectiveOperationException e) { throw new IllegalStateException(\"Entity default ctor not invocable: \" + c, e); }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return session.find(Order.class, id);\n}\ncatch ( org.hibernate.InstantiationException e ) {\n    Throwable root = e.getCause() != null ? e.getCause() : e;\n    if ( root instanceof IllegalAccessException ) {\n        throw new IllegalStateException(\"Entity constructor not accessible - check visibility/JPMS opens\", root);\n    }\n    throw new IllegalStateException(\"Entity default constructor threw\", root);\n}","preventionTips":["Keep entity no-arg constructors trivial - no I/O, service lookups, or throwing initializers","Under JPMS, 'opens <entity.package> to hibernate.core' in module-info.java","Smoke-test one em.find per entity in CI to execute default constructors in a real environment"],"tags":["hibernate","jpa","entity","instantiation","reflection","jpms"],"backgroundTag":"entity-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}